3 import java.io.IOException;
4 import java.net.MalformedURLException;
6 import java.util.ArrayList;
7 import java.util.Comparator;
9 import java.util.HashMap;
10 import java.util.List;
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;
18 import org.jsoup.Jsoup;
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;
28 import pnews.Category;
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);
36 private ArticleProvider() {
37 for (Category cat:Category.values())
38 scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 120, TimeUnit.SECONDS);
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);
47 private static Map<Category, String[]> getFeeds() {
48 Map<Category, String[]> result;
50 result = new HashMap<>();
52 result.put(Category.TOP,
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/"
62 result.put(Category.SPORT,
63 new String[] { "http://www.france24.com/fr/sports/rss" });
65 result.put(Category.FRANCE,
66 new String[] { "http://www.france24.com/fr/france/rss",
67 "http://www.rfi.fr/france/rss"});
69 result.put(Category.EUROPE,
70 new String[] { "http://www.france24.com/fr/europe/rss" });
72 result.put(Category.ECO,
73 new String[] { "http://www.france24.com/fr/economie/rss",
74 "http://www.rfi.fr/economie/rss" });
76 result.put(Category.ESSONNE,
77 new String[] { /*"https://www.essonneinfo.fr/feed/"*/ });
79 result.put(Category.TECHNOLOGIE,
80 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
81 "http://www.zdnet.fr/feeds/rss/actualites/"});
86 private void addArticles(Category cat, SyndFeed feed) {
90 List<Article> articles;
92 LOG.info("addArticles" + cat.getId());
94 for (SyndEntry entry: feed.getEntries()) {
96 for (SyndEnclosure e: entry.getEnclosures()) {
97 if (e.getType().startsWith("image/"))
98 thumbnail = e.getUrl();
102 if (entry.getDescription() != null) {
103 desc = Jsoup.parse(entry.getDescription().getValue()).text();
106 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
109 date = entry.getPublishedDate();
111 date = entry.getUpdatedDate();
113 synchronized(articlesByCategory) {
114 articles = articlesByCategory.get(cat);
115 if (articles == null) {
116 articles = new ArrayList<>();
117 articlesByCategory.put(cat, articles);
119 for (Article a: articles)
120 if (a.link.equals(entry.getLink()))
124 articles.add(new Article(entry.getLink(),
132 articles.sort(new Comparator<Article>() {
134 public int compare(Article o1, Article o2) {
135 return o2.publicationDate.compareTo(o1.publicationDate);
143 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
146 feeds = getFeeds().get(cat);
149 for (String str: feeds)
150 addArticles(cat, getSyndFeed(str));
152 LOG.severe("No feed for category " + cat);
155 public List<Article> getArticles(Category cat)
156 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
157 synchronized (articlesByCategory) {
158 return articlesByCategory.get(cat);
162 private class Refresher implements Runnable {
163 private final Category category;
165 public Refresher(Category category) {
166 this.category = category;
171 List<Article> articles;
173 LOG.info("refresher "+ category.getId());
176 retrieveArticles(category);
178 synchronized (articlesByCategory) {
179 articles = articlesByCategory.get(category);
180 if (articles != null && articles.size() > 100) {
181 articlesByCategory.put(category,
182 articles.subList(0, 100));
186 } catch (IllegalArgumentException | FeedException | IOException e) {
187 LOG.log(Level.SEVERE, "refresher failure", e);
190 LOG.info("refresher "+ category.getId() + " done");