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.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
121 result.put(Category.SPORT,
122 new String[] { "http://www.france24.com/fr/sports/rss" });
124 result.put(Category.SPORT,
125 new String[] { "http://www.france24.com/fr/sports/rss" });
127 result.put(Category.FRANCE,
128 new String[] { "http://www.france24.com/fr/france/rss",
129 "http://www.rfi.fr/france/rss"});
131 result.put(Category.EUROPE,
132 new String[] { "http://www.france24.com/fr/europe/rss" });
134 result.put(Category.ECO,
135 new String[] { "http://www.france24.com/fr/economie/rss",
136 "http://www.rfi.fr/economie/rss" });
138 result.put(Category.ESSONNE,
139 new String[] { "https://www.essonneinfo.fr/feed/" });
141 result.put(Category.TECHNOLOGIE,
142 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
143 "http://www.zdnet.fr/feeds/rss/actualites/"});
148 private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
149 List<Article> articles;
153 articles = new ArrayList<>();
155 feeds = getFeeds().get(cat);
158 for (String str: feeds)
159 addArticles(cat, getSyndFeed(str), articles);
161 LOG.severe("No feed for category " + cat);
163 links = new HashSet<>(articles.size());
164 for (Article a: articles) {
165 if (links.contains(a.link))
166 LOG.severe(a.link + "is not uniq");
171 articles.sort(new Comparator<Article> () {
173 public int compare(Article o1, Article o2) {
174 return o2.publicationDate.compareTo(o1.publicationDate);
181 private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
182 List<Article> articles;
186 articles = getArticles(cat);
188 html = HTML.toHTML(articles, cat);
190 f = new File(cat.getId() + ".html");
192 try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
197 public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {
198 System.out.println("pnews");
200 for (Category cat: Category.values())
203 System.out.println("done");