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 (lang.equals("en"))
122 NER.classify(title, entities, config);
124 NER.classify(desc, entities, config);
125 } catch (ClassCastException | ClassNotFoundException | IOException e1) {
126 LOG.log(Level.SEVERE, "Cannot classify " + feedTitle, e1);
129 return new Article(link, title, desc, thumbnail, instant, feedTitle, entities.toArray(new String[0]));
132 private void addArticles(Category cat, SyndFeed feed) {
134 List<Article> articles;
137 feedTitle = feed.getTitle().trim();
139 LOG.info("addArticles " + cat.getLabel() + " " + feedTitle + " number of articles: " + feed.getEntries().size());
141 for (SyndEntry entry: feed.getEntries()) {
142 String link = entry.getLink().trim();
143 articles = getArticlesForUpdate(cat);
144 if (exists(link, articles)) {
145 LOG.fine("addArticles " + link + " is already present");
149 final Instant instant = getArticleInstant(entry);
151 if (config.isObsolete(instant))
154 a = ArticleStore.singleton.getArticle(link, ()->toArticle(link, entry, feed, cat.getLanguage(), instant));
156 synchronized (articles) {
159 Collections.sort(articles, new Comparator<Article>() {
161 public int compare(Article o1, Article o2) {
162 if (o1.publicationDate == o2.publicationDate)
164 if (o1.publicationDate == null)
166 if (o2.publicationDate == null)
168 return o2.publicationDate.compareTo(o1.publicationDate);
174 LOG.info("addArticles done " + cat.getLabel());
177 private void retrieveArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
180 feeds = config.getFeedsByCategory().get(cat);
185 addArticles(cat, getSyndFeed(f.getURL()));
186 } catch (Throwable e) {
187 LOG.log(Level.SEVERE,
188 "retrieveArticles failure " + cat.getLabel() + " " + f.toString(),
192 LOG.severe("No feed for category " + cat);
198 public List<Article> getArticles(Category cat, String entity)
199 throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
200 List<Article> articles, result;
202 synchronized (articlesByCategory) {
203 articles = getArticlesForUpdate(cat);
206 synchronized (articles) {
208 return new ArrayList<>(articles);
210 result = new ArrayList<>(articles.size());
211 for (Article a: articles)
212 if (a.hasEntity(entity))
219 public List<EntityStat> getEntityStats(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
220 List<Article> articles;
221 Map<String, EntityStat> entities;
222 final String FUNCTION_NAME = "getEntities";
224 List<EntityStat> stats;
227 LOG.entering(CLASS_NAME, FUNCTION_NAME, cat);
229 articles = getArticles(cat, null);
231 minInstant = Instant.now().minus(15, ChronoUnit.DAYS);
233 entities = new HashMap<>();
234 for (Article a: articles)
235 if (a.getPublicationDate().isAfter(minInstant) && a.getEntities() != null)
236 for (String e: a.getEntities()) {
239 s = new EntityStat(e);
245 stats = new ArrayList<>(entities.values());
246 stats.sort(new Comparator<EntityStat>() {
249 public int compare(EntityStat o1, EntityStat o2) {
250 return Integer.compare(o2.getCount(), o1.getCount());
255 LOG.exiting(CLASS_NAME, FUNCTION_NAME, stats);
260 private class Refresher implements Runnable {
261 private final Category category;
263 public Refresher(Category category) {
264 this.category = category;
269 LOG.info("refresher "+ category.getLabel());
272 retrieveArticles(category);
273 } catch (IllegalArgumentException | FeedException | IOException e) {
274 LOG.log(Level.SEVERE, "refresher failure", e);
277 LOG.info("refresher "+ category.getLabel() + " done");