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