1 package be
.nikiroo
.gofetch
.support
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
9 import org
.jsoup
.helper
.DataUtil
;
10 import org
.jsoup
.nodes
.Document
;
11 import org
.jsoup
.nodes
.Element
;
12 import org
.jsoup
.select
.Elements
;
14 import be
.nikiroo
.gofetch
.data
.Comment
;
15 import be
.nikiroo
.gofetch
.data
.Story
;
18 * Support <a href='https://slashdot.org/'>https://slashdot.org/</a>.
22 public class Slashdot
extends BasicSupport
{
24 public String
getDescription() {
25 return "Slashdot: News for nerds, stuff that matters!";
29 public List
<Story
> list() throws IOException
{
30 List
<Story
> list
= new ArrayList
<Story
>();
32 URL url
= new URL("https://slashdot.org/");
33 InputStream in
= open(url
);
34 Document doc
= DataUtil
.load(in
, "UTF-8", url
.toString());
35 Elements stories
= doc
.getElementsByTag("header");
36 for (Element story
: stories
) {
37 Elements titles
= story
.getElementsByClass("story-title");
38 if (titles
.size() == 0) {
42 Element title
= titles
.get(0);
44 String id
= "" + title
.attr("id");
45 if (id
.startsWith("title-")) {
46 id
= id
.substring("title-".length());
49 Elements links
= title
.getElementsByTag("a");
52 if (links
.size() > 0) {
53 intUrl
= links
.get(0).absUrl("href");
55 if (links
.size() > 1) {
56 extUrl
= links
.get(1).absUrl("href");
60 Elements detailsElements
= story
.getElementsByClass("details");
61 if (detailsElements
.size() > 0) {
62 details
= detailsElements
.get(0).text();
66 Element bodyElement
= doc
.getElementById("text-" + id
);
67 if (bodyElement
!= null) {
68 body
= bodyElement
.text();
71 list
.add(new Story(getType(), id
, title
.text(), details
, intUrl
,
79 public void fetch(Story story
) throws IOException
{
80 List
<Comment
> comments
= new ArrayList
<Comment
>();
82 URL url
= new URL(story
.getUrlInternal());
83 InputStream in
= open(url
);
84 Document doc
= DataUtil
.load(in
, "UTF-8", url
.toString());
85 Element listing
= doc
.getElementById("commentlisting");
86 if (listing
!= null) {
87 comments
.addAll(getComments(listing
));
90 story
.setComments(comments
);
93 private List
<Comment
> getComments(Element listing
) {
94 List
<Comment
> comments
= new ArrayList
<Comment
>();
95 for (Element commentElement
: listing
.children()) {
96 if (commentElement
.hasClass("comment")) {
97 Comment comment
= getComment(commentElement
);
98 if (!comment
.isEmpty()) {
99 comments
.add(comment
);
106 private Comment
getComment(Element commentElement
) {
107 String title
= firstOrEmpty(commentElement
, "title");
108 String author
= firstOrEmpty(commentElement
, "by");
109 String content
= firstOrEmpty(commentElement
, "commentBody");
110 String date
= firstOrEmpty(commentElement
, "otherdetails");
112 Comment comment
= new Comment(commentElement
.id(), author
, title
, date
,
115 for (Element child
: commentElement
.children()) {
116 if (child
.id().contains("commtree_")) {
117 comment
.addAll(getComments(child
));
124 private String
firstOrEmpty(Element element
, String className
) {
125 Elements subElements
= element
.getElementsByClass(className
);
126 if (subElements
.size() > 0) {
127 return subElements
.get(0).text();