3 import java.io.IOException;
4 import java.net.MalformedURLException;
6 import java.util.ArrayList;
7 import java.util.Collections;
8 import java.util.Comparator;
10 import java.util.HashMap;
11 import java.util.List;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.ScheduledExecutorService;
15 import java.util.concurrent.TimeUnit;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
19 import org.jsoup.Jsoup;
21 import com.rometools.rome.feed.synd.SyndEnclosure;
22 import com.rometools.rome.feed.synd.SyndEntry;
23 import com.rometools.rome.feed.synd.SyndFeed;
24 import com.rometools.rome.io.FeedException;
25 import com.rometools.rome.io.SyndFeedInput;
26 import com.rometools.rome.io.XmlReader;
29 import pnews.Category;
31 public class ArticleProvider {
32 public final static ArticleProvider singleton = new ArticleProvider();
33 private static final Logger LOG = Logger.getLogger(ArticleProvider.class.getName());
34 private final Map<Category, List<Article>> articlesByCategory = new HashMap<>();
35 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
37 private ArticleProvider() {
38 for (Category cat:Category.values())
39 scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 600, TimeUnit.SECONDS);
42 private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
45 r = new XmlReader(new URL(u));
46 XmlReader.setDefaultEncoding("UTF-8");
48 return new SyndFeedInput().build(r);
51 private static Map<Category, String[]> getFeeds() {
52 Map<Category, String[]> result;
54 result = new HashMap<>();
56 result.put(Category.TOP,
58 "http://www.francetvinfo.fr/titres.rss",
59 "http://www.rfi.fr/general/rss",
60 "http://www.cnews.fr/rss/une",
61 "http://www.ladepeche.fr/rss/a-la-une.rss",
62 "https://www.franceinter.fr/rss/a-la-une.xml",
63 "https://www.francebleu.fr/rss/a-la-une.xml",
64 "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
67 result.put(Category.SPORT,
68 new String[] { "http://www.france24.com/fr/sports/rss" });
70 result.put(Category.FRANCE,
71 new String[] { "http://www.france24.com/fr/france/rss",
72 "http://www.rfi.fr/france/rss"});
74 result.put(Category.EUROPE,
75 new String[] { "http://www.france24.com/fr/europe/rss" });
77 result.put(Category.MONDE,
78 new String[] { "http://www.france24.com/fr/actualites/rss" });
81 result.put(Category.ECO,
82 new String[] { "http://www.france24.com/fr/economie/rss",
83 "http://www.rfi.fr/economie/rss" });
85 result.put(Category.ESSONNE,
86 new String[] { "http://www.tourisme-essonne.com/rss/actus/",
87 "http://www.ville-palaiseau.fr/rss/actualites.htm" });
89 result.put(Category.PEOPLE,
90 new String[] { "http://www.premiere.fr/rss/actu-live",
91 "http://www.purepeople.com/rss/news_t0.xml"
94 result.put(Category.TECHNOLOGIE,
95 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
96 "http://www.zdnet.fr/feeds/rss/actualites/"});
101 private List<Article> getArticlesForUpdate(Category cat) {
102 List<Article> result;
104 synchronized (articlesByCategory) {
105 result = articlesByCategory.get(cat);
106 if (result == null) {
107 result = new ArrayList<>();
108 articlesByCategory.put(cat, result);
114 private boolean exists(String articleLink, List<Article> articles) {
115 synchronized (articles) {
116 for (Article a: articles)
117 if (a.link.equals(articleLink))
123 private Article toArticle(String link, Category cat, SyndEntry entry, SyndFeed feed) {
124 String desc, title, thumbnail;
128 for (SyndEnclosure e: entry.getEnclosures()) {
129 if (e.getType().startsWith("image/"))
130 thumbnail = e.getUrl();
134 if (thumbnail == null && feed.getImage() != null)
135 thumbnail = feed.getImage().getUrl();
137 title = entry.getTitle().trim();
139 if (entry.getDescription() != null) {
140 desc = Jsoup.parse(entry.getDescription().getValue()).text();
143 LOG.severe("No description for " + feed.getTitle() + " - " + title);
146 date = entry.getPublishedDate();
148 date = entry.getUpdatedDate();
150 LOG.severe("The article " + feed.getTitle() + " - " + title + " does not have a date");
152 return new Article(link, cat, title, desc, thumbnail, date, title);
157 private void addArticles(Category cat, SyndFeed feed) {
158 String link, feedTitle;
159 List<Article> articles;
162 feedTitle = feed.getTitle().trim();
164 LOG.info("addArticles " + cat.getId() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
166 for (SyndEntry entry: feed.getEntries()) {
167 link = entry.getLink().trim();
168 articles = getArticlesForUpdate(cat);
169 if (exists(link, articles)) {
170 LOG.fine("addArticles " + link + " is already present");
174 a = toArticle(link, cat, entry, feed);
176 synchronized (articles) {
179 Collections.sort(articles, new Comparator<Article>() {
181 public int compare(Article o1, Article o2) {
182 if (o1.publicationDate == o2.publicationDate)
184 if (o1.publicationDate == null)
186 if (o2.publicationDate == null)
188 return o2.publicationDate.compareTo(o1.publicationDate);
195 LOG.info("addArticles done " + cat.getId());
198 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
201 feeds = getFeeds().get(cat);
204 for (String str: feeds)
206 addArticles(cat, getSyndFeed(str));
207 } catch (Throwable e) {
208 LOG.log(Level.SEVERE,
209 "retrieveArticles failure " + cat.getId() + " " + str,
213 LOG.severe("No feed for category " + cat);
219 public List<Article> getArticles(Category cat)
220 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
221 List<Article> articles;
223 synchronized (articlesByCategory) {
224 articles = getArticlesForUpdate(cat);
227 synchronized (articles) {
228 return new ArrayList<>(articles);
232 private class Refresher implements Runnable {
233 private final Category category;
235 public Refresher(Category category) {
236 this.category = category;
241 List<Article> articles;
243 LOG.info("refresher "+ category.getId());
246 retrieveArticles(category);
248 synchronized (articlesByCategory) {
249 articles = articlesByCategory.get(category);
250 if (articles != null && articles.size() > 100) {
251 articlesByCategory.put(category,
252 articles.subList(0, 100));
255 LOG.info("refresher " + category.getId() + " number of articles: " + articles.size());
257 } catch (IllegalArgumentException | FeedException | IOException e) {
258 LOG.log(Level.SEVERE, "refresher failure", e);
261 LOG.info("refresher "+ category.getId() + " done");