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, 120, TimeUnit.SECONDS);
42 private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
43 try (XmlReader reader = new XmlReader(new URL(u))) {
44 return new SyndFeedInput().build(reader);
48 private static Map<Category, String[]> getFeeds() {
49 Map<Category, String[]> result;
51 result = new HashMap<>();
53 result.put(Category.TOP,
55 "http://www.francetvinfo.fr/titres.rss",
56 "http://www.france24.com/fr/actualites/rss",
57 //"https://www.franceinter.fr/rss/a-la-une.xml",
58 "http://www.rfi.fr/general/rss",
59 "http://www.cnews.fr/rss/une",
60 "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
63 result.put(Category.SPORT,
64 new String[] { "http://www.france24.com/fr/sports/rss" });
66 result.put(Category.FRANCE,
67 new String[] { "http://www.france24.com/fr/france/rss",
68 "http://www.rfi.fr/france/rss"});
70 result.put(Category.EUROPE,
71 new String[] { "http://www.france24.com/fr/europe/rss" });
73 result.put(Category.ECO,
74 new String[] { "http://www.france24.com/fr/economie/rss",
75 "http://www.rfi.fr/economie/rss" });
77 result.put(Category.ESSONNE,
78 new String[] { /*"https://www.essonneinfo.fr/feed/"*/ });
80 result.put(Category.TECHNOLOGIE,
81 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
82 "http://www.zdnet.fr/feeds/rss/actualites/"});
87 private void addArticles(Category cat, SyndFeed feed) {
91 List<Article> articles;
93 LOG.info("addArticles" + cat.getId());
95 for (SyndEntry entry: feed.getEntries()) {
97 for (SyndEnclosure e: entry.getEnclosures()) {
98 if (e.getType().startsWith("image/"))
99 thumbnail = e.getUrl();
103 if (entry.getDescription() != null) {
104 desc = Jsoup.parse(entry.getDescription().getValue()).text();
107 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
110 date = entry.getPublishedDate();
112 date = entry.getUpdatedDate();
114 synchronized(articlesByCategory) {
115 articles = articlesByCategory.get(cat);
116 if (articles == null) {
117 articles = new ArrayList<>();
118 articlesByCategory.put(cat, articles);
120 for (Article a: articles)
121 if (a.link.equals(entry.getLink()))
125 articles.add(new Article(entry.getLink(),
134 Collections.sort(articles, new Comparator<Article>() {
136 public int compare(Article o1, Article o2) {
137 return o2.publicationDate.compareTo(o1.publicationDate);
145 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
148 feeds = getFeeds().get(cat);
151 for (String str: feeds)
152 addArticles(cat, getSyndFeed(str));
154 LOG.severe("No feed for category " + cat);
157 public List<Article> getArticles(Category cat)
158 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
159 synchronized (articlesByCategory) {
160 return articlesByCategory.get(cat);
164 private class Refresher implements Runnable {
165 private final Category category;
167 public Refresher(Category category) {
168 this.category = category;
173 List<Article> articles;
175 LOG.info("refresher "+ category.getId());
178 retrieveArticles(category);
180 synchronized (articlesByCategory) {
181 articles = articlesByCategory.get(category);
182 if (articles != null && articles.size() > 100) {
183 articlesByCategory.put(category,
184 articles.subList(0, 100));
188 } catch (IllegalArgumentException | FeedException | IOException e) {
189 LOG.log(Level.SEVERE, "refresher failure", e);
192 LOG.info("refresher "+ category.getId() + " done");