removed useless vars
[pnews.git] / war / src / main / java / pnews / servlet / ArticleProvider.java
index 010317a..dd5daeb 100644 (file)
@@ -1,7 +1,6 @@
 package pnews.servlet;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
@@ -18,13 +17,13 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import org.jsoup.Jsoup;
-import org.xml.sax.InputSource;
 
 import com.rometools.rome.feed.synd.SyndEnclosure;
 import com.rometools.rome.feed.synd.SyndEntry;
 import com.rometools.rome.feed.synd.SyndFeed;
 import com.rometools.rome.io.FeedException;
 import com.rometools.rome.io.SyndFeedInput;
+import com.rometools.rome.io.XmlReader;
 
 import pnews.Article;
 import pnews.Category;
@@ -37,15 +36,16 @@ public class ArticleProvider {
         
         private ArticleProvider() {      
                 for (Category cat:Category.values())
-                        scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 120, TimeUnit.SECONDS);
+                        scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 600, TimeUnit.SECONDS);
         }
         
         private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
-                InputStream is = new URL(u).openConnection().getInputStream();
-                InputSource source = new InputSource(is);
-                                
-                return new SyndFeedInput().build(source);
+                XmlReader r;
+                
+                r = new XmlReader(new URL(u));
+                XmlReader.setDefaultEncoding("UTF-8");
                 
+                return new SyndFeedInput().build(r);                
         }
         
         private static Map<Category, String[]> getFeeds() {
@@ -56,10 +56,11 @@ public class ArticleProvider {
                 result.put(Category.TOP,
                            new String[] {
                                            "http://www.francetvinfo.fr/titres.rss",
-                                           "http://www.france24.com/fr/actualites/rss",
-                                           //"https://www.franceinter.fr/rss/a-la-une.xml",
                                            "http://www.rfi.fr/general/rss",
                                            "http://www.cnews.fr/rss/une",
+                                           "http://www.ladepeche.fr/rss/a-la-une.rss",
+                                           "https://www.franceinter.fr/rss/a-la-une.xml",
+                                           "https://www.francebleu.fr/rss/a-la-une.xml",
                                            "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
                            });
                 
@@ -72,6 +73,10 @@ public class ArticleProvider {
                 
                 result.put(Category.EUROPE,
                                 new String[] { "http://www.france24.com/fr/europe/rss" });
+
+                result.put(Category.MONDE, 
+                           new String[] { "http://www.france24.com/fr/actualites/rss" });                                           
+
                 
                 result.put(Category.ECO,
                                 new String[] { "http://www.france24.com/fr/economie/rss",
@@ -79,8 +84,7 @@ public class ArticleProvider {
                 
                 result.put(Category.ESSONNE,
                                 new String[] { "http://www.tourisme-essonne.com/rss/actus/",
-                                               "http://www.ville-palaiseau.fr/rss/actualites.htm"
-                                                /*"https://www.essonneinfo.fr/feed/"*/ });
+                                               "http://www.ville-palaiseau.fr/rss/actualites.htm" });
                 
                 result.put(Category.PEOPLE,
                                 new String[] { "http://www.premiere.fr/rss/actu-live",
@@ -94,66 +98,101 @@ public class ArticleProvider {
                 return result;
         }
         
-        private void addArticles(Category cat, SyndFeed feed) {
-                String thumbnail;
-                String desc, link, title;
+        private List<Article> getArticlesForUpdate(Category cat) {
+                List<Article> result;
+                
+                synchronized (articlesByCategory) {
+                        result = articlesByCategory.get(cat);
+                        if (result == null) {
+                                result = new ArrayList<>();
+                                articlesByCategory.put(cat, result);
+                        }
+                        return result;
+                }                
+        }
+        
+        private boolean exists(String articleLink, List<Article> articles) {
+                synchronized (articles) {
+                        for (Article a: articles)
+                                if (a.link.equals(articleLink))
+                                        return true;
+                }
+                return false;
+        }
+        
+        private Article toArticle(String link, Category cat, SyndEntry entry, SyndFeed feed) {
+                String desc, title, thumbnail;
                 Date date;
+
+                thumbnail = null;
+                for (SyndEnclosure e: entry.getEnclosures()) {
+                        if (e.getType().startsWith("image/"))
+                                thumbnail = e.getUrl();    
+                        break;
+                }
+                
+                if (thumbnail == null && feed.getImage() != null)
+                        thumbnail = feed.getImage().getUrl();
+                                                
+                title = entry.getTitle().trim();
+                
+                if (entry.getDescription() != null) {                                      
+                        desc = Jsoup.parse(entry.getDescription().getValue()).text();
+                } else {       
+                        desc = null;
+                        LOG.severe("No description for " + feed.getTitle() + " - " + title);
+                }
+                
+                date = entry.getPublishedDate();
+                if (date == null)
+                        date = entry.getUpdatedDate();
+                if (date == null)
+                        LOG.severe("The article " + feed.getTitle() + " - " + title + " does not have a date");
+                                     
+                return new Article(link, cat, title, desc, thumbnail, date, title);
+        }
+        
+        
+        
+        private void addArticles(Category cat, SyndFeed feed) {
+                String link, feedTitle;
                 List<Article> articles;
-                boolean exist;
+                Article a;
+                
+                feedTitle = feed.getTitle().trim();
                 
-                LOG.info("addArticles" + cat.getId());
+                LOG.info("addArticles " + cat.getId() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
                 
                 for (SyndEntry entry: feed.getEntries()) {
-                        thumbnail = null;
-                        for (SyndEnclosure e: entry.getEnclosures()) {
-                                if (e.getType().startsWith("image/"))
-                                        thumbnail = e.getUrl();    
-                                break;
-                        }
-                                
-                        if (entry.getDescription() != null) {                                      
-                                desc = Jsoup.parse(entry.getDescription().getValue()).text();
-                        } else {       
-                                desc = null;
-                                LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
+                        link = entry.getLink().trim();
+                        articles = getArticlesForUpdate(cat);
+                        if (exists(link, articles)) {
+                                LOG.fine("addArticles " + link + " is already present");
+                                continue ;
                         }
                         
-                        date = entry.getPublishedDate();
-                        if (date == null)
-                                date = entry.getUpdatedDate();
+                        a = toArticle(link, cat, entry, feed);
                         
-                        synchronized(articlesByCategory) {
-                                link = entry.getLink().trim();
-                                title = entry.getTitle().trim();
-                                articles = articlesByCategory.get(cat);
-                                exist = false;
-                                if (articles == null) {
-                                        articles = new ArrayList<>();
-                                        articlesByCategory.put(cat, articles);
-                                } else {                                
-                                        for (Article a: articles)
-                                                if (a.link.equals(link)) {
-                                                        LOG.finest(link + " already present");
-                                                        exist = true;
-                                                }
-                                }
-                                
-                                if (!exist) {
-                                        LOG.info("add " + cat.getId() + " " + feed.getTitle() + " " + title);
-                                
-                                        articles.add(new Article(link, cat, title, desc, thumbnail, date,
-                                                        feed.getTitle()));
+                        synchronized (articles) {
+                                articles.add(a);
 
-                                        Collections.sort(articles, new Comparator<Article>() {
-                                                @Override
-                                                public int compare(Article o1, Article o2) {
-                                                        return o2.publicationDate.compareTo(o1.publicationDate);
-                                                }
+                                Collections.sort(articles, new Comparator<Article>() {
+                                        @Override
+                                        public int compare(Article o1, Article o2) {
+                                                if (o1.publicationDate == o2.publicationDate)
+                                                        return 0;
+                                                if (o1.publicationDate == null)
+                                                        return 1;
+                                                if (o2.publicationDate == null)
+                                                        return -1;
+                                                return o2.publicationDate.compareTo(o1.publicationDate);
+                                        }
 
-                                        });
-                                }
+                                });
                         }
-                }               
+                }          
+                
+                LOG.info("addArticles done " + cat.getId());
         }
         
         private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
@@ -165,17 +204,28 @@ public class ArticleProvider {
                         for (String str: feeds)
                                 try {
                                         addArticles(cat, getSyndFeed(str));
-                                } catch (IOException e) {
-                                        LOG.log(Level.SEVERE, "retrieveArticles failure " + cat.getId(), e);
+                                } catch (Throwable e) {
+                                        LOG.log(Level.SEVERE,
+                                                "retrieveArticles failure " + cat.getId() + " " + str,
+                                                e);
                                 }
                 else
                         LOG.severe("No feed for category " + cat);
         }
         
+        /**
+         * Returns a copy.
+         */
         public List<Article> getArticles(Category cat)
                         throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+                List<Article> articles;
+                
                 synchronized (articlesByCategory) {
-                        return articlesByCategory.get(cat);
+                        articles = getArticlesForUpdate(cat);
+                }
+                
+                synchronized (articles) {
+                        return new ArrayList<>(articles);
                 }
         }
         
@@ -202,6 +252,7 @@ public class ArticleProvider {
                                                                        articles.subList(0, 100));
                                                                 
                                         }
+                                        LOG.info("refresher " + category.getId() + " number of articles: " + articles.size());
                                 }
                         } catch (IllegalArgumentException | FeedException | IOException e) {
                                 LOG.log(Level.SEVERE, "refresher failure", e);