updated
authorJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 10 Oct 2017 23:13:52 +0000 (01:13 +0200)
committerJean-Philippe Orsini <orsinije@fr.ibm.com>
Tue, 10 Oct 2017 23:13:52 +0000 (01:13 +0200)
src/main/java/pnews/Category.java
src/main/java/pnews/HTML.java
src/main/java/pnews/Main.java
style.css

index d1b41de..0720d58 100644 (file)
@@ -5,7 +5,9 @@ public enum Category {
         FRANCE("france"),
         SPORT("sport"),
         EUROPE("europe"),
-        ECO("eco");
+        ECO("eco"),
+        ESSONE("essone"),
+        TECHNOLOGIE("technologie");
         
         private final String id;
                 
index 5f089cc..78d584d 100644 (file)
@@ -3,10 +3,16 @@ package pnews;
 import java.util.List;
 
 public class HTML {
-       private static void appendA(StringBuffer buf, String child, String href) {
+       private static void appendA(StringBuffer buf, String child, String href, String cl) {
                buf.append("<a href='");
                buf.append(href);
-               buf.append("'>");
+               buf.append("'");
+               if (cl != null) {
+                       buf.append(" class='");
+                       buf.append(cl);
+                       buf.append('\'');
+               }
+               buf.append('>');
                buf.append(child);
                buf.append("</a>");
        }
@@ -24,31 +30,43 @@ public class HTML {
        }
        
        private static void append(StringBuffer buf, Article a) {               
-               buf.append("<section>\n");
+               buf.append("<div class='article'>\n");
+               
                buf.append("<h2>");
-               appendA(buf, a.title, a.link);
+               if (a.thumbnail != null) {
+                       buf.append("<img class='left' src='");
+                       buf.append(a.thumbnail);
+                       buf.append("'/>\n");
+               }
+               appendA(buf, a.title, a.link, null);
                buf.append("</h2>\n");
-                               
-               buf.append("<img class='left' src='");
-               buf.append(a.thumbnail);
-               buf.append("'/>\n");
                
-               buf.append("<p>" + a.website + " - " + a.publicationDate + "</p>");
+               buf.append("<div class='article-info'>" + a.website + " - " + a.publicationDate + "</div>");
                
-               buf.append("<p>");
-               buf.append(a.description);
-               buf.append("</p>");
+               if (a.description != null) {
+                       buf.append("<p>");
+                       buf.append(a.description);
+                       buf.append("</p>");
+               }
                
-               buf.append("</section>\n");             
+               buf.append("</div>\n");         
        }
        
-       private static void appendMenu(StringBuffer buf) {
+       private static void appendMenu(StringBuffer buf, Category catActive) {
+               String cl;
+               
                buf.append("<nav>\n");
                buf.append("<ul>\n");
 
                for (Category cat: Category.values()) {
                        buf.append("<li>");
-                       appendA(buf, cat.getId(), cat.getId() + ".html");
+                       
+                       if (cat.equals(catActive))
+                               cl = "active";
+                       else
+                               cl = null;
+                       
+                       appendA(buf, cat.getId(), cat.getId() + ".html", cl);
                        buf.append("</li>");
                }
                
@@ -56,7 +74,7 @@ public class HTML {
                buf.append("</nav>\n");
        }
        
-       public static String toHTML(List<Article> articles) {
+       public static String toHTML(List<Article> articles, Category catActive) {
                StringBuffer buf;
                
                buf = new StringBuffer();
@@ -69,7 +87,7 @@ public class HTML {
                buf.append("</head>\n");
                buf.append("<body>\n");
                
-               appendMenu(buf);
+               appendMenu(buf, catActive);
                
                for (Article e: articles)
                        append(buf, e);
index 064d597..1dd29f7 100644 (file)
@@ -7,9 +7,24 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.Comparator;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+
+import org.jsoup.Jsoup;
 
 import com.rometools.rome.feed.synd.SyndEnclosure;
 import com.rometools.rome.feed.synd.SyndEntry;
@@ -17,15 +32,42 @@ 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 com.sun.xml.internal.ws.policy.privateutil.PolicyUtils.Collections;
-
-import org.jsoup.*;
 
 public class Main {
+        private static final Logger LOG = Logger.getLogger(Main.class.getName());
+        
+        static {
+                TrustManager[] mgrs;
+                SSLContext sc;
+                
+                mgrs = new TrustManager[]{
+                                new X509TrustManager() {
+                                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+                                                return null;
+                                        }
+
+                                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
+                                        }
+
+                                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
+                                        }
+                                }
+                };
+
+                try {
+                        sc = SSLContext.getInstance("SSL");
 
+                        sc.init(null, mgrs, new java.security.SecureRandom());
+                        SSLContext.setDefault(sc);
+                } catch (NoSuchAlgorithmException | KeyManagementException e) {
+                        e.printStackTrace();
+                }
+        }
+        
        private static void addArticles(Category cat, SyndFeed feed, List<Article> articles) {
                String thumbnail;
                String desc;
+               Date date;              
                
                for (SyndEntry entry: feed.getEntries()) {
                        thumbnail = null;
@@ -34,17 +76,24 @@ public class Main {
                                        thumbnail = e.getUrl();    
                                break;
                        }
-
-           
+                               
+                       if (entry.getDescription() != null) {                             
+                               desc = Jsoup.parse(entry.getDescription().getValue()).text();
+                       } else {       
+                               desc = null;
+                               LOG.severe("No description for " + feed.getTitle() + " - " + entry.getTitle());
+                       }
                        
-                       desc = Jsoup.parse(entry.getDescription().getValue()).text();
+                       date = entry.getPublishedDate();
+                       if (date == null)
+                               date = entry.getUpdatedDate();
                        
                        articles.add(new Article(entry.getLink(),
                                                 cat,
                                                 entry.getTitle(),
                                                 desc,
                                                 thumbnail,
-                                                entry.getPublishedDate(),
+                                                date,
                                                 feed.getTitle()));
                 }               
        }
@@ -55,30 +104,68 @@ public class Main {
                 }
        }
        
+       private static Map<Category, String[]> getFeeds() {
+               Map<Category, String[]> result;
+               
+               result = new HashMap<>();
+               
+               result.put(Category.TOP,
+                          new String[] {
+                                          "http://www.francetvinfo.fr/titres.rss",
+                                          "http://www.france24.com/fr/actualites/rss",
+                                          "https://www.franceinter.fr/rss/a-la-une.xml",
+                                          "http://www.rfi.fr/general/rss",
+                                          "http://www.bfmtv.com/rss/info/flux-rss/flux-toutes-les-actualites/"
+                          });
+               
+               result.put(Category.SPORT,
+                          new String[] { "http://www.france24.com/fr/sports/rss" });
+               
+               result.put(Category.SPORT,
+                                  new String[] { "http://www.france24.com/fr/sports/rss" });
+               
+               result.put(Category.FRANCE,
+                                new String[] { "http://www.france24.com/fr/france/rss",
+                                               "http://www.rfi.fr/france/rss"});
+               
+               result.put(Category.EUROPE,
+                                new String[] { "http://www.france24.com/fr/europe/rss" });
+               
+               result.put(Category.ECO,
+                                new String[] { "http://www.france24.com/fr/economie/rss",
+                                               "http://www.rfi.fr/economie/rss" });
+               
+               result.put(Category.ESSONE,
+                                new String[] { "https://www.essonneinfo.fr/feed/" });
+               
+               result.put(Category.TECHNOLOGIE,
+                                new String[] { "http://feeds.feedburner.com/lesnumeriques/news",
+                                               "http://www.zdnet.fr/feeds/rss/actualites/"});
+               
+               return result;
+       }
+       
        private static List<Article> getArticles(Category cat) throws IllegalArgumentException, MalformedURLException, FeedException, IOException {
                List<Article> articles;
+               String[] feeds;
+               Set<String> links;
                
                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();
+               feeds = getFeeds().get(cat);
+               
+               if (feeds != null)
+                       for (String str: feeds)
+                               addArticles(cat, getSyndFeed(str), articles);
+               else
+                       LOG.severe("No feed for category " + cat);
+
+               links = new HashSet<>(articles.size());
+               for (Article a: articles) {
+                       if (links.contains(a.link))
+                               LOG.severe(a.link + "is not uniq");
+                       else
+                               links.add(a.link);
                }
                
                articles.sort(new Comparator<Article> () {
@@ -98,7 +185,7 @@ public class Main {
                
                articles = getArticles(cat);
                
-               html = HTML.toHTML(articles);
+               html = HTML.toHTML(articles, cat);
                
                f = new File(cat.getId() + ".html");
                
index 4005623..85a880d 100644 (file)
--- a/style.css
+++ b/style.css
@@ -7,17 +7,38 @@ body {
         margin: 0 0 0 0;
         padding: 1em 1em 1em 1em;
         background-color: #eee;
+        font-family: sans-serif;
 }
 
-section {
-        margin: 1em 1em 1em 1em;
+nav {
+        font-size: 125%;
+        margin: 0 0 0 0;
         padding: 0 0 0 0;
 }
 
+a.active {
+        text-decoration: none;
+        border-bottom: 4px solid black;
+}
+
+div {
+        margin: 0em 0em 0em 0em;
+        padding: 0 0 0 0;
+}
+
+div.article {
+        margin-bottom: 1em;
+}
+
+.article-info {
+        font-size: 80%;
+        color: #bbb;
+}
+
 img {
-        margin: 1em 1em 1em 1em;
+        margin: 0em 1em 1em 0em;
         padding: 0 0 0 0;
-        height: 8em;
+        width: 8em;
 }
 
 p {