updated
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 10 Oct 2017 12:58:32 +0000 (14:58 +0200)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 10 Oct 2017 12:58:32 +0000 (14:58 +0200)
.gitignore [new file with mode: 0644]
pom.xml
src/main/java/pnews/Article.java [new file with mode: 0644]
src/main/java/pnews/Category.java [new file with mode: 0644]
src/main/java/pnews/HTML.java
src/main/java/pnews/Main.java
style.css [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..2061c9e
--- /dev/null
@@ -0,0 +1,3 @@
+target
+.classpath
+.project
diff --git a/pom.xml b/pom.xml
index ad4f27a..5631d03 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                         <artifactId>rome</artifactId>
                         <version>1.8.0</version>
                 </dependency>
+                <dependency>
+                        <groupId>org.jsoup</groupId>
+                        <artifactId>jsoup</artifactId>
+                        <version>1.10.3</version>
+                </dependency>
         </dependencies>
 
         <build>
diff --git a/src/main/java/pnews/Article.java b/src/main/java/pnews/Article.java
new file mode 100644 (file)
index 0000000..6bfe61b
--- /dev/null
@@ -0,0 +1,17 @@
+package pnews;
+
+public class Article {
+        public final String title;
+        public final String description;
+        public final String thumbnail;
+        public final String link;
+        public final Category category;
+        
+        public Article(String link, Category category, String title, String description, String thumbnail) {
+                this.link = link;
+                this.title = title;
+                this.description = description;
+                this.thumbnail = thumbnail;
+                this.category = category;
+        }
+}
diff --git a/src/main/java/pnews/Category.java b/src/main/java/pnews/Category.java
new file mode 100644 (file)
index 0000000..d1b41de
--- /dev/null
@@ -0,0 +1,19 @@
+package pnews;
+
+public enum Category {
+        TOP("top"),
+        FRANCE("france"),
+        SPORT("sport"),
+        EUROPE("europe"),
+        ECO("eco");
+        
+        private final String id;
+                
+        private Category(String id) {
+                this.id = id;
+        }
+        
+        public String getId() {
+                return id;
+        }
+}
index b761b88..b919cf1 100644 (file)
@@ -2,9 +2,6 @@ package pnews;
 
 import java.util.List;
 
-import com.rometools.rome.feed.synd.SyndEntry;
-import com.rometools.rome.feed.synd.SyndFeed;
-
 public class HTML {
        private static void appendA(StringBuffer buf, String child, String href) {
                buf.append("<a href='");
@@ -20,33 +17,61 @@ public class HTML {
                buf.append("</div>\n");
        }
        
-       private static void append(StringBuffer buf, SyndEntry entry) {
-               buf.append("<div>");
-               appendA(buf, entry.getTitle(), entry.getLink());
-               buf.append("</div>\n");
+       private static void appendP(StringBuffer buf, String child) {
+               buf.append("<p>");
+               buf.append(child);
+               buf.append("</p>\n");
+       }
+       
+       private static void append(StringBuffer buf, Article entry) {
+               
+               buf.append("<section>\n");
+               buf.append("<h2>");
+               appendA(buf, entry.title, entry.link);
+               buf.append("</h2>\n");
+                               
+               buf.append("<img class='left' src='");
+               buf.append(entry.thumbnail);
+               buf.append("'/>\n");
+               
+               buf.append("<p>");
+               buf.append(entry.description);
+               buf.append("</p>");
+               
+               buf.append("</section>\n");
                
-               appendDiv(buf, entry.getPublishedDate().toString());
-               appendDiv(buf, entry.getDescription().getValue());
        }
        
-       public static void append(StringBuffer buf, SyndFeed feed) {
-               for (SyndEntry e: feed.getEntries()) {
-                       append(buf, e);
+       private static void appendMenu(StringBuffer buf) {
+               buf.append("<nav>\n");
+               buf.append("<ul>\n");
+
+               for (Category cat: Category.values()) {
+                       buf.append("<li>");
+                       appendA(buf, cat.getId(), cat.getId() + ".html");
+                       buf.append("</li>");
                }
+               
+               buf.append("</ul>\n");
+               buf.append("</nav>\n");
        }
        
-       public static String toHTML(List<SyndFeed> feeds) {
+       public static String toHTML(List<Article> articles) {
                StringBuffer buf;
                
                buf = new StringBuffer();
                buf.append("<!DOCTYPE html>\n");
-               buf.append("<html>\n");
+               buf.append("<html lang='fr'>\n");
                buf.append("<head>\n");
                buf.append("<meta charset=\"UTF-8\">\n");
+               buf.append("<link rel='stylesheet' href='style.css' />\n");
+               buf.append("<title>PNews</title>\n");
                buf.append("</head>\n");
                buf.append("<body>\n");
                
-               for (SyndFeed e: feeds)
+               appendMenu(buf);
+               
+               for (Article e: articles)
                        append(buf, e);
                
                buf.append("</body>\n");
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");
        }
diff --git a/style.css b/style.css
new file mode 100644 (file)
index 0000000..4005623
--- /dev/null
+++ b/style.css
@@ -0,0 +1,48 @@
+a {
+        text-decoration: none;
+        color: black;
+}
+
+body {
+        margin: 0 0 0 0;
+        padding: 1em 1em 1em 1em;
+        background-color: #eee;
+}
+
+section {
+        margin: 1em 1em 1em 1em;
+        padding: 0 0 0 0;
+}
+
+img {
+        margin: 1em 1em 1em 1em;
+        padding: 0 0 0 0;
+        height: 8em;
+}
+
+p {
+        margin: 1em 1em 1em 1em;
+        padding: 0 0 0 0;
+}
+
+.left {
+        float: left;
+}
+
+h2 {
+        clear: left;
+        margin: 0 0 0 0;
+        padding: 0 0 0 0;
+}
+
+nav ul {
+        list-style-type: none;
+        padding: 0 0 0 0;
+}
+
+nav ul li {
+        display: inline;
+        margin: 0em 1em 0 0;
+        padding: 0 0 0 0;
+        text-transform: uppercase;
+}