3 import java.io.BufferedWriter;
5 import java.io.IOException;
6 import java.net.MalformedURLException;
8 import java.nio.charset.StandardCharsets;
9 import java.nio.file.Files;
10 import java.security.KeyManagementException;
11 import java.security.NoSuchAlgorithmException;
12 import java.security.cert.X509Certificate;
13 import java.util.ArrayList;
14 import java.util.Comparator;
15 import java.util.Date;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
21 import java.util.logging.Logger;
23 import javax.net.ssl.SSLContext;
24 import javax.net.ssl.TrustManager;
25 import javax.net.ssl.X509TrustManager;
27 import org.jsoup.Jsoup;
29 import com.rometools.rome.feed.synd.SyndEnclosure;
30 import com.rometools.rome.feed.synd.SyndEntry;
31 import com.rometools.rome.feed.synd.SyndFeed;
32 import com.rometools.rome.io.FeedException;
33 import com.rometools.rome.io.SyndFeedInput;
34 import com.rometools.rome.io.XmlReader;
37 private static final Logger LOG = Logger.getLogger(Main.class.getName());
43 mgrs = new TrustManager[]{
44 new X509TrustManager() {
45 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
49 public void checkClientTrusted(X509Certificate[] certs, String authType) {
52 public void checkServerTrusted(X509Certificate[] certs, String authType) {
58 sc = SSLContext.getInstance("SSL");
60 sc.init(null, mgrs, new java.security.SecureRandom());
61 SSLContext.setDefault(sc);
62 } catch (NoSuchAlgorithmException | KeyManagementException e) {
67 private static void addArticles(Category cat, SyndFeed feed, List<Article> articles) {
72 for (SyndEntry entry: feed.getEntries()) {
74 for (SyndEnclosure e: entry.getEnclosures()) {
75 if (e.getType().startsWith("image/"))
76 thumbnail = e.getUrl();
80 if (entry.getDescription() != null) {
81 desc = Jsoup.parse(entry.getDescription().getValue()).text();
84 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
87 date = entry.getPublishedDate();
89 date = entry.getUpdatedDate();
91 articles.add(new Article(entry.getLink(),
101 private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
102 try (XmlReader reader = new XmlReader(new URL(u))) {
103 return new SyndFeedInput().build(reader);
107 private static Map<Category, String[]> getFeeds() {
108 Map<Category, String[]> result;
110 result = new HashMap<>();
112 result.put(Category.TOP,
114 "http://www.francetvinfo.fr/titres.rss",
115 "http://www.france24.com/fr/actualites/rss",
116 "https://www.franceinter.fr/rss/a-la-une.xml",
117 "http://www.rfi.fr/general/rss",
118 "http://www.cnews.fr/rss/une",
119 "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
122 result.put(Category.SPORT,
123 new String[] { "http://www.france24.com/fr/sports/rss" });
125 result.put(Category.FRANCE,
126 new String[] { "http://www.france24.com/fr/france/rss",
127 "http://www.rfi.fr/france/rss"});
129 result.put(Category.EUROPE,
130 new String[] { "http://www.france24.com/fr/europe/rss" });
132 result.put(Category.ECO,
133 new String[] { "http://www.france24.com/fr/economie/rss",
134 "http://www.rfi.fr/economie/rss" });
136 result.put(Category.ESSONNE,
137 new String[] { "https://www.essonneinfo.fr/feed/" });
139 result.put(Category.TECHNOLOGIE,
140 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
141 "http://www.zdnet.fr/feeds/rss/actualites/"});
146 private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
147 List<Article> articles;
151 articles = new ArrayList<>();
153 feeds = getFeeds().get(cat);
156 for (String str: feeds)
157 addArticles(cat, getSyndFeed(str), articles);
159 LOG.severe("No feed for category " + cat);
161 links = new HashSet<>(articles.size());
162 for (Article a: articles) {
163 if (links.contains(a.link))
164 LOG.severe(a.link + "is not uniq");
169 articles.sort(new Comparator<Article> () {
171 public int compare(Article o1, Article o2) {
172 return o2.publicationDate.compareTo(o1.publicationDate);
179 private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
180 List<Article> articles;
184 articles = getArticles(cat);
186 html = HTML.toHTML(articles, cat);
188 f = new File(cat.getId() + ".html");
190 try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
195 public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {
196 System.out.println("pnews");
198 for (Category cat: Category.values())
201 System.out.println("done");