X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FArticleProvider.java;h=ba96fe39856c1aff444dc4dc2aabe0c9be5e5f8a;hb=8372537c3503ab0ce4681fac13ccd65023fbcd84;hp=4e85e83968681eb7670cae8af21618ded4d5cd7a;hpb=52bcbff162b9fc88606d2e4e5195657f5040b261;p=pnews.git diff --git a/war/src/main/java/pnews/servlet/ArticleProvider.java b/war/src/main/java/pnews/servlet/ArticleProvider.java index 4e85e83..ba96fe3 100644 --- a/war/src/main/java/pnews/servlet/ArticleProvider.java +++ b/war/src/main/java/pnews/servlet/ArticleProvider.java @@ -3,6 +3,8 @@ package pnews.servlet; import java.io.IOException; 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; @@ -74,7 +76,20 @@ public class ArticleProvider { return false; } - private static Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang) { + private Instant getArticleInstant(SyndEntry entry) { + Date date; + + date = entry.getUpdatedDate(); + if (date == null) + date = entry.getPublishedDate(); + + if (date == null) + return Instant.now(); + + return date.toInstant(); + } + + private Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang, Instant instant) { String desc, title, thumbnail, feedTitle, str; Date date; List entities; @@ -100,24 +115,17 @@ public class ArticleProvider { desc = null; LOG.severe("No description for " + feedTitle + " - " + title); } - - date = entry.getPublishedDate(); - if (date == null) - date = entry.getUpdatedDate(); - if (date == null) - LOG.severe("The article " + feedTitle + " - " + title + " does not have a date"); - - + entities = new ArrayList<>(); if (desc != null && lang.equals("en")) try { - NER.classify(title, entities); - NER.classify(desc, entities); + NER.classify(title, entities, config); + NER.classify(desc, entities, config); } catch (ClassCastException | ClassNotFoundException | IOException e1) { LOG.log(Level.SEVERE, "Cannot classify " + feedTitle, e1); } - return new Article(link, title, desc, thumbnail, date, feedTitle, entities.toArray(new String[0])); + return new Article(link, title, desc, thumbnail, instant, feedTitle, entities.toArray(new String[0])); } private void addArticles(Category cat, SyndFeed feed) { @@ -137,7 +145,12 @@ public class ArticleProvider { continue ; } - a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage())); + final Instant instant = getArticleInstant(entry); + + if (config.isObsolete(instant)) + continue ; + + a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage(), instant)); synchronized (articles) { articles.add(a); @@ -181,16 +194,24 @@ public class ArticleProvider { /** * Returns a copy. */ - public List
getArticles(Category cat) + public List
getArticles(Category cat, String entity) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { - List
articles; + List
articles, result; synchronized (articlesByCategory) { articles = getArticlesForUpdate(cat); } - synchronized (articles) { - return new ArrayList<>(articles); + 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; } } @@ -200,14 +221,17 @@ public class ArticleProvider { final String FUNCTION_NAME = "getEntities"; EntityStat s; List stats; + Instant minInstant; LOG.entering(CLASS_NAME, FUNCTION_NAME, cat); - articles = getArticles(cat); + articles = getArticles(cat, null); + + minInstant = Instant.now().minus(15, ChronoUnit.DAYS); entities = new HashMap<>(); - for (Article a: articles) - if (a.getEntities() != null) { + for (Article a: articles) + if (a.getPublicationDate().isAfter(minInstant) && a.getEntities() != null) for (String e: a.getEntities()) { s = entities.get(e); if (s == null) { @@ -216,8 +240,7 @@ public class ArticleProvider { } s.increment(); } - } - + stats = new ArrayList<>(entities.values()); stats.sort(new Comparator() {