d4665de0bea6dcf1ed80f9bf32713dc3f7eaaa9a
[pnews.git] / src / main / java / pnews / Main.java
1 package pnews;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.IOException;
6 import java.net.MalformedURLException;
7 import java.net.URL;
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;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.logging.Logger;
22
23 import javax.net.ssl.SSLContext;
24 import javax.net.ssl.TrustManager;
25 import javax.net.ssl.X509TrustManager;
26
27 import org.jsoup.Jsoup;
28
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;
35
36 public class Main {
37         private static final Logger LOG = Logger.getLogger(Main.class.getName());
38         
39         static {
40                 TrustManager[] mgrs;
41                 SSLContext sc;
42                 
43                 mgrs = new TrustManager[]{
44                                 new X509TrustManager() {
45                                         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
46                                                 return null;
47                                         }
48
49                                         public void checkClientTrusted(X509Certificate[] certs, String authType) {
50                                         }
51
52                                         public void checkServerTrusted(X509Certificate[] certs, String authType) {
53                                         }
54                                 }
55                 };
56
57                 try {
58                         sc = SSLContext.getInstance("SSL");
59
60                         sc.init(null, mgrs, new java.security.SecureRandom());
61                         SSLContext.setDefault(sc);
62                 } catch (NoSuchAlgorithmException | KeyManagementException e) {
63                         e.printStackTrace();
64                 }
65         }
66         
67         private static void addArticles(Category cat, SyndFeed feed, List<Article> articles) {
68                 String thumbnail;
69                 String desc;
70                 Date date;              
71                 
72                 for (SyndEntry entry: feed.getEntries()) {
73                         thumbnail = null;
74                         for (SyndEnclosure e: entry.getEnclosures()) {
75                                 if (e.getType().startsWith("image/"))
76                                         thumbnail = e.getUrl();    
77                                 break;
78                         }
79                                 
80                         if (entry.getDescription() != null) {                             
81                                 desc = Jsoup.parse(entry.getDescription().getValue()).text();
82                         } else {       
83                                 desc = null;
84                                 LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
85                         }
86                         
87                         date = entry.getPublishedDate();
88                         if (date == null)
89                                 date = entry.getUpdatedDate();
90                         
91                         articles.add(new Article(entry.getLink(),
92                                                  cat,
93                                                  entry.getTitle(),
94                                                  desc,
95                                                  thumbnail,
96                                                  date,
97                                                  feed.getTitle()));
98                 }               
99         }
100         
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);
104                 }
105         }
106         
107         private static Map<Category, String[]> getFeeds() {
108                 Map<Category, String[]> result;
109                 
110                 result = new HashMap<>();
111                 
112                 result.put(Category.TOP,
113                            new String[] {
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/"
119                            });
120                 
121                 result.put(Category.SPORT,
122                            new String[] { "http://www.france24.com/fr/sports/rss" });
123                 
124                 result.put(Category.SPORT,
125                                    new String[] { "http://www.france24.com/fr/sports/rss" });
126                 
127                 result.put(Category.FRANCE,
128                                 new String[] { "http://www.france24.com/fr/france/rss",
129                                                "http://www.rfi.fr/france/rss"});
130                 
131                 result.put(Category.EUROPE,
132                                 new String[] { "http://www.france24.com/fr/europe/rss" });
133                 
134                 result.put(Category.ECO,
135                                 new String[] { "http://www.france24.com/fr/economie/rss",
136                                                "http://www.rfi.fr/economie/rss" });
137                 
138                 result.put(Category.ESSONNE,
139                                 new String[] { "https://www.essonneinfo.fr/feed/" });
140                 
141                 result.put(Category.TECHNOLOGIE,
142                                 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
143                                                "http://www.zdnet.fr/feeds/rss/actualites/"});
144                 
145                 return result;
146         }
147         
148         private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
149                 List<Article> articles;
150                 String[] feeds;
151                 Set<String> links;
152                 
153                 articles = new ArrayList<>();
154                 
155                 feeds = getFeeds().get(cat);
156                 
157                 if (feeds != null)
158                         for (String str: feeds)
159                                 addArticles(cat, getSyndFeed(str), articles);
160                 else
161                         LOG.severe("No feed for category " + cat);
162
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");
167                         else
168                                 links.add(a.link);
169                 }
170                 
171                 articles.sort(new Comparator<Article> () {
172                         @Override
173                         public int compare(Article o1, Article o2) {
174                                 return o2.publicationDate.compareTo(o1.publicationDate);
175                         }                       
176                 });
177                 
178                 return articles;
179         }
180         
181         private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
182                 List<Article> articles;
183                 String html;
184                 File f;
185                 
186                 articles = getArticles(cat);
187                 
188                 html = HTML.toHTML(articles, cat);
189                 
190                 f = new File(cat.getId() + ".html");
191                 
192                 try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
193                         writer.write(html);                     
194                 }
195         }
196         
197         public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {                            
198                 System.out.println("pnews");
199                 
200                 for (Category cat: Category.values())
201                         writeHTMLFile(cat);
202                 
203                 System.out.println("done");
204         }
205 }