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;
28 import com.rometools.rome.io.XmlReader;
31 import pnews.Category;
33 public class ArticleProvider {
34 public final static ArticleProvider singleton = new ArticleProvider();
35 private static final Logger LOG = Logger.getLogger(ArticleProvider.class.getName());
36 private final Map<Category, List<Article>> articlesByCategory = new HashMap<>();
37 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
39 private ArticleProvider() {
40 for (Category cat:Category.values())
41 scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 120, TimeUnit.SECONDS);
44 private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
45 InputStream is = new URL(u).openConnection().getInputStream();
46 InputSource source = new InputSource(is);
48 return new SyndFeedInput().build(source);
52 private static Map<Category, String[]> getFeeds() {
53 Map<Category, String[]> result;
55 result = new HashMap<>();
57 result.put(Category.TOP,
59 "http://www.francetvinfo.fr/titres.rss",
60 "http://www.france24.com/fr/actualites/rss",
61 //"https://www.franceinter.fr/rss/a-la-une.xml",
62 "http://www.rfi.fr/general/rss",
63 "http://www.cnews.fr/rss/une",
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.ECO,
78 new String[] { "http://www.france24.com/fr/economie/rss",
79 "http://www.rfi.fr/economie/rss" });
81 result.put(Category.ESSONNE,
82 new String[] { "http://www.tourisme-essonne.com/rss/actus/",
83 "http://www.ville-palaiseau.fr/rss/actualites.htm"
84 /*"https://www.essonneinfo.fr/feed/"*/ });
86 result.put(Category.PEOPLE,
87 new String[] { "http://www.premiere.fr/rss/actu-live",
88 "http://www.purepeople.com/rss/news_t0.xml"
91 result.put(Category.TECHNOLOGIE,
92 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
93 "http://www.zdnet.fr/feeds/rss/actualites/"});
98 private void addArticles(Category cat, SyndFeed feed) {
100 String desc, link, title;
102 List<Article> articles;
105 LOG.info("addArticles" + cat.getId());
107 for (SyndEntry entry: feed.getEntries()) {
109 for (SyndEnclosure e: entry.getEnclosures()) {
110 if (e.getType().startsWith("image/"))
111 thumbnail = e.getUrl();
115 if (entry.getDescription() != null) {
116 desc = Jsoup.parse(entry.getDescription().getValue()).text();
119 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
122 date = entry.getPublishedDate();
124 date = entry.getUpdatedDate();
126 synchronized(articlesByCategory) {
127 link = entry.getLink().trim();
128 title = entry.getTitle().trim();
129 articles = articlesByCategory.get(cat);
131 if (articles == null) {
132 articles = new ArrayList<>();
133 articlesByCategory.put(cat, articles);
135 for (Article a: articles)
136 if (a.link.equals(link)) {
137 LOG.info(link + " already present");
143 LOG.info("add " + cat.getId() + " " + feed.getTitle() + " " + title);
145 articles.add(new Article(link, cat, title, desc, thumbnail, date,
148 Collections.sort(articles, new Comparator<Article>() {
150 public int compare(Article o1, Article o2) {
151 return o2.publicationDate.compareTo(o1.publicationDate);
160 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
163 feeds = getFeeds().get(cat);
166 for (String str: feeds)
168 addArticles(cat, getSyndFeed(str));
169 } catch (IOException e) {
170 LOG.log(Level.SEVERE, "retrieveArticles failure " + cat.getId(), e);
173 LOG.severe("No feed for category " + cat);
176 public List<Article> getArticles(Category cat)
177 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
178 synchronized (articlesByCategory) {
179 return articlesByCategory.get(cat);
183 private class Refresher implements Runnable {
184 private final Category category;
186 public Refresher(Category category) {
187 this.category = category;
192 List<Article> articles;
194 LOG.info("refresher "+ category.getId());
197 retrieveArticles(category);
199 synchronized (articlesByCategory) {
200 articles = articlesByCategory.get(category);
201 if (articles != null && articles.size() > 100) {
202 articlesByCategory.put(category,
203 articles.subList(0, 100));
207 } catch (IllegalArgumentException | FeedException | IOException e) {
208 LOG.log(Level.SEVERE, "refresher failure", e);
211 LOG.info("refresher "+ category.getId() + " done");