3 import java.io.IOException;
4 import java.net.MalformedURLException;
6 import java.time.Instant;
7 import java.time.temporal.ChronoUnit;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.Date;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
21 import org.jsoup.Jsoup;
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;
28 import com.rometools.rome.io.XmlReader;
31 import pnews.Category;
32 import pnews.EntityStat;
36 public class ArticleProvider {
37 private static final String CLASS_NAME = ArticleProvider.class.getName();
38 private static final Logger LOG = Logger.getLogger(CLASS_NAME);
39 private final Map<Category, List<Article>> articlesByCategory = new HashMap<>();
40 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors());
41 private final Config config;
43 public ArticleProvider(Config config) {
45 for (Category cat: config.getCategories())
46 scheduler.scheduleAtFixedRate(new Refresher(cat), 2, 600, TimeUnit.SECONDS);
49 private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
52 r = new XmlReader(new URL(u));
54 return new SyndFeedInput().build(r);
57 private List<Article> getArticlesForUpdate(Category cat) {
60 synchronized (articlesByCategory) {
61 result = articlesByCategory.get(cat);
63 result = new ArrayList<>();
64 articlesByCategory.put(cat, result);
70 private boolean exists(String articleLink, List<Article> articles) {
71 synchronized (articles) {
72 for (Article a: articles)
73 if (a.link.equals(articleLink))
79 private Instant getArticleInstant(SyndEntry entry) {
82 date = entry.getUpdatedDate();
84 date = entry.getPublishedDate();
89 return date.toInstant();
92 private Article toArticle(String link, SyndEntry entry, SyndFeed feed, String lang, Instant instant) {
93 String desc, title, thumbnail, feedTitle, str;
95 List<String> entities;
97 feedTitle = feed.getTitle();
98 if (feedTitle != null) {
99 feedTitle = feedTitle.trim();
103 for (SyndEnclosure e: entry.getEnclosures()) {
104 if (e.getType().startsWith("image/"))
105 thumbnail = e.getUrl();
109 title = entry.getTitle().trim();
111 if (entry.getDescription() != null) {
112 str = entry.getDescription().getValue();
113 desc = Jsoup.parse(str).text();
116 LOG.severe("No description for " + feedTitle + " - " + title);
119 entities = new ArrayList<>();
120 if (desc != null && lang.equals("en"))
122 NER.classify(title, entities, config);
123 NER.classify(desc, entities, config);
124 } catch (ClassCastException | ClassNotFoundException | IOException e1) {
125 LOG.log(Level.SEVERE, "Cannot classify " + feedTitle, e1);
128 return new Article(link, title, desc, thumbnail, instant, feedTitle, entities.toArray(new String[0]));
131 private void addArticles(Category cat, SyndFeed feed) {
133 List<Article> articles;
136 feedTitle = feed.getTitle().trim();
138 LOG.info("addArticles " + cat.getLabel() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
140 for (SyndEntry entry: feed.getEntries()) {
141 String link = entry.getLink().trim();
142 articles = getArticlesForUpdate(cat);
143 if (exists(link, articles)) {
144 LOG.fine("addArticles " + link + " is already present");
148 final Instant instant = getArticleInstant(entry);
150 if (config.isObsolete(instant))
153 a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage(), instant));
155 synchronized (articles) {
158 Collections.sort(articles, new Comparator<Article>() {
160 public int compare(Article o1, Article o2) {
161 if (o1.publicationDate == o2.publicationDate)
163 if (o1.publicationDate == null)
165 if (o2.publicationDate == null)
167 return o2.publicationDate.compareTo(o1.publicationDate);
173 LOG.info("addArticles done " + cat.getLabel());
176 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
179 feeds = config.getFeedsByCategory().get(cat);
184 addArticles(cat, getSyndFeed(f.getURL()));
185 } catch (Throwable e) {
186 LOG.log(Level.SEVERE,
187 "retrieveArticles failure " + cat.getLabel() + " " + f.toString(),
191 LOG.severe("No feed for category " + cat);
197 public List<Article> getArticles(Category cat, String entity)
198 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
199 List<Article> articles, result;
201 synchronized (articlesByCategory) {
202 articles = getArticlesForUpdate(cat);
205 synchronized (articles) {
207 return new ArrayList<>(articles);
209 result = new ArrayList<>(articles.size());
210 for (Article a: articles)
211 if (a.hasEntity(entity))
218 public List<EntityStat> getEntityStats(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
219 List<Article> articles;
220 Map<String, EntityStat> entities;
221 final String FUNCTION_NAME = "getEntities";
223 List<EntityStat> stats;
226 LOG.entering(CLASS_NAME, FUNCTION_NAME, cat);
228 articles = getArticles(cat, null);
230 minInstant = Instant.now().minus(15, ChronoUnit.DAYS);
232 entities = new HashMap<>();
233 for (Article a: articles)
234 if (a.getPublicationDate().isAfter(minInstant) && a.getEntities() != null)
235 for (String e: a.getEntities()) {
238 s = new EntityStat(e);
244 stats = new ArrayList<>(entities.values());
245 stats.sort(new Comparator<EntityStat>() {
248 public int compare(EntityStat o1, EntityStat o2) {
249 return Integer.compare(o2.getCount(), o1.getCount());
254 LOG.exiting(CLASS_NAME, FUNCTION_NAME, stats);
259 private class Refresher implements Runnable {
260 private final Category category;
262 public Refresher(Category category) {
263 this.category = category;
268 LOG.info("refresher "+ category.getLabel());
271 retrieveArticles(category);
272 } catch (IllegalArgumentException | FeedException | IOException e) {
273 LOG.log(Level.SEVERE, "refresher failure", e);
276 LOG.info("refresher "+ category.getLabel() + " done");