RSSChecker.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package rss;
  2. import java.io.IOException;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import javax.xml.parsers.ParserConfigurationException;
  9. import org.w3c.dom.DOMException;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Node;
  12. import org.w3c.dom.NodeList;
  13. import org.xml.sax.SAXException;
  14. import main.Bot;
  15. import main.Main;
  16. /**
  17. * Cette classe verifie si il y a de nouveaux articles sur le flux RSS.
  18. * Dans ce cas, il les affiche et les met en mémoire pour l'action +RSS
  19. * @author marmat
  20. */
  21. public class RSSChecker implements Runnable {
  22. private Thread thread;
  23. private final String threadName="RssChecker";
  24. private long timeout=3600;
  25. private String rssaddr; // Must be https://planet.ffdn.org/atom.xml
  26. boolean end = false;
  27. DocumentBuilderFactory docbfact=DocumentBuilderFactory.newInstance();
  28. private Date lastarticle = new Date(); //last info
  29. public static final SimpleDateFormat DATE_FORMATIN = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'");
  30. private Bot b;
  31. private volatile RssDataRemainder remainder;
  32. public RSSChecker() {
  33. this("",null);
  34. }
  35. public RSSChecker(String address, Bot b) {
  36. this.rssaddr = address;
  37. this.b=b;
  38. this.remainder = b.getRssdata();
  39. }
  40. public void start() {
  41. if(thread == null) {
  42. thread = new Thread(this, this.threadName);
  43. thread.start();
  44. }
  45. }
  46. private void afficheArticle(RssData data) {
  47. b.sendMessagesOnAllChannels(data.toStringIRC());
  48. }
  49. private void afficheArticle(Node article) {
  50. afficheArticle(new RssData(article));
  51. }
  52. private void workOnEntry(NodeList nl) {
  53. if(Main.isDebug()) {
  54. System.out.println("Verification des <entry>");
  55. System.out.println("Dernier article le: "+Main.DATE_FORMAT_OUT.format(lastarticle));
  56. }
  57. int len = nl.getLength();
  58. boolean istherenews = false;
  59. for(int i=len-1; i>=0;i--) {
  60. Node article = nl.item(i);
  61. NodeList fils = article.getChildNodes();
  62. int flen = fils.getLength();
  63. for(int j=0; j<flen;j++) {
  64. if(fils.item(j).getNodeName().equalsIgnoreCase("updated")){
  65. try {
  66. Date date = DATE_FORMATIN.parse(fils.item(j).getTextContent());
  67. if(Main.isDebug()) {
  68. System.out.println("Date de l'article: "+Main.DATE_FORMAT_OUT.format(date));
  69. }
  70. if(date.after(lastarticle)) {
  71. if(Main.isDebug()) {
  72. System.out.println("Cet article est nouveau.");
  73. }
  74. if(!istherenews) {
  75. istherenews=true;
  76. b.sendMessageOnAllChannels("Nouveautée sur planet.ffdn.org:");
  77. }
  78. RssData rs = new RssData(article, date);
  79. remainder.push(rs);
  80. afficheArticle(rs);
  81. lastarticle = date;
  82. }
  83. } catch (DOMException | ParseException e) {
  84. e.printStackTrace();
  85. }
  86. }
  87. }
  88. }
  89. }
  90. public RssDataRemainder getRemainder() {
  91. return remainder;
  92. }
  93. public void setRemainder(RssDataRemainder remainder) {
  94. this.remainder = remainder;
  95. }
  96. @Override
  97. public void run() {
  98. if(Main.isDebug()) {
  99. System.out.println(this.threadName+" lancé sur "+rssaddr);
  100. }
  101. boolean firstRun = true;
  102. do {
  103. if(Main.isDebug()) {
  104. System.out.println("Parsing du RSS "+rssaddr);
  105. }
  106. DocumentBuilder db=null;
  107. Document doc = null;
  108. try {
  109. db = docbfact.newDocumentBuilder();
  110. } catch (ParserConfigurationException e1) {
  111. b.sendMessageToAdmins("Erreur lors de la creation du document Builder dans RSSChecker.run()");
  112. e1.printStackTrace();
  113. }
  114. try {
  115. doc = db.parse(rssaddr);
  116. } catch (SAXException | IOException e1) {
  117. e1.printStackTrace();
  118. b.sendMessageToAdmins("Erreur du parseur XML");
  119. }
  120. if(doc!=null) {
  121. NodeList nl = doc.getElementsByTagName("entry");
  122. if(firstRun) {
  123. initRemainder(nl);
  124. }
  125. workOnEntry(nl);
  126. }else {
  127. b.sendMessageToAdmins("Erreur au parsing du RSS, le document était null");
  128. System.err.println("Erreur au parsing du RSS, le document était null");
  129. }
  130. try {
  131. Thread.sleep(1000*timeout);
  132. } catch (InterruptedException e) {
  133. System.err.println(this.threadName+" à été arrété");
  134. }
  135. firstRun=false;
  136. }while(!end);
  137. }
  138. private void initRemainder(NodeList nl) {
  139. int len = nl.getLength();
  140. for(int i=len-1; i>=0;--i) {
  141. RssData data = new RssData(nl.item(i));
  142. remainder.push(data);
  143. }
  144. }
  145. public String getRssaddr() {
  146. return rssaddr;
  147. }
  148. public void setRssaddr(String rssaddr) {
  149. this.rssaddr = rssaddr;
  150. }
  151. public long getTimeout() {
  152. return timeout;
  153. }
  154. public void setTimeout(long timeout) {
  155. this.timeout = timeout;
  156. }
  157. }