updated
[pnews.git] / war / src / main / java / pnews / servlet / ArticleProvider.java
1 package pnews.servlet;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Comparator;
8 import java.util.Date;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.concurrent.Executors;
13 import java.util.concurrent.ScheduledExecutorService;
14 import java.util.concurrent.TimeUnit;
15 import java.util.logging.Level;
16 import java.util.logging.Logger;
17
18 import org.jsoup.Jsoup;
19
20 import com.rometools.rome.feed.synd.SyndEnclosure;
21 import com.rometools.rome.feed.synd.SyndEntry;
22 import com.rometools.rome.feed.synd.SyndFeed;
23 import com.rometools.rome.io.FeedException;
24 import com.rometools.rome.io.SyndFeedInput;
25 import com.rometools.rome.io.XmlReader;
26
27 import pnews.Article;
28 import pnews.Category;
29
30 public class ArticleProvider {
31         public final static ArticleProvider singleton = new ArticleProvider();
32         private static final Logger LOG = Logger.getLogger(ArticleProvider.class.getName());
33         private final Map<Category, List<Article>> articlesByCategory = new HashMap<>();
34         private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
35         
36         private ArticleProvider() {      
37                 for (Category cat:Category.values())
38                         scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 120, TimeUnit.SECONDS);
39         }
40         
41         private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
42                 try (XmlReader reader = new XmlReader(new URL(u))) {
43                         return new SyndFeedInput().build(reader);
44                 }
45         }
46         
47         private static Map<Category, String[]> getFeeds() {
48                 Map<Category, String[]> result;
49                 
50                 result = new HashMap<>();
51                 
52                 result.put(Category.TOP,
53                            new String[] {
54                                            "http://www.francetvinfo.fr/titres.rss",
55                                            "http://www.france24.com/fr/actualites/rss",
56                                            //"https://www.franceinter.fr/rss/a-la-une.xml",
57                                            "http://www.rfi.fr/general/rss",
58                                            "http://www.cnews.fr/rss/une",
59                                            "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
60                            });
61                 
62                 result.put(Category.SPORT,
63                                 new String[] { "http://www.france24.com/fr/sports/rss" });
64                 
65                 result.put(Category.FRANCE,
66                                 new String[] { "http://www.france24.com/fr/france/rss",
67                                                "http://www.rfi.fr/france/rss"});
68                 
69                 result.put(Category.EUROPE,
70                                 new String[] { "http://www.france24.com/fr/europe/rss" });
71                 
72                 result.put(Category.ECO,
73                                 new String[] { "http://www.france24.com/fr/economie/rss",
74                                                "http://www.rfi.fr/economie/rss" });
75                 
76                 result.put(Category.ESSONNE,
77                                 new String[] { /*"https://www.essonneinfo.fr/feed/"*/ });
78                 
79                 result.put(Category.TECHNOLOGIE,
80                                 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
81                                                "http://www.zdnet.fr/feeds/rss/actualites/"});
82                 
83                 return result;
84         }
85         
86         private void addArticles(Category cat, SyndFeed feed) {
87                 String thumbnail;
88                 String desc;
89                 Date date;
90                 List<Article> articles;
91                 
92                 LOG.info("addArticles" + cat.getId());
93                 
94                 for (SyndEntry entry: feed.getEntries()) {
95                         thumbnail = null;
96                         for (SyndEnclosure e: entry.getEnclosures()) {
97                                 if (e.getType().startsWith("image/"))
98                                         thumbnail = e.getUrl();    
99                                 break;
100                         }
101                                 
102                         if (entry.getDescription() != null) {                             
103                                 desc = Jsoup.parse(entry.getDescription().getValue()).text();
104                         } else {       
105                                 desc = null;
106                                 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
107                         }
108                         
109                         date = entry.getPublishedDate();
110                         if (date == null)
111                                 date = entry.getUpdatedDate();
112                         
113                         synchronized(articlesByCategory) {
114                                 articles = articlesByCategory.get(cat);
115                                 if (articles == null) {
116                                         articles = new ArrayList<>();
117                                         articlesByCategory.put(cat, articles);
118                                 } else {                                
119                                         for (Article a: articles)
120                                                 if (a.link.equals(entry.getLink()))
121                                                         return ;
122                                 }
123                                 
124                                 articles.add(new Article(entry.getLink(),
125                                                          cat,
126                                                          entry.getTitle(),
127                                                          desc,
128                                                          thumbnail,
129                                                          date,
130                                                          feed.getTitle()));
131
132                                 articles.sort(new Comparator<Article>() {
133                                         @Override
134                                         public int compare(Article o1, Article o2) {
135                                                 return o2.publicationDate.compareTo(o1.publicationDate);
136                                         }
137                                         
138                                 });
139                         }
140                 }               
141         }
142         
143         private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
144                 String[] feeds;
145                 
146                 feeds = getFeeds().get(cat);
147                 
148                 if (feeds != null)
149                         for (String str: feeds)
150                                 addArticles(cat, getSyndFeed(str));
151                 else
152                         LOG.severe("No feed for category " + cat);
153         }
154         
155         public List<Article> getArticles(Category cat)
156                         throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
157                 synchronized (articlesByCategory) {
158                         return articlesByCategory.get(cat);
159                 }
160         }
161         
162         private class Refresher implements Runnable {
163                 private final Category category;
164                 
165                 public Refresher(Category category) {
166                         this.category = category;
167                 }
168                 
169                 @Override
170                 public void run() {
171                         List<Article> articles;
172                         
173                         LOG.info("refresher "+ category.getId());
174                         
175                         try {
176                                 retrieveArticles(category);
177                                 
178                                 synchronized (articlesByCategory) {
179                                         articles = articlesByCategory.get(category);
180                                         if (articles != null && articles.size() > 100) {
181                                                 articlesByCategory.put(category,
182                                                                        articles.subList(0, 100));
183                                                                 
184                                         }
185                                 }
186                         } catch (IllegalArgumentException | FeedException | IOException e) {
187                                 LOG.log(Level.SEVERE, "refresher failure", e);
188                         }                        
189                         
190                         LOG.info("refresher "+ category.getId() + " done");
191                 }                
192         }
193 }