Small fixes in different places
[gofetch.git] / src / be / nikiroo / gofetch / support / Slashdot.java
index 1581d23cb2361f8fa55c912ff1e2516c8fc91d9d..b3a779da62d229469346f8a9455b1e01b160ab9d 100644 (file)
@@ -14,6 +14,7 @@ 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://slashdot.org/'>https://slashdot.org/</a>.
@@ -31,7 +32,7 @@ public class Slashdot extends BasicSupport {
                List<Story> list = new ArrayList<Story>();
 
                URL url = new URL("https://slashdot.org/");
-               InputStream in = open(url);
+               InputStream in = downloader.open(url);
                Document doc = DataUtil.load(in, "UTF-8", url.toString());
                Elements articles = doc.getElementsByTag("header");
                for (Element article : articles) {
@@ -48,8 +49,8 @@ public class Slashdot extends BasicSupport {
                        }
 
                        Elements links = title.getElementsByTag("a");
-                       String intUrl = null;
-                       String extUrl = null;
+                       String intUrl = "";
+                       String extUrl = "";
                        if (links.size() > 0) {
                                intUrl = links.get(0).absUrl("href");
                        }
@@ -63,14 +64,41 @@ public class Slashdot extends BasicSupport {
                                details = detailsElements.get(0).text();
                        }
 
+                       // details:
+                       // "Posted by AUTHOR on DATE from the further-crackdown dept."
+                       String author = "";
+                       int pos = details.indexOf(" on ");
+                       if (details.startsWith("Posted by ") && pos >= 0) {
+                               author = details.substring("Posted by ".length(), pos).trim();
+                       }
+                       pos = details.indexOf(" from the ");
+                       if (pos >= 0) {
+                               details = details.substring(pos).trim();
+                       }
+
                        String body = "";
                        Element bodyElement = doc.getElementById("text-" + id);
                        if (bodyElement != null) {
                                body = bodyElement.text();
                        }
 
-                       list.add(new Story(getType(), id, title.text(), details, intUrl,
-                                       extUrl, body));
+                       String categ = "";
+                       Element categElement = doc.getElementsByClass("topic").first();
+                       if (categElement != null) {
+                               categ = StringUtils.unhtml(categElement.text()).trim();
+                       }
+
+                       String date = "";
+                       Element dateElement = doc.getElementsByTag("time").first();
+                       if (dateElement != null) {
+                               date = StringUtils.unhtml(dateElement.text()).trim();
+                               if (date.startsWith("on ")) {
+                                       date = date.substring("on ".length());
+                               }
+                       }
+
+                       list.add(new Story(getType(), id, title.text(), author, date,
+                                       categ, details, intUrl, extUrl, body));
                }
 
                return list;
@@ -81,7 +109,7 @@ public class Slashdot extends BasicSupport {
                List<Comment> comments = new ArrayList<Comment>();
 
                URL url = new URL(story.getUrlInternal());
-               InputStream in = open(url);
+               InputStream in = downloader.open(url);
                Document doc = DataUtil.load(in, "UTF-8", url.toString());
                Element listing = doc.getElementById("commentlisting");
                if (listing != null) {