From: Jean-Philippe Orsini Date: Fri, 13 Oct 2017 15:47:00 +0000 (+0200) Subject: Merge branch 'master' of ssh://wpitchoune.net/srv/git/pnews X-Git-Url: http://git.wpitchoune.net/gitweb/?p=pnews.git;a=commitdiff_plain;h=782edbcad560e9aba5a0ae0c731bfd76491618c8;hp=1335bf069ad5f5ed219a17b034a06ea35b540b41 Merge branch 'master' of ssh://wpitchoune.net/srv/git/pnews --- diff --git a/pnews/src/main/java/pnews/Category.java b/pnews/src/main/java/pnews/Category.java index efabe5e..7a8106f 100644 --- a/pnews/src/main/java/pnews/Category.java +++ b/pnews/src/main/java/pnews/Category.java @@ -7,7 +7,8 @@ public enum Category { EUROPE("europe"), ECO("eco"), ESSONNE("essonne"), - TECHNOLOGIE("technologie"); + TECHNOLOGIE("technologie"), + PEOPLE("people"); private final String id; diff --git a/pnews/src/main/resources/style.css b/pnews/src/main/resources/style.css new file mode 100644 index 0000000..85a880d --- /dev/null +++ b/pnews/src/main/resources/style.css @@ -0,0 +1,69 @@ +a { + text-decoration: none; + color: black; +} + +body { + margin: 0 0 0 0; + padding: 1em 1em 1em 1em; + background-color: #eee; + font-family: sans-serif; +} + +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: 0em 1em 1em 0em; + padding: 0 0 0 0; + width: 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; +} diff --git a/war/src/main/java/pnews/servlet/ArticleProvider.java b/war/src/main/java/pnews/servlet/ArticleProvider.java index d09cb0c..11689e5 100644 --- a/war/src/main/java/pnews/servlet/ArticleProvider.java +++ b/war/src/main/java/pnews/servlet/ArticleProvider.java @@ -1,6 +1,7 @@ package pnews.servlet; import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; @@ -17,6 +18,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.jsoup.Jsoup; +import org.xml.sax.InputSource; import com.rometools.rome.feed.synd.SyndEnclosure; import com.rometools.rome.feed.synd.SyndEntry; @@ -40,9 +42,11 @@ public class ArticleProvider { } private static SyndFeed getSyndFeed(String u) throws IllegalArgumentException, FeedException, MalformedURLException, IOException { - try (XmlReader reader = new XmlReader(new URL(u))) { - return new SyndFeedInput().build(reader); - } + InputStream is = new URL(u).openConnection().getInputStream(); + InputSource source = new InputSource(is); + + return new SyndFeedInput().build(source); + } private static Map getFeeds() { @@ -75,7 +79,14 @@ public class ArticleProvider { "http://www.rfi.fr/economie/rss" }); result.put(Category.ESSONNE, - new String[] { /*"https://www.essonneinfo.fr/feed/"*/ }); + new String[] { "http://www.tourisme-essonne.com/rss/actus/", + "http://www.ville-palaiseau.fr/rss/actualites.htm" + /*"https://www.essonneinfo.fr/feed/"*/ }); + + result.put(Category.PEOPLE, + new String[] { "http://www.premiere.fr/rss/actu-live", + "http://www.purepeople.com/rss/news_t0.xml" + }); result.put(Category.TECHNOLOGIE, new String[] { "http://feeds.feedburner.com/lesnumeriques/news", @@ -86,9 +97,10 @@ public class ArticleProvider { private void addArticles(Category cat, SyndFeed feed) { String thumbnail; - String desc; + String desc, link, title; Date date; List
articles; + boolean exist; LOG.info("addArticles" + cat.getId()); @@ -100,7 +112,7 @@ public class ArticleProvider { break; } - if (entry.getDescription() != null) { + if (entry.getDescription() != null) { desc = Jsoup.parse(entry.getDescription().getValue()).text(); } else { desc = null; @@ -112,32 +124,35 @@ public class ArticleProvider { date = entry.getUpdatedDate(); synchronized(articlesByCategory) { + link = entry.getLink().trim(); + title = entry.getTitle().trim(); articles = articlesByCategory.get(cat); + exist = false; if (articles == null) { articles = new ArrayList<>(); articlesByCategory.put(cat, articles); } else { for (Article a: articles) - if (a.link.equals(entry.getLink())) - return ; + if (a.link.equals(link)) { + LOG.info(link + " already present"); + exist = true; + } } - articles.add(new Article(entry.getLink(), - cat, - entry.getTitle(), - desc, - thumbnail, - date, - feed.getTitle())); - + if (!exist) { + LOG.info("add " + cat.getId() + " " + feed.getTitle() + " " + title); - Collections.sort(articles, new Comparator
() { - @Override - public int compare(Article o1, Article o2) { - return o2.publicationDate.compareTo(o1.publicationDate); - } - - }); + articles.add(new Article(link, cat, title, desc, thumbnail, date, + feed.getTitle())); + + Collections.sort(articles, new Comparator
() { + @Override + public int compare(Article o1, Article o2) { + return o2.publicationDate.compareTo(o1.publicationDate); + } + + }); + } } } } @@ -149,7 +164,11 @@ public class ArticleProvider { if (feeds != null) for (String str: feeds) - addArticles(cat, getSyndFeed(str)); + try { + addArticles(cat, getSyndFeed(str)); + } catch (IOException e) { + LOG.log(Level.SEVERE, "retrieveArticles failure " + cat.getId(), e); + } else LOG.severe("No feed for category " + cat); } diff --git a/war/src/main/java/pnews/servlet/Pnews.java b/war/src/main/java/pnews/servlet/Pnews.java index 529a40e..845abbb 100644 --- a/war/src/main/java/pnews/servlet/Pnews.java +++ b/war/src/main/java/pnews/servlet/Pnews.java @@ -28,40 +28,40 @@ public class Pnews extends HttpServlet { private static final Logger LOG = Logger.getLogger(Pnews.class.getName()); private static final long serialVersionUID = 1L; private static final ArticleProvider provider = ArticleProvider.singleton; - + private static String getQueryParameter(HttpServletRequest rq, String key) throws UnsupportedEncodingException { String[] params; int idx; String q; - + q = rq.getQueryString(); - + if (q == null) return null; - + params = URLDecoder.decode(q, "UTF-8").split("&"); - - for (String p: params) { + + for (String p: params) { idx = p.indexOf('='); - + if (idx > 1 && p.substring(0, idx).equals(key)) return p.substring(idx + 1); } - + return null; } - + private static void redirect(HttpServletRequest rq, HttpServletResponse rp) { String redirectURL; - + LOG.entering(Pnews.class.getName(), "redirect"); - + try { redirectURL = getQueryParameter(rq, "url"); - + LOG.info("Request redirection to " + redirectURL); - + if (redirectURL != null) { rp.setHeader("Location", redirectURL); rp.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT); @@ -74,16 +74,16 @@ public class Pnews extends HttpServlet { e.printStackTrace(); LOG.log(Level.SEVERE, "redirect failure", e); rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - } - + } + LOG.exiting(Pnews.class.getName(), "redirect"); } - + private void writeArticles(Category cat, HttpServletResponse rp) { String html; List
articles; - - try { + + try { articles = provider.getArticles(cat); if (articles != null) { html = HTML.toHTML(articles, cat); @@ -100,59 +100,59 @@ public class Pnews extends HttpServlet { rp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } - + private void copy(InputStream in, Writer writer) throws IOException { Reader r; char[] buf; int n; - + buf = new char[1024]; r = new InputStreamReader(in); while ( (n = r.read(buf, 0, buf.length)) != -1) - writer.write(buf, 0, n); + writer.write(buf, 0, n); } - + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) { String path; InputStream in; - + LOG.info("doGet " + req.getRequestURI()); path = req.getPathInfo(); - + if (path.equals("/redirect")) { redirect(req, resp); return ; - } - + } + if (path.equals("/style.css")) { try { in = HTML.class.getClassLoader().getResourceAsStream("style.css"); copy(in, resp.getWriter()); resp.setContentType("text/css"); - + return ; } catch (IOException e) { LOG.log(Level.SEVERE, "doGet failure", e); resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - + return ; } } - + if (path.equals("/")) { writeArticles(Category.TOP, resp); return ; } - + for (Category cat: Category.values()) { if (path.equals('/' + cat.getId())) { writeArticles(cat, resp); return ; } } - + try { resp.getWriter().write("Not found " + req.getPathInfo()); resp.setStatus(HttpServletResponse.SC_NOT_FOUND); @@ -161,10 +161,10 @@ public class Pnews extends HttpServlet { resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } - + @Override public void init(ServletConfig config) throws ServletException { LOG.info("Pnews servlet init " + config.getServletContext().getContextPath()); - + } }