3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.MalformedURLException;
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;
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;
20 import org.jsoup.Jsoup;
21 import org.xml.sax.InputSource;
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;
30 import pnews.Category;
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);
38 private ArticleProvider() {
39 for (Category cat:Category.values())
40 scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 120, TimeUnit.SECONDS);
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);
47 return new SyndFeedInput().build(source);
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.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/"
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.rfi.fr/france/rss"});
73 result.put(Category.EUROPE,
74 new String[] { "http://www.france24.com/fr/europe/rss" });
76 result.put(Category.ECO,
77 new String[] { "http://www.france24.com/fr/economie/rss",
78 "http://www.rfi.fr/economie/rss" });
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/"*/ });
85 result.put(Category.PEOPLE,
86 new String[] { "http://www.premiere.fr/rss/actu-live",
87 "http://www.purepeople.com/rss/news_t0.xml"
90 result.put(Category.TECHNOLOGIE,
91 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
92 "http://www.zdnet.fr/feeds/rss/actualites/"});
97 private void addArticles(Category cat, SyndFeed feed) {
99 String desc, link, title;
101 List<Article> articles;
104 LOG.info("addArticles" + cat.getId());
106 for (SyndEntry entry: feed.getEntries()) {
108 for (SyndEnclosure e: entry.getEnclosures()) {
109 if (e.getType().startsWith("image/"))
110 thumbnail = e.getUrl();
114 if (entry.getDescription() != null) {
115 desc = Jsoup.parse(entry.getDescription().getValue()).text();
118 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
121 date = entry.getPublishedDate();
123 date = entry.getUpdatedDate();
125 synchronized(articlesByCategory) {
126 link = entry.getLink().trim();
127 title = entry.getTitle().trim();
128 articles = articlesByCategory.get(cat);
130 if (articles == null) {
131 articles = new ArrayList<>();
132 articlesByCategory.put(cat, articles);
134 for (Article a: articles)
135 if (a.link.equals(link)) {
136 LOG.finest(link + " already present");
142 LOG.info("add " + cat.getId() + " " + feed.getTitle() + " " + title);
144 articles.add(new Article(link, cat, title, desc, thumbnail, date,
147 Collections.sort(articles, new Comparator<Article>() {
149 public int compare(Article o1, Article o2) {
150 return o2.publicationDate.compareTo(o1.publicationDate);
159 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
162 feeds = getFeeds().get(cat);
165 for (String str: feeds)
167 addArticles(cat, getSyndFeed(str));
168 } catch (IOException e) {
169 LOG.log(Level.SEVERE, "retrieveArticles failure " + cat.getId(), e);
172 LOG.severe("No feed for category " + cat);
175 public List<Article> getArticles(Category cat)
176 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
177 synchronized (articlesByCategory) {
178 return articlesByCategory.get(cat);
182 private class Refresher implements Runnable {
183 private final Category category;
185 public Refresher(Category category) {
186 this.category = category;
191 List<Article> articles;
193 LOG.info("refresher "+ category.getId());
196 retrieveArticles(category);
198 synchronized (articlesByCategory) {
199 articles = articlesByCategory.get(category);
200 if (articles != null && articles.size() > 100) {
201 articlesByCategory.put(category,
202 articles.subList(0, 100));
206 } catch (IllegalArgumentException | FeedException | IOException e) {
207 LOG.log(Level.SEVERE, "refresher failure", e);
210 LOG.info("refresher "+ category.getId() + " done");