updated
[pnews.git] / src / main / java / pnews / Main.java
index fce2fd1..35962bc 100644 (file)
@@ -3,57 +3,103 @@ package pnews;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.IOException;
+import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 
-import com.rometools.rome.feed.synd.SyndCategory;
+import com.rometools.rome.feed.synd.SyndEnclosure;
+import com.rometools.rome.feed.synd.SyndEntry;
 import com.rometools.rome.feed.synd.SyndFeed;
 import com.rometools.rome.io.FeedException;
 import com.rometools.rome.io.SyndFeedInput;
 import com.rometools.rome.io.XmlReader;
+import org.jsoup.*;
 
 public class Main {
-       private static void println(SyndFeed feed) {
-               System.out.println(feed.getTitle());
-               for (SyndCategory cat: feed.getCategories()) {
-                       System.out.println(cat.getName());
-               }
-               System.out.println(feed.getFeedType());
+
+       private static void addArticles(Category cat, SyndFeed feed, List<Article> articles) {
+               String thumbnail;
+               String desc;
+               
+               for (SyndEntry entry: feed.getEntries()) {
+                       thumbnail = null;
+                       for (SyndEnclosure e: entry.getEnclosures()) {
+                               if (e.getType().startsWith("image/"))
+                                       thumbnail = e.getUrl();    
+                               break;
+                       }
+
+           
+                       
+                       desc = Jsoup.parse(entry.getDescription().getValue()).text();
+                       
+                       articles.add(new Article(entry.getLink(),
+                                                cat,
+                                                entry.getTitle(),
+                                                desc,
+                                                thumbnail));
+                }               
        }
        
-       public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {
-               URL url;
-               String html;
-               List<SyndFeed> feeds;
-               SyndFeed feed;
-               String[] urls = new String[] {
-                               "http://www.france24.com/fr/france/rss",
-                               "https://www.lesechos.fr/rss/rss_une_titres.xml"
-               };
-               
+       private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException {
+                try (XmlReader reader = new XmlReader(new URL(u))) {
+                        return new SyndFeedInput().build(reader);
+                }
+       }
+       
+       private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+               List<Article> articles;
+               
+               articles = new ArrayList<>();
+               
+               switch (cat) {
+               case TOP:
+                       addArticles(cat, getSyndFeed("http://www.france24.com/fr/actualites/rss"), articles);
+                       addArticles(cat, getSyndFeed("http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"), articles);
+                       break;
+               case SPORT:
+                       addArticles(cat, getSyndFeed("http://www.france24.com/fr/sports/rss"), articles);
+                       break;
+               case FRANCE:
+                       addArticles(cat, getSyndFeed("http://www.france24.com/fr/france/rss"), articles);
+                       break;
+                case EUROPE:
+                        addArticles(cat, getSyndFeed("http://www.france24.com/fr/europe/rss"), articles);
+                        break;
+                case ECO:
+                        addArticles(cat, getSyndFeed("http://www.france24.com/fr/economie/rss"), articles);
+                        break;
+               default:
+                       throw new IllegalArgumentException();
+               }
+               
+               return articles;
+       }
+       
+       private static void writeHTMLFile(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
+               List<Article> articles;
+               String html;
+               File f;
+               
+               articles = getArticles(cat);
+               
+               html = HTML.toHTML(articles);
+               
+               f = new File(cat.getId() + ".html");
+               
+               try (BufferedWriter writer = Files.newBufferedWriter(f.toPath(), StandardCharsets.UTF_8)) {
+                        writer.write(html);                     
+                }
+       }
+       
+       public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {                            
                System.out.println("pnews");
                
-               feeds = new ArrayList<>(urls.length);
-               for (String u: urls) {
-                       url = new URL(u);
-                               
-                       try (XmlReader reader = new XmlReader(url)) {
-                               feed = new SyndFeedInput().build(reader);
-                               println(feed);
-                               feeds.add(feed);
-                       };
-               }
-               
-       
-               html = HTML.toHTML(feeds);
-                       
-               try (BufferedWriter writer = Files.newBufferedWriter(new File("pnews.html").toPath(), StandardCharsets.UTF_8)) {
-                       writer.write(html);                     
-               }
-                       
+               for (Category cat: Category.values())
+                       writeHTMLFile(cat);
                
                System.out.println("done");
        }