import pnews.Article;
import pnews.Category;
+import pnews.EntityStat;
+import pnews.Feed;
+import pnews.NER;
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<Category, List<Article>> 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);
}
XmlReader r;
r = new XmlReader(new URL(u));
- XmlReader.setDefaultEncoding("UTF-8");
return new SyndFeedInput().build(r);
}
- 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.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.MONDE,
- new String[] { "http://www.france24.com/fr/actualites/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" });
-
- result.put(Category.PEOPLE,
- new String[] { "http://www.premiere.fr/rss/actu-live",
- "http://www.purepeople.com/rss/news_t0.xml"
- });
-
- result.put(Category.TECHNOLOGIE,
- new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
- "http://www.zdnet.fr/feeds/rss/actualites/"});
-
- return result;
- }
-
private List<Article> getArticlesForUpdate(Category cat) {
List<Article> result;
return false;
}
- private static Article toArticle(String link, SyndEntry entry, SyndFeed feed) {
- String desc, title, thumbnail;
+ private static Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang) {
+ String desc, title, thumbnail, feedTitle, str;
Date date;
+ List<String> entities;
+
+ feedTitle = feed.getTitle();
+ if (feedTitle != null) {
+ feedTitle = feedTitle.trim();
+ }
thumbnail = null;
for (SyndEnclosure e: entry.getEnclosures()) {
thumbnail = e.getUrl();
break;
}
-
- if (thumbnail == null && feed.getImage() != null)
- thumbnail = feed.getImage().getUrl();
-
+
title = entry.getTitle().trim();
- if (entry.getDescription() != null) {
- desc = Jsoup.parse(entry.getDescription().getValue()).text();
+ if (entry.getDescription() != null) {
+ str = entry.getDescription().getValue();
+ desc = Jsoup.parse(str).text();
} else {
desc = null;
- LOG.severe("No description for " + feed.getTitle() + " - " + title);
+ LOG.severe("No description for " + feedTitle + " - " + title);
}
date = entry.getPublishedDate();
if (date == null)
date = entry.getUpdatedDate();
if (date == null)
- LOG.severe("The article " + feed.getTitle() + " - " + title + " does not have a date");
+ LOG.severe("The article " + feedTitle + " - " + title + " does not have a date");
- return new Article(link, title, desc, thumbnail, date, title);
+
+ entities = new ArrayList<>();
+ if (desc != null && lang.equals("en"))
+ try {
+ NER.classify(title, entities);
+ NER.classify(desc, entities);
+ } 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]));
}
private void addArticles(Category cat, SyndFeed feed) {
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()) {
String link = entry.getLink().trim();
continue ;
}
- a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed));
+ a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage()));
synchronized (articles) {
articles.add(a);
}
}
- 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<Feed> 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
/**
* Returns a copy.
*/
- public List<Article> getArticles(Category cat)
+ public List<Article> getArticles(Category cat, String entity)
throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
- List<Article> articles;
+ List<Article> 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;
}
}
+ public List<EntityStat> getEntityStats(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+ List<Article> articles;
+ Map<String, EntityStat> entities;
+ final String FUNCTION_NAME = "getEntities";
+ EntityStat s;
+ List<EntityStat> stats;
+
+ LOG.entering(CLASS_NAME, FUNCTION_NAME, cat);
+
+ articles = getArticles(cat, null);
+
+ entities = new HashMap<>();
+ for (Article a: articles)
+ if (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<EntityStat>() {
+
+ @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;
}
@Override
- public void run() {
- List<Article> 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");
}
}
}