X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=war%2Fsrc%2Fmain%2Fjava%2Fpnews%2Fservlet%2FConfig.java;h=78db69450d15e10a2c5ae9ef1741cfa723ca32e1;hb=88a7ba9745b8318ca6c4f741906a40e3d6a8f07e;hp=5c0c1343a571557362027379e82759e801ef1922;hpb=27591c1933180a43423c78cc8cda28a3da1bfccf;p=pnews.git diff --git a/war/src/main/java/pnews/servlet/Config.java b/war/src/main/java/pnews/servlet/Config.java index 5c0c134..78db694 100644 --- a/war/src/main/java/pnews/servlet/Config.java +++ b/war/src/main/java/pnews/servlet/Config.java @@ -3,114 +3,254 @@ package pnews.servlet; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalUnit; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; +import javax.json.JsonString; +import javax.json.JsonValue; +import pnews.Article; import pnews.Category; import pnews.Feed; +import pnews.Language; public class Config { - public static Feed[] getFeeds() { + private Feed[] feeds; + private Category[] categories; + private Language[] languages; + private final Set blacklistedEntities = new HashSet<>(); + private final HashMap entityAliases = new HashMap<>(); + private static final String CLASS_NAME = Config.class.getName(); + + /** + * The key is the language, the value is the default category for this language. + */ + private Map defaultCategories = new HashMap<>(); + private static final Logger LOG = Logger.getLogger(CLASS_NAME); + + private void loadCategories(JsonArray jcats) { + List cats; + JsonObject jcat; + Category cat; + String id, label, title, language; + + cats = new ArrayList<>(jcats.size()); + + for (JsonValue v: jcats) { + jcat = (JsonObject)v; + id = jcat.getString("id"); + label = jcat.getString("label"); + title = jcat.getString("title"); + language = jcat.getString("language"); + cat = new Category(id, label, title, language); + cats.add(cat); + if (defaultCategories.get(language) == null) + defaultCategories.put(language, cat); + } + + categories = cats.toArray(new Category[0]); + } + + private void loadLanguages(JsonArray jlangs) { + List langs; + JsonObject jlang; + String id; + + langs = new ArrayList<>(jlangs.size()); + + for (JsonValue v: jlangs) { + jlang = (JsonObject)v; + id = jlang.getString("id"); + langs.add(new Language(id)); + } + + languages = langs.toArray(new Language[0]); + } + + private Category getCategory(String id) { + for (Category c: categories) + if (c.getId().equals(id)) + return c; + return null; + } + + private void loadEntities(JsonObject jroot) { + JsonObject jentities, jaliases; + JsonArray jblacklist; + final String METHOD_NAME = "loadEntities"; + + jentities = jroot.getJsonObject("entities"); + + jblacklist = jentities.getJsonArray("blacklist"); + jblacklist.forEach((jv)-> { + JsonString js; + + js = (JsonString)jv; + blacklistedEntities.add(js.getString()); + }); + + jaliases = jentities.getJsonObject("aliases"); + jaliases.forEach((k, v)-> { + JsonArray jsources = (JsonArray)v; + + jsources.forEach((jsource)-> { + entityAliases.put(((JsonString)jsource).getString(), k); + }); + }); + + LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " blacklistedEntities=" + blacklistedEntities); + LOG.logp(Level.FINEST, CLASS_NAME, METHOD_NAME, " entityAliases=" + entityAliases); + } + + public String getEntityAlias(String entity) { + String result; + + result = entityAliases.get(entity); + + if (result == null) + return entity; + else + return result; + } + + public void loadConfig() throws UnsupportedEncodingException { Reader r; - List feeds; - JsonObject jfeeds; + JsonObject jfeeds, jroot; + List feedList; r = null; try { - r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json")); - jfeeds = Json.createReader(r).readObject(); + r = new InputStreamReader(Config.class.getClassLoader().getResourceAsStream("feeds.json"), + "UTF-8"); + jroot = Json.createReader(r).readObject(); } finally { if (r != null) try { r.close(); } catch (IOException e) { }; } - feeds = new ArrayList(jfeeds.size()); + loadLanguages(jroot.getJsonArray("languages")); + loadCategories(jroot.getJsonArray("categories")); + + jfeeds = jroot.getJsonObject("feeds"); + + feedList = new ArrayList(jfeeds.size()); jfeeds.forEach((k, v)-> { JsonObject jf; + String str; + Category cat; JsonArray jcategories; jf = (JsonObject)v; jcategories = jf.getJsonArray("categories"); - feeds.add(new Feed(k, Category.valueOf(jcategories.getString(0)))); - }); + str = jcategories.getString(0); + + cat = getCategory(str); - return feeds.toArray(new Feed[] {}); + if (cat != null) + feedList.add(new Feed(k, cat)); + else + LOG.severe("Missing category: " + str); + }); + + feeds = feedList.toArray(new Feed[0]); + + loadEntities(jroot); } - public static Map getFeedsByCategory() { - Map result; + public boolean isBlacklistedEntity(String e) { + final String METHOD_NAME = "isBlacklistedEntity"; + boolean result; - result = new HashMap<>(); + LOG.entering(CLASS_NAME, METHOD_NAME, e); - result.put(Category.ACTUALITE, - new String[] { - "http://www.europe1.fr/var/export/rss/europe1/actus.xml", - "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.europe1.fr/var/export/rss/europe1/sport.xml", - "http://www.sportune.fr/feed", - "http://www.france24.com/fr/sports/rss" }); - - result.put(Category.FRANCE, - new String[] { "http://www.france24.com/fr/france/rss", - "http://www.francetvinfo.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.europe1.fr/var/export/rss/europe1/international.xml", - "http://www.france24.com/fr/actualites/rss" }); - - - result.put(Category.ECONOMIE, - new String[] { "http://www.france24.com/fr/economie/rss", - "http://www.europe1.fr/var/export/rss/europe1/economie.xml", - "http://www.rfi.fr/economie/rss" }); + result = blacklistedEntities.contains(e); - result.put(Category.ESSONNE, - new String[] { "http://www.tourisme-essonne.com/rss/actus/", - "http://www.ville-palaiseau.fr/rss/actualites.htm" }); + LOG.exiting(CLASS_NAME, METHOD_NAME, result); - result.put(Category.PEOPLE, - new String[] { "http://www.premiere.fr/rss/actu-live", - "http://www.purepeople.com/rss/news_t0.xml" - }); + return result; + } + + public boolean isObsolete(Instant instant) { + Instant olderInstant; - result.put(Category.TECHNOLOGIE, - new String[] { "http://www.generation-nt.com/export/rss.xml", - "http://www.europe1.fr/var/export/rss/europe1/sciences.xml", - "http://feeds.feedburner.com/lesnumeriques/news", - "http://www.zdnet.fr/feeds/rss/actualites/", - "http://www.frandroid.com/feed", - "http://www.silicon.fr/feed", - "http://www.fredzone.org/feed", - "http://www.futura-sciences.com/rss/actualites.xml", - "https://www-03.ibm.com/press/fr/fr/rssfeed.wss?keyword=null&maxFeed=&feedType=RSS&topic=all"}); + olderInstant = Instant.now().minus(60, ChronoUnit.DAYS); - return result; + if (instant.isAfter(olderInstant)) + return false; + else + return true; } - public static void main(String[] args) { + public Feed[] getFeeds() { + return feeds; + } + + public Map> getFeedsByCategory() { + Map> result; Feed[] feeds; + List catFeeds; + Category cat; + + result = new HashMap<>(); feeds = getFeeds(); + for (Feed f: feeds) { + cat = f.getCategory(); + + catFeeds = result.get(cat); + if (catFeeds == null) { + catFeeds = new ArrayList(); + result.put(cat, catFeeds); + } + catFeeds.add(f); + } + + return result; + } + + public Category[] getCategories() { + return categories; + } + + public Category getDefaultCategory(Language lang) { + return defaultCategories.get(lang.getId()); + } + + public Language[] getLanguages() { + return languages; + } + + public Language getDefaultLanguage() { + return languages[0]; + } + + public static void main(String[] args) throws UnsupportedEncodingException { + Config cfg; + Feed[] feeds; + Category[] cats; + + cfg = new Config(); + cfg.loadConfig(); + + cats = cfg.getCategories(); + for (Category cat: cats) + System.out.println(cat); + + feeds = cfg.getFeeds(); System.out.println("Number of feeds: " + feeds.length); for (Feed f: feeds)