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));
47 return new SyndFeedInput().build(r);
50 private static Map<Category, String[]> getFeeds() {
51 Map<Category, String[]> result;
53 result = new HashMap<>();
55 result.put(Category.TOP,
57 "http://www.francetvinfo.fr/titres.rss",
58 "http://www.rfi.fr/general/rss",
59 "http://www.cnews.fr/rss/une",
60 "http://www.ladepeche.fr/rss/a-la-une.rss",
61 "https://www.franceinter.fr/rss/a-la-une.xml",
62 "https://www.francebleu.fr/rss/a-la-une.xml",
63 "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
66 result.put(Category.SPORT,
67 new String[] { "http://www.france24.com/fr/sports/rss" });
69 result.put(Category.FRANCE,
70 new String[] { "http://www.france24.com/fr/france/rss",
71 "http://www.francetvinfo.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/",
97 "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all"});
102 private List<Article> getArticlesForUpdate(Category cat) {
103 List<Article> result;
105 synchronized (articlesByCategory) {
106 result = articlesByCategory.get(cat);
107 if (result == null) {
108 result = new ArrayList<>();
109 articlesByCategory.put(cat, result);
115 private boolean exists(String articleLink, List<Article> articles) {
116 synchronized (articles) {
117 for (Article a: articles)
118 if (a.link.equals(articleLink))
124 private static Article toArticle(String link, SyndEntry entry, SyndFeed feed) {
125 String desc, title, thumbnail, feedTitle, str;
128 feedTitle = feed.getTitle();
129 if (feedTitle != null) {
130 feedTitle = feedTitle.trim();
134 for (SyndEnclosure e: entry.getEnclosures()) {
135 if (e.getType().startsWith("image/"))
136 thumbnail = e.getUrl();
140 if (thumbnail == null && feed.getImage() != null)
141 thumbnail = feed.getImage().getUrl();
144 title = entry.getTitle().trim();
146 if (entry.getDescription() != null) {
147 str = entry.getDescription().getValue();
148 desc = Jsoup.parse(str).text();
151 LOG.severe("No description for " + feedTitle + " - " + title);
154 date = entry.getPublishedDate();
156 date = entry.getUpdatedDate();
158 LOG.severe("The article " + feedTitle + " - " + title + " does not have a date");
160 return new Article(link, title, desc, thumbnail, date, feedTitle);
163 private void addArticles(Category cat, SyndFeed feed) {
165 List<Article> articles;
168 feedTitle = feed.getTitle().trim();
170 LOG.info("addArticles " + cat.getId() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
172 for (SyndEntry entry: feed.getEntries()) {
173 String link = entry.getLink().trim();
174 articles = getArticlesForUpdate(cat);
175 if (exists(link, articles)) {
176 LOG.fine("addArticles " + link + " is already present");
180 a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed));
182 synchronized (articles) {
185 Collections.sort(articles, new Comparator<Article>() {
187 public int compare(Article o1, Article o2) {
188 if (o1.publicationDate == o2.publicationDate)
190 if (o1.publicationDate == null)
192 if (o2.publicationDate == null)
194 return o2.publicationDate.compareTo(o1.publicationDate);
200 LOG.info("addArticles done " + cat.getId());
203 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
206 feeds = getFeeds().get(cat);
209 for (String str: feeds)
211 addArticles(cat, getSyndFeed(str));
212 } catch (Throwable e) {
213 LOG.log(Level.SEVERE,
214 "retrieveArticles failure " + cat.getId() + " " + str,
218 LOG.severe("No feed for category " + cat);
224 public List<Article> getArticles(Category cat)
225 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
226 List<Article> articles;
228 synchronized (articlesByCategory) {
229 articles = getArticlesForUpdate(cat);
232 synchronized (articles) {
233 return new ArrayList<>(articles);
237 private class Refresher implements Runnable {
238 private final Category category;
240 public Refresher(Category category) {
241 this.category = category;
246 LOG.info("refresher "+ category.getId());
249 retrieveArticles(category);
250 } catch (IllegalArgumentException | FeedException | IOException e) {
251 LOG.log(Level.SEVERE, "refresher failure", e);
254 LOG.info("refresher "+ category.getId() + " done");