moved to a subdir
[pnews.git] / pnews / 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.cnews.fr/rss/une",
119                                            "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
120                            });
121                 
122                 result.put(Category.SPORT,
123                                 new String[] { "http://www.france24.com/fr/sports/rss" });
124                 
125                 result.put(Category.FRANCE,
126                                 new String[] { "http://www.france24.com/fr/france/rss",
127                                                "http://www.rfi.fr/france/rss"});
128                 
129                 result.put(Category.EUROPE,
130                                 new String[] { "http://www.france24.com/fr/europe/rss" });
131                 
132                 result.put(Category.ECO,
133                                 new String[] { "http://www.france24.com/fr/economie/rss",
134                                                "http://www.rfi.fr/economie/rss" });
135                 
136                 result.put(Category.ESSONNE,
137                                 new String[] { "https://www.essonneinfo.fr/feed/" });
138                 
139                 result.put(Category.TECHNOLOGIE,
140                                 new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
141                                                "http://www.zdnet.fr/feeds/rss/actualites/"});
142                 
143                 return result;
144         }
145         
146         private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
147                 List<Article> articles;
148                 String[] feeds;
149                 Set<String> links;
150                 
151                 articles = new ArrayList<>();
152                 
153                 feeds = getFeeds().get(cat);
154                 
155                 if (feeds != null)
156                         for (String str: feeds)
157                                 addArticles(cat, getSyndFeed(str), articles);
158                 else
159                         LOG.severe("No feed for category " + cat);
160
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");
165                         else
166                                 links.add(a.link);
167                 }
168                 
169                 articles.sort(new Comparator<Article> () {
170                         @Override
171                         public int compare(Article o1, Article o2) {
172                                 return o2.publicationDate.compareTo(o1.publicationDate);
173                         }                       
174                 });
175                 
176                 return articles;
177         }
178         
179         private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
180                 List<Article> articles;
181                 String html;
182                 File f;
183                 
184                 articles = getArticles(cat);
185                 
186                 html = HTML.toHTML(articles, cat);
187                 
188                 f = new File(cat.getId() + ".html");
189                 
190                 try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
191                         writer.write(html);                     
192                 }
193         }
194         
195         public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {                            
196                 System.out.println("pnews");
197                 
198                 for (Category cat: Category.values())
199                         writeHTMLFile(cat);
200                 
201                 System.out.println("done");
202         }
203 }