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