+package pnews.servlet;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import org.jsoup.Jsoup;
+
+import com.rometools.rome.feed.synd.SyndEnclosure;
+import com.rometools.rome.feed.synd.SyndEntry;
+import com.rometools.rome.feed.synd.SyndFeed;
+import com.rometools.rome.io.FeedException;
+import com.rometools.rome.io.SyndFeedInput;
+import com.rometools.rome.io.XmlReader;
+
+import pnews.Article;
+import pnews.Category;
+
+public class ArticleProvider {
+ public final static ArticleProvider singleton = new ArticleProvider();
+ private static final Logger LOG = Logger.getLogger(ArticleProvider.class.getName());
+ private final Map<Category, List<Article>> articlesByCategory = new HashMap<>();
+ private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
+
+ private ArticleProvider() {
+ for (Category cat:Category.values())
+ scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 120, TimeUnit.SECONDS);
+ }
+
+ private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
+ try (XmlReader reader = new XmlReader(new URL(u))) {
+ return new SyndFeedInput().build(reader);
+ }
+ }
+
+ private static Map<Category, String[]> getFeeds() {
+ Map<Category, String[]> result;
+
+ result = new HashMap<>();
+
+ result.put(Category.TOP,
+ new String[] {
+ "http://www.francetvinfo.fr/titres.rss",
+ "http://www.france24.com/fr/actualites/rss",
+ //"https://www.franceinter.fr/rss/a-la-une.xml",
+ "http://www.rfi.fr/general/rss",
+ "http://www.cnews.fr/rss/une",
+ "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
+ });
+
+ result.put(Category.SPORT,
+ new String[] { "http://www.france24.com/fr/sports/rss" });
+
+ result.put(Category.FRANCE,
+ new String[] { "http://www.france24.com/fr/france/rss",
+ "http://www.rfi.fr/france/rss"});
+
+ result.put(Category.EUROPE,
+ new String[] { "http://www.france24.com/fr/europe/rss" });
+
+ result.put(Category.ECO,
+ new String[] { "http://www.france24.com/fr/economie/rss",
+ "http://www.rfi.fr/economie/rss" });
+
+ result.put(Category.ESSONNE,
+ new String[] { /*"https://www.essonneinfo.fr/feed/"*/ });
+
+ result.put(Category.TECHNOLOGIE,
+ new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
+ "http://www.zdnet.fr/feeds/rss/actualites/"});
+
+ return result;
+ }
+
+ private void addArticles(Category cat, SyndFeed feed) {
+ String thumbnail;
+ String desc;
+ Date date;
+ List<Article> articles;
+
+ LOG.info("addArticles" + cat.getId());
+
+ for (SyndEntry entry: feed.getEntries()) {
+ thumbnail = null;
+ for (SyndEnclosure e: entry.getEnclosures()) {
+ if (e.getType().startsWith("image/"))
+ thumbnail = e.getUrl();
+ break;
+ }
+
+ if (entry.getDescription() != null) {
+ desc = Jsoup.parse(entry.getDescription().getValue()).text();
+ } else {
+ desc = null;
+ LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
+ }
+
+ date = entry.getPublishedDate();
+ if (date == null)
+ date = entry.getUpdatedDate();
+
+ synchronized(articlesByCategory) {
+ articles = articlesByCategory.get(cat);
+ if (articles == null) {
+ articles = new ArrayList<>();
+ articlesByCategory.put(cat, articles);
+ } else {
+ for (Article a: articles)
+ if (a.link.equals(entry.getLink()))
+ return ;
+ }
+
+ articles.add(new Article(entry.getLink(),
+ cat,
+ entry.getTitle(),
+ desc,
+ thumbnail,
+ date,
+ feed.getTitle()));
+
+ articles.sort(new Comparator<Article>() {
+ @Override
+ public int compare(Article o1, Article o2) {
+ return o2.publicationDate.compareTo(o1.publicationDate);
+ }
+
+ });
+ }
+ }
+ }
+
+ private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+ String[] feeds;
+
+ feeds = getFeeds().get(cat);
+
+ if (feeds != null)
+ for (String str: feeds)
+ addArticles(cat, getSyndFeed(str));
+ else
+ LOG.severe("No feed for category " + cat);
+ }
+
+ public List<Article> getArticles(Category cat)
+ throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+ synchronized (articlesByCategory) {
+ return articlesByCategory.get(cat);
+ }
+ }
+
+ private class Refresher implements Runnable {
+ private final Category category;
+
+ public Refresher(Category category) {
+ this.category = category;
+ }
+
+ @Override
+ public void run() {
+ List<Article> articles;
+
+ LOG.info("refresher "+ category.getId());
+
+ try {
+ retrieveArticles(category);
+
+ synchronized (articlesByCategory) {
+ articles = articlesByCategory.get(category);
+ if (articles != null && articles.size() > 100) {
+ articlesByCategory.put(category,
+ articles.subList(0, 100));
+
+ }
+ }
+ } catch (IllegalArgumentException | FeedException | IOException e) {
+ LOG.log(Level.SEVERE, "refresher failure", e);
+ }
+
+ LOG.info("refresher "+ category.getId() + " done");
+ }
+ }
+}