X-Git-Url: http://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fpnews%2FMain.java;h=6086a74f17c08883600d540764959ef3a4620d14;hb=78b4da5fea99561e7707e6de5195ae1c693193ae;hp=fce2fd1788d42578c778e65c2988d6f2a5b59db6;hpb=f2312531df437dd4d753d2dc13fb7975184437cf;p=pnews.git diff --git a/src/main/java/pnews/Main.java b/src/main/java/pnews/Main.java index fce2fd1..6086a74 100644 --- a/src/main/java/pnews/Main.java +++ b/src/main/java/pnews/Main.java @@ -3,57 +3,200 @@ package pnews; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; import java.util.ArrayList; +import java.util.Comparator; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.logging.Logger; -import com.rometools.rome.feed.synd.SyndCategory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.jsoup.Jsoup; + +import com.rometools.rome.feed.synd.SyndEnclosure; +import com.rometools.rome.feed.synd.SyndEntry; import com.rometools.rome.feed.synd.SyndFeed; import com.rometools.rome.io.FeedException; import com.rometools.rome.io.SyndFeedInput; import com.rometools.rome.io.XmlReader; public class Main { - private static void println(SyndFeed feed) { - System.out.println(feed.getTitle()); - for (SyndCategory cat: feed.getCategories()) { - System.out.println(cat.getName()); - } - System.out.println(feed.getFeedType()); + private static final Logger LOG = Logger.getLogger(Main.class.getName()); + + static { + TrustManager[] mgrs; + SSLContext sc; + + mgrs = new TrustManager[]{ + new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + } + }; + + try { + sc = SSLContext.getInstance("SSL"); + + sc.init(null, mgrs, new java.security.SecureRandom()); + SSLContext.setDefault(sc); + } catch (NoSuchAlgorithmException | KeyManagementException e) { + e.printStackTrace(); + } + } + + private static void addArticles(Category cat, SyndFeed feed, List
articles) { + String thumbnail; + String desc; + Date date; + + for (SyndEntry entry: feed.getEntries()) { + thumbnail = null; + for (SyndEnclosure e: entry.getEnclosures()) { + if (e.getType().startsWith("image/")) + thumbnail = e.getUrl(); + break; + } + + if (entry.getDescription() != null) { + desc = Jsoup.parse(entry.getDescription().getValue()).text(); + } else { + desc = null; + LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle()); + } + + date = entry.getPublishedDate(); + if (date == null) + date = entry.getUpdatedDate(); + + articles.add(new Article(entry.getLink(), + cat, + entry.getTitle(), + desc, + thumbnail, + date, + feed.getTitle())); + } } - public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException { - URL url; - String html; - List feeds; - SyndFeed feed; - String[] urls = new String[] { - "http://www.france24.com/fr/france/rss", - "https://www.lesechos.fr/rss/rss_une_titres.xml" - }; - + private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException { + try (XmlReader reader = new XmlReader(new URL(u))) { + return new SyndFeedInput().build(reader); + } + } + + private static Map getFeeds() { + Map result; + + result = new HashMap<>(); + + result.put(Category.TOP, + new String[] { + "http://www.francetvinfo.fr/titres.rss", + "http://www.france24.com/fr/actualites/rss", + "https://www.franceinter.fr/rss/a-la-une.xml", + "http://www.rfi.fr/general/rss", + "http://www.cnews.fr/rss/une", + "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.ECO, + new String[] { "http://www.france24.com/fr/economie/rss", + "http://www.rfi.fr/economie/rss" }); + + result.put(Category.ESSONNE, + new String[] { "https://www.essonneinfo.fr/feed/" }); + + result.put(Category.TECHNOLOGIE, + new String[] { "http://feeds.feedburner.com/lesnumeriques/news", + "http://www.zdnet.fr/feeds/rss/actualites/"}); + + return result; + } + + private static List
getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { + List
articles; + String[] feeds; + Set links; + + articles = new ArrayList<>(); + + feeds = getFeeds().get(cat); + + if (feeds != null) + for (String str: feeds) + addArticles(cat, getSyndFeed(str), articles); + else + LOG.severe("No feed for category " + cat); + + links = new HashSet<>(articles.size()); + for (Article a: articles) { + if (links.contains(a.link)) + LOG.severe(a.link + "is not uniq"); + else + links.add(a.link); + } + + articles.sort(new Comparator
() { + @Override + public int compare(Article o1, Article o2) { + return o2.publicationDate.compareTo(o1.publicationDate); + } + }); + + return articles; + } + + private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException { + List
articles; + String html; + File f; + + articles = getArticles(cat); + + html = HTML.toHTML(articles, cat); + + f = new File(cat.getId() + ".html"); + + try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) { + writer.write(html); + } + } + + public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException { System.out.println("pnews"); - feeds = new ArrayList<>(urls.length); - for (String u: urls) { - url = new URL(u); - - try (XmlReader reader = new XmlReader(url)) { - feed = new SyndFeedInput().build(reader); - println(feed); - feeds.add(feed); - }; - } - - - html = HTML.toHTML(feeds); - - try (BufferedWriter writer = Files.newBufferedWriter(new File("pnews.html").toPath(), StandardCharsets.UTF_8)) { - writer.write(html); - } - + for (Category cat: Category.values()) + writeHTMLFile(cat); System.out.println("done"); }