Bug fixes + rework of BasicSupport
[gofetch.git] / src / be / nikiroo / gofetch / support / TooLinux.java
index 0cc4c6cf58839d9646ad5d567f48767dfdff068e..ba909cfdc63fa60b8fdae0beba04efa0e709c075 100644 (file)
@@ -1,20 +1,15 @@
 package be.nikiroo.gofetch.support;
 
 import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
+import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map.Entry;
 
-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;
-import be.nikiroo.gofetch.data.Story;
-import be.nikiroo.utils.StringUtils;
 
 /**
  * Support <a href="https://www.toolinux.com/">https://www.toolinux.com/</a>.
@@ -28,97 +23,141 @@ public class TooLinux extends BasicSupport {
        }
 
        @Override
-       public List<Story> list() throws IOException {
-               List<Story> list = new ArrayList<Story>();
-
-               URL url = new URL("https://www.toolinux.com/");
-               InputStream in = downloader.open(url);
-               Document doc = DataUtil.load(in, "UTF-8", url.toString());
-               Elements articles = doc.getElementsByClass("hentry");
-               for (Element article : articles) {
-                       String id = "";
-                       String intUrl = "";
-                       String extUrl = ""; // nope
-                       String title = "";
-                       String date = "";
-                       String details = "";
-                       String body = "";
-                       String author = ""; // nope
-                       String categ = ""; // nope
-
-                       Element urlElement = article.getElementsByTag("a").first();
-                       if (urlElement != null) {
-                               intUrl = urlElement.absUrl("href");
-                       }
+       protected List<Entry<URL, String>> getUrls() throws IOException {
+               List<Entry<URL, String>> urls = new ArrayList<Entry<URL, String>>();
+               urls.add(new AbstractMap.SimpleEntry<URL, String>(new URL(
+                               "https://www.toolinux.com/"), ""));
+               return urls;
+       }
 
-                       Element titleElement = article.getElementsByClass("entry-title")
-                                       .first();
-                       if (titleElement != null) {
-                               title = StringUtils.unhtml(titleElement.text()).trim();
-                       }
+       @Override
+       protected List<Element> getArticles(Document doc) {
+               return doc.getElementsByClass("hentry");
+       }
 
-                       Element dateElement = article.getElementsByClass("published")
-                                       .first();
-                       if (dateElement != null) {
-                               date = StringUtils.unhtml(dateElement.text()).trim();
-                               id = dateElement.attr("title").trim();
-                       }
+       @Override
+       protected String getArticleId(Document doc, Element article) {
+               return ""; // We use the date
+       }
 
-                       if (id.isEmpty()) {
-                               // fallback
-                               id = intUrl.replace("/", "_");
-                       }
+       @Override
+       protected String getArticleTitle(Document doc, Element article) {
+               Element titleElement = article.getElementsByClass("entry-title")
+                               .first();
+               if (titleElement != null) {
+                       return titleElement.text();
+               }
 
-                       Element bodyElement = article.getElementsByClass("introduction")
-                                       .first();
-                       if (bodyElement != null) {
-                               body = StringUtils.unhtml(bodyElement.text()).trim();
-                       }
+               return "";
+       }
 
-                       list.add(new Story(getType(), id, title, author, date, categ,
-                                       details, intUrl, extUrl, body));
+       @Override
+       protected String getArticleAuthor(Document doc, Element article) {
+               return "";
+       }
+
+       @Override
+       protected String getArticleDate(Document doc, Element article) {
+               Element dateElement = article.getElementsByClass("published").first();
+               if (dateElement != null) {
+                       return dateElement.text();
                }
 
-               return list;
-       }
-
-       @Override
-       public void fetch(Story story) throws IOException {
-               String fullContent = story.getContent();
-               List<Comment> comments = new ArrayList<Comment>();
-               story.setComments(comments);
-
-               URL url = new URL(story.getUrlInternal());
-               InputStream in = downloader.open(url);
-               try {
-                       Document doc = DataUtil.load(in, "UTF-8", url.toString());
-                       Element article = doc.getElementById("content");
-                       if (article != null) {
-                               for (String line : toLines(article,
-                                               new BasicElementProcessor() {
-                                                       @Override
-                                                       public boolean ignoreNode(Node node) {
-                                                               if ("notes".equals(node.attr("class"))) {
-                                                                       return true;
-                                                               }
-                                                               return false;
-                                                       }
-                                               })) {
-                                       fullContent += line + "\n";
-                               }
+               return "";
+       }
 
-                               // Content is too tight with a single break per line:
-                               fullContent = fullContent.replace("\n", "\n\n") //
-                                               .replace("\n\n\n\n", "\n\n") //
-                                               .replace("\n\n\n\n", "\n\n") //
-                                               .trim();
-                       }
+       @Override
+       protected String getArticleCategory(Document doc, Element article,
+                       String currentCategory) {
+               return "";
+       }
 
-                       story.setFullContent(fullContent);
-               } finally {
-                       if (in != null) {
-                               in.close();
-                       }
+       @Override
+       protected String getArticleDetails(Document doc, Element article) {
+               return "";
+       }
+
+       @Override
+       protected String getArticleIntUrl(Document doc, Element article) {
+               Element urlElement = article.getElementsByTag("a").first();
+               if (urlElement != null) {
+                       return urlElement.absUrl("href");
                }
+
+               return "";
+       }
+
+       @Override
+       protected String getArticleExtUrl(Document doc, Element article) {
+               return "";
+       }
+
+       @Override
+       protected String getArticleContent(Document doc, Element article) {
+               Element content = article.getElementsByClass("introduction").first();
+               if (content != null) {
+                       return content.text();
+               }
+
+               return "";
+       }
+
+       @Override
+       protected Element getFullArticle(Document doc) {
+               return doc.getElementById("content");
+       }
+
+       @Override
+       protected List<Element> getFullArticleCommentPosts(Document doc, URL intUrl) {
+               return null;
+       }
+
+       @Override
+       protected ElementProcessor getElementProcessorFullArticle() {
+               return new BasicElementProcessor() {
+                       @Override
+                       public boolean ignoreNode(Node node) {
+                               if ("notes".equals(node.attr("class"))) {
+                                       return true;
+                               }
+                               return false;
+                       }
+               };
+       }
+
+       @Override
+       protected List<Element> getCommentCommentPosts(Document doc,
+                       Element container) {
+               return null;
+       }
+
+       @Override
+       protected String getCommentId(Element post) {
+               return null;
+       }
+
+       @Override
+       protected String getCommentAuthor(Element post) {
+               return null;
+       }
+
+       @Override
+       protected String getCommentTitle(Element post) {
+               return null;
+       }
+
+       @Override
+       protected String getCommentDate(Element post) {
+               return null;
+       }
+
+       @Override
+       protected Element getCommentContentElement(Element post) {
+               return null;
+       }
+
+       @Override
+       protected ElementProcessor getElementProcessorComment() {
+               return null;
        }
 }