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, 600, 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 "http://www.rfi.fr/general/rss",
61 "http://www.cnews.fr/rss/une",
62 "http://www.ladepeche.fr/rss/a-la-une.rss",
63 "https://www.franceinter.fr/rss/a-la-une.xml",
64 "https://www.francebleu.fr/rss/a-la-une.xml",
65 "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
68 result.put(Category.SPORT,
69 new String[] { "http://www.france24.com/fr/sports/rss" });
71 result.put(Category.FRANCE,
72 new String[] { "http://www.france24.com/fr/france/rss",
73 "http://www.rfi.fr/france/rss"});
75 result.put(Category.EUROPE,
76 new String[] { "http://www.france24.com/fr/europe/rss" });
78 result.put(Category.ECO,
79 new String[] { "http://www.france24.com/fr/economie/rss",
80 "http://www.rfi.fr/economie/rss" });
82 result.put(Category.ESSONNE,
83 new String[] { "http://www.tourisme-essonne.com/rss/actus/",
84 "http://www.ville-palaiseau.fr/rss/actualites.htm"
85 /*"https://www.essonneinfo.fr/feed/"*/ });
87 result.put(Category.PEOPLE,
88 new String[] { "http://www.premiere.fr/rss/actu-live",
89 "http://www.purepeople.com/rss/news_t0.xml"
92 result.put(Category.TECHNOLOGIE,
93 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
94 "http://www.zdnet.fr/feeds/rss/actualites/"});
99 private void addArticles(Category cat, SyndFeed feed) {
101 String desc, link, title, feedTitle, feedImage;
103 List<Article> articles;
106 feedTitle = feed.getTitle().trim();
108 if (feed.getImage() != null)
109 feedImage = feed.getImage().getUrl();
113 LOG.info("addArticles " + cat.getId() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
115 for (SyndEntry entry: feed.getEntries()) {
117 for (SyndEnclosure e: entry.getEnclosures()) {
118 if (e.getType().startsWith("image/"))
119 thumbnail = e.getUrl();
122 if (thumbnail == null)
123 thumbnail = feedImage;
125 title = entry.getTitle().trim();
127 if (entry.getDescription() != null) {
128 desc = Jsoup.parse(entry.getDescription().getValue()).text();
131 LOG.severe("No description for " + feedTitle + " - " + title);
134 date = entry.getPublishedDate();
136 date = entry.getUpdatedDate();
138 LOG.severe("The article " + feedTitle + " - " + title + " does not have a date");
142 synchronized(articlesByCategory) {
143 link = entry.getLink().trim();
145 articles = articlesByCategory.get(cat);
147 if (articles == null) {
148 articles = new ArrayList<>();
149 articlesByCategory.put(cat, articles);
151 for (Article a: articles)
152 if (a.link.equals(link)) {
153 LOG.fine("addArticles " + link + " is already present");
159 LOG.fine("add " + cat.getId() + " " + feedTitle + " " + title);
161 articles.add(new Article(link, cat, title, desc, thumbnail, date,
164 Collections.sort(articles, new Comparator<Article>() {
166 public int compare(Article o1, Article o2) {
167 return o2.publicationDate.compareTo(o1.publicationDate);
172 LOG.finest("addArticles already exist: " + title);
177 LOG.info("addArticles done " + cat.getId());
180 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
183 feeds = getFeeds().get(cat);
186 for (String str: feeds)
188 addArticles(cat, getSyndFeed(str));
189 } catch (Throwable e) {
190 LOG.log(Level.SEVERE,
191 "retrieveArticles failure " + cat.getId() + " " + str,
195 LOG.severe("No feed for category " + cat);
198 public List<Article> getArticles(Category cat)
199 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
200 synchronized (articlesByCategory) {
201 return articlesByCategory.get(cat);
205 private class Refresher implements Runnable {
206 private final Category category;
208 public Refresher(Category category) {
209 this.category = category;
214 List<Article> articles;
216 LOG.info("refresher "+ category.getId());
219 retrieveArticles(category);
221 synchronized (articlesByCategory) {
222 articles = articlesByCategory.get(category);
223 if (articles != null && articles.size() > 100) {
224 articlesByCategory.put(category,
225 articles.subList(0, 100));
228 LOG.info("refresher " + category.getId() + " number of articles: " + articles.size());
230 } catch (IllegalArgumentException | FeedException | IOException e) {
231 LOG.log(Level.SEVERE, "refresher failure", e);
234 LOG.info("refresher "+ category.getId() + " done");