Version 1.0.0: add Le Monde support
[gofetch.git] / src / be / nikiroo / gofetch / support / Pipedot.java
index 4d68fe7821df1c7b781d7ef02e52384384e44530..89932f7636c170abc3b34b7c71542880b255984c 100644 (file)
@@ -9,6 +9,7 @@ import java.util.List;
 import org.jsoup.helper.DataUtil;
 import org.jsoup.nodes.Document;
 import org.jsoup.nodes.Element;
+import org.jsoup.nodes.Node;
 import org.jsoup.select.Elements;
 
 import be.nikiroo.gofetch.data.Comment;
@@ -32,9 +33,9 @@ public class Pipedot extends BasicSupport {
                URL url = new URL("https://pipedot.org/");
                InputStream in = open(url);
                Document doc = DataUtil.load(in, "UTF-8", url.toString());
-               Elements stories = doc.getElementsByClass("story");
-               for (Element story : stories) {
-                       Elements titles = story.getElementsByTag("h1");
+               Elements articles = doc.getElementsByClass("story");
+               for (Element article : articles) {
+                       Elements titles = article.getElementsByTag("h1");
                        if (titles.size() == 0) {
                                continue;
                        }
@@ -42,7 +43,7 @@ public class Pipedot extends BasicSupport {
                        Element title = titles.get(0);
 
                        String id = "";
-                       for (Element idElem : story.getElementsByTag("a")) {
+                       for (Element idElem : article.getElementsByTag("a")) {
                                if (idElem.attr("href").startsWith("/pipe/")) {
                                        id = idElem.attr("href").substring("/pipe/".length());
                                        break;
@@ -52,7 +53,7 @@ public class Pipedot extends BasicSupport {
                        String intUrl = null;
                        String extUrl = null;
 
-                       Elements links = story.getElementsByTag("a");
+                       Elements links = article.getElementsByTag("a");
                        if (links.size() > 0) {
                                intUrl = links.get(0).absUrl("href");
                        }
@@ -67,13 +68,13 @@ public class Pipedot extends BasicSupport {
                        }
 
                        String details = "";
-                       Elements detailsElements = story.getElementsByTag("div");
+                       Elements detailsElements = article.getElementsByTag("div");
                        if (detailsElements.size() > 0) {
                                details = detailsElements.get(0).text();
                        }
 
                        String body = "";
-                       for (Element elem : story.children()) {
+                       for (Element elem : article.children()) {
                                String tag = elem.tag().toString();
                                if (!tag.equals("header") && !tag.equals("footer")) {
                                        body = elem.text();
@@ -89,7 +90,7 @@ public class Pipedot extends BasicSupport {
        }
 
        @Override
-       public List<Comment> getComments(Story story) throws IOException {
+       public void fetch(Story story) throws IOException {
                List<Comment> comments = new ArrayList<Comment>();
 
                URL url = new URL(story.getUrlInternal());
@@ -100,7 +101,7 @@ public class Pipedot extends BasicSupport {
                        comments.addAll(getComments(listing.get(0)));
                }
 
-               return comments;
+               story.setComments(comments);
        }
 
        private List<Comment> getComments(Element listing) {
@@ -117,9 +118,9 @@ public class Pipedot extends BasicSupport {
        }
 
        private Comment getComment(Element commentElement) {
-               String title = firstOrEmptyTag(commentElement, "h3");
-               String author = firstOrEmpty(commentElement, "h4");
-               String content = firstOrEmpty(commentElement, "comment-body");
+               String title = firstOrEmptyTag(commentElement, "h3").text();
+               String author = firstOrEmpty(commentElement, "h4").text();
+               Element content = firstOrEmpty(commentElement, "comment-body");
 
                String date = "";
                int pos = author.lastIndexOf(" on ");
@@ -129,7 +130,7 @@ public class Pipedot extends BasicSupport {
                }
 
                Comment comment = new Comment(commentElement.id(), author, title, date,
-                               content);
+                               toLines(content));
 
                Elements commentOutline = commentElement
                                .getElementsByClass("comment-outline");
@@ -140,43 +141,35 @@ public class Pipedot extends BasicSupport {
                return comment;
        }
 
-       /**
-        * Get the first element of the given class, or an empty {@link String} if
-        * none found.
-        * 
-        * @param element
-        *            the element to look in
-        * @param className
-        *            the class to look for
-        * 
-        * @return the value or an empty {@link String}
-        */
-       private String firstOrEmpty(Element element, String className) {
-               Elements subElements = element.getElementsByClass(className);
-               if (subElements.size() > 0) {
-                       return subElements.get(0).text();
-               }
+       private List<String> toLines(Element element) {
+               return toLines(element, new QuoteProcessor() {
+                       @Override
+                       public String processText(String text) {
+                               return text;
+                       }
 
-               return "";
-       }
+                       @Override
+                       public boolean detectQuote(Node node) {
+                               if (node instanceof Element) {
+                                       Element elementNode = (Element) node;
+                                       if (elementNode.tagName().equals("blockquote")
+                                                       || elementNode.hasClass("quote")) {
+                                               return true;
+                                       }
+                               }
 
-       /**
-        * Get the first element of the given tag, or an empty {@link String} if
-        * none found.
-        * 
-        * @param element
-        *            the element to look in
-        * @param tagName
-        *            the tag to look for
-        * 
-        * @return the value or an empty {@link String}
-        */
-       private String firstOrEmptyTag(Element element, String tagName) {
-               Elements subElements = element.getElementsByTag(tagName);
-               if (subElements.size() > 0) {
-                       return subElements.get(0).text();
-               }
+                               return false;
+                       }
 
-               return "";
+                       @Override
+                       public boolean ignoreNode(Node node) {
+                               return false;
+                       }
+
+                       @Override
+                       public String manualProcessing(Node node) {
+                               return null;
+                       }
+               });
        }
 }