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