X-Git-Url: https://git.wpitchoune.net/gitweb/?p=pnews.git;a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FArticleProvider.java;h=55898a479a898713e94a3b57bc261fc14b5e89bd;hp=ddec874a0863cb56646817dcd3e8edbc8166f7ce;hb=aff83c8798602b535d13edeaffdb8f4238e2bbf5;hpb=9c7a682f3e891b86fb1c14a881d360dcf65e7d47 diff --git a/war/src/main/java/pnews/servlet/ArticleProvider.java b/war/src/main/java/pnews/servlet/ArticleProvider.java index ddec874..55898a4 100644 --- a/war/src/main/java/pnews/servlet/ArticleProvider.java +++ b/war/src/main/java/pnews/servlet/ArticleProvider.java @@ -1,9 +1,10 @@ package pnews.servlet; import java.io.IOException; -import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; +import java.time.Instant; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -18,183 +19,245 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jsoup.Jsoup; -import org.xml.sax.InputSource; 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; +import net.wpitchoune.pnews.Article; +import net.wpitchoune.pnews.ArticleStore; +import net.wpitchoune.pnews.Category; +import net.wpitchoune.pnews.Config; +import net.wpitchoune.pnews.EntityStat; +import net.wpitchoune.pnews.Feed; +import net.wpitchoune.pnews.classifier.NamedEntityRecognizer; public class ArticleProvider { - public final static ArticleProvider singleton = new ArticleProvider(); - private static final Logger LOG = Logger.getLogger(ArticleProvider.class.getName()); + private static final String CLASS_NAME = ArticleProvider.class.getName(); + private static final Logger LOG = Logger.getLogger(CLASS_NAME); private final Map> articlesByCategory = new HashMap<>(); - private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors()); + private final Config config; - private ArticleProvider() { - for (Category cat:Category.values()) + public ArticleProvider(Config config) { + this.config = config; + for (Category cat: config.getCategories()) scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 600, TimeUnit.SECONDS); } private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException { - InputStream is = new URL(u).openConnection().getInputStream(); - InputSource source = new InputSource(is); - - return new SyndFeedInput().build(source); + XmlReader r; + + r = new XmlReader(new URL(u)); + return new SyndFeedInput().build(r); } - private static Map getFeeds() { - Map result; - - result = new HashMap<>(); - - result.put(Category.TOP, - new String[] { - "http://www.francetvinfo.fr/titres.rss", - "http://www.france24.com/fr/actualites/rss", - "http://www.rfi.fr/general/rss", - "http://www.cnews.fr/rss/une", - "http://www.ladepeche.fr/rss/a-la-une.rss", - "https://www.franceinter.fr/rss/a-la-une.xml", - "https://www.francebleu.fr/rss/a-la-une.xml", - "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[] { "http://www.tourisme-essonne.com/rss/actus/", - "http://www.ville-palaiseau.fr/rss/actualites.htm" - /*"https://www.essonneinfo.fr/feed/"*/ }); - - result.put(Category.PEOPLE, - new String[] { "http://www.premiere.fr/rss/actu-live", - "http://www.purepeople.com/rss/news_t0.xml" - }); + private List
getArticlesForUpdate(Category cat) { + List
result; + + synchronized (articlesByCategory) { + result = articlesByCategory.get(cat); + if (result == null) { + result = new ArrayList<>(); + articlesByCategory.put(cat, result); + } + return result; + } + } + + private boolean exists(String articleLink, List
articles) { + synchronized (articles) { + for (Article a: articles) + if (a.getLink().equals(articleLink)) + return true; + } + return false; + } + + private Instant getArticleInstant(SyndEntry entry) { + Date date; + + date = entry.getUpdatedDate(); + if (date == null) + date = entry.getPublishedDate(); + + if (date == null) + return Instant.now(); - result.put(Category.TECHNOLOGIE, - new String[] { "http://feeds.feedburner.com/lesnumeriques/news", - "http://www.zdnet.fr/feeds/rss/actualites/"}); + return date.toInstant(); + } + + private Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang, Instant instant) { + String desc, title, thumbnail, feedTitle, str; + List entities; + + feedTitle = feed.getTitle(); + if (feedTitle != null) { + feedTitle = feedTitle.trim(); + } + + thumbnail = null; + for (SyndEnclosure e: entry.getEnclosures()) { + if (e.getType().startsWith("image/")) + thumbnail = e.getUrl(); + break; + } + + title = entry.getTitle().trim(); + + if (entry.getDescription() != null) { + str = entry.getDescription().getValue(); + desc = Jsoup.parse(str).text(); + } else { + desc = null; + LOG.severe("No description for " + feedTitle + " - " + title); + } + + entities = new ArrayList<>(); + if (lang.equals("en")) + try { + NamedEntityRecognizer.classify(title, entities, config); + if (desc != null) + NamedEntityRecognizer.classify(desc, entities, config); + } catch (ClassCastException | ClassNotFoundException | IOException e1) { + LOG.log(Level.SEVERE, "Cannot classify " + feedTitle, e1); + } - return result; + return new Article(link, title, desc, thumbnail, instant, feedTitle, entities.toArray(new String[0])); } private void addArticles(Category cat, SyndFeed feed) { - String thumbnail; - String desc, link, title, feedTitle; - Date date; + String feedTitle; List
articles; - boolean exist; + Article a; feedTitle = feed.getTitle().trim(); - LOG.info("addArticles " + cat.getId() + " " + feedTitle + " number of articles: " + feed.getEntries().size()); + LOG.info("addArticles " + cat.getLabel() + " " + feedTitle + " number of articles: " + feed.getEntries().size()); for (SyndEntry entry: feed.getEntries()) { - thumbnail = null; - for (SyndEnclosure e: entry.getEnclosures()) { - if (e.getType().startsWith("image/")) - thumbnail = e.getUrl(); - break; + String link = entry.getLink().trim(); + articles = getArticlesForUpdate(cat); + if (exists(link, articles)) { + LOG.fine("addArticles " + link + " is already present"); + continue ; } - - title = entry.getTitle().trim(); - if (entry.getDescription() != null) { - desc = Jsoup.parse(entry.getDescription().getValue()).text(); - } else { - desc = null; - LOG.severe("No description for " + feedTitle + " - " + title); - } + final Instant instant = getArticleInstant(entry); - date = entry.getPublishedDate(); - if (date == null) - date = entry.getUpdatedDate(); - if (date == null) { - LOG.severe("The article " + feedTitle + " - " + title + " does not have a date"); - continue; - } + if (config.isObsolete(instant)) + continue ; - synchronized(articlesByCategory) { - link = entry.getLink().trim(); - - articles = articlesByCategory.get(cat); - exist = false; - if (articles == null) { - articles = new ArrayList<>(); - articlesByCategory.put(cat, articles); - } else { - for (Article a: articles) - if (a.link.equals(link)) { - LOG.info("addArticles " + link + " already present"); - exist = true; - } - } - - if (!exist) { - LOG.fine("add " + cat.getId() + " " + feedTitle + " " + title); - - articles.add(new Article(link, cat, title, desc, thumbnail, date, - feed.getTitle())); - - Collections.sort(articles, new Comparator
() { - @Override - public int compare(Article o1, Article o2) { - return o2.publicationDate.compareTo(o1.publicationDate); - } + a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage(), instant)); + + synchronized (articles) { + articles.add(a); - }); - } else { - LOG.finest("addArticles already exist: " + title); - } + Collections.sort(articles, new Comparator
() { + @Override + public int compare(Article o1, Article o2) { + if (o1.getPublicationDate() == o2.getPublicationDate()) + return 0; + if (o1.getPublicationDate() == null) + return 1; + if (o2.getPublicationDate() == null) + return -1; + return o2.getPublicationDate().compareTo(o1.getPublicationDate()); + } + }); } } - LOG.info("addArticles done " + cat.getId()); + LOG.info("addArticles done " + cat.getLabel()); } - + private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { - String[] feeds; + List feeds; - feeds = getFeeds().get(cat); + feeds = config.getFeedsByCategory().get(cat); if (feeds != null) - for (String str: feeds) + for (Feed f: feeds) try { - addArticles(cat, getSyndFeed(str)); + addArticles(cat, getSyndFeed(f.getURL())); } catch (Throwable e) { LOG.log(Level.SEVERE, - "retrieveArticles failure " + cat.getId() + " " + str, + "retrieveArticles failure " + cat.getLabel() + " " + f.toString(), e); } else LOG.severe("No feed for category " + cat); } - public List
getArticles(Category cat) + /** + * Returns a copy. + */ + public List
getArticles(Category cat, String entity) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { + List
articles, result; + synchronized (articlesByCategory) { - return articlesByCategory.get(cat); + articles = getArticlesForUpdate(cat); + } + + synchronized (articles) { + if (entity == null) + return new ArrayList<>(articles); + + result = new ArrayList<>(articles.size()); + for (Article a: articles) + if (a.hasEntity(entity)) + result.add(a); + + return result; } } + public List getEntityStats(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { + List
articles; + Map entities; + final String FUNCTION_NAME = "getEntities"; + EntityStat s; + List stats; + Instant minInstant; + + LOG.entering(CLASS_NAME, FUNCTION_NAME, cat); + + articles = getArticles(cat, null); + + minInstant = Instant.now().minus(15, ChronoUnit.DAYS); + + entities = new HashMap<>(); + for (Article a: articles) + if (a.getPublicationDate().isAfter(minInstant) && a.getEntities() != null) + for (String e: a.getEntities()) { + s = entities.get(e); + if (s == null) { + s = new EntityStat(e); + entities.put(e, s); + } + s.increment(); + } + + stats = new ArrayList<>(entities.values()); + stats.sort(new Comparator() { + + @Override + public int compare(EntityStat o1, EntityStat o2) { + return Integer.compare(o2.getCount(), o1.getCount()); + } + + }); + + LOG.exiting(CLASS_NAME, FUNCTION_NAME, stats); + + return stats; + } + private class Refresher implements Runnable { private final Category category; @@ -203,28 +266,16 @@ public class ArticleProvider { } @Override - public void run() { - List
articles; - - LOG.info("refresher "+ category.getId()); + public void run() { + LOG.info("refresher "+ category.getLabel()); try { retrieveArticles(category); - - synchronized (articlesByCategory) { - articles = articlesByCategory.get(category); - if (articles != null && articles.size() > 100) { - articlesByCategory.put(category, - articles.subList(0, 100)); - - } - LOG.info("refresher " + category.getId() + " number of articles: " + articles.size()); - } } catch (IllegalArgumentException | FeedException | IOException e) { LOG.log(Level.SEVERE, "refresher failure", e); } - LOG.info("refresher "+ category.getId() + " done"); + LOG.info("refresher "+ category.getLabel() + " done"); } } }