35962bc6c54a0a54baa5a916253db253a50f1dd5
[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.util.ArrayList;
11 import java.util.List;
12
13 import com.rometools.rome.feed.synd.SyndEnclosure;
14 import com.rometools.rome.feed.synd.SyndEntry;
15 import com.rometools.rome.feed.synd.SyndFeed;
16 import com.rometools.rome.io.FeedException;
17 import com.rometools.rome.io.SyndFeedInput;
18 import com.rometools.rome.io.XmlReader;
19 import org.jsoup.*;
20
21 public class Main {
22
23         private static void addArticles(Category cat, SyndFeed feed, List<Article> articles) {
24                 String thumbnail;
25                 String desc;
26                 
27                 for (SyndEntry entry: feed.getEntries()) {
28                         thumbnail = null;
29                         for (SyndEnclosure e: entry.getEnclosures()) {
30                                 if (e.getType().startsWith("image/"))
31                                         thumbnail = e.getUrl();    
32                                 break;
33                         }
34
35             
36                         
37                         desc = Jsoup.parse(entry.getDescription().getValue()).text();
38                         
39                         articles.add(new Article(entry.getLink(),
40                                                  cat,
41                                                  entry.getTitle(),
42                                                  desc,
43                                                  thumbnail));
44                 }               
45         }
46         
47         private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
48                 try (XmlReader reader = new XmlReader(new URL(u))) {
49                         return new SyndFeedInput().build(reader);
50                 }
51         }
52         
53         private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
54                 List<Article> articles;
55                 
56                 articles = new ArrayList<>();
57                 
58                 switch (cat) {
59                 case TOP:
60                         addArticles(cat, getSyndFeed("http://www.france24.com/fr/actualites/rss"), articles);
61                         addArticles(cat, getSyndFeed("http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"), articles);
62                         break;
63                 case SPORT:
64                         addArticles(cat, getSyndFeed("http://www.france24.com/fr/sports/rss"), articles);
65                         break;
66                 case FRANCE:
67                         addArticles(cat, getSyndFeed("http://www.france24.com/fr/france/rss"), articles);
68                         break;
69                 case EUROPE:
70                         addArticles(cat, getSyndFeed("http://www.france24.com/fr/europe/rss"), articles);
71                         break;
72                 case ECO:
73                         addArticles(cat, getSyndFeed("http://www.france24.com/fr/economie/rss"), articles);
74                         break;
75                 default:
76                         throw new IllegalArgumentException();
77                 }
78                 
79                 return articles;
80         }
81         
82         private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
83                 List<Article> articles;
84                 String html;
85                 File f;
86                 
87                 articles = getArticles(cat);
88                 
89                 html = HTML.toHTML(articles);
90                 
91                 f = new File(cat.getId() + ".html");
92                 
93                 try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
94                         writer.write(html);                     
95                 }
96         }
97         
98         public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {                            
99                 System.out.println("pnews");
100                 
101                 for (Category cat: Category.values())
102                         writeHTMLFile(cat);
103                 
104                 System.out.println("done");
105         }
106 }