5dfa03b227e30e2786d0a3dacf0195c6e5d66096
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
;
17 public class Slashdot
extends BasicSupport
{
19 public String
getDescription() {
20 return "Slashdot: News for nerds, stuff that matters!";
24 public List
<Story
> list() throws IOException
{
25 List
<Story
> list
= new ArrayList
<Story
>();
27 URL url
= new URL("https://slashdot.org/");
28 InputStream in
= open(url
);
29 Document doc
= DataUtil
.load(in
, "UTF-8", url
.toString());
30 Elements stories
= doc
.getElementsByTag("header");
31 for (Element story
: stories
) {
32 Elements titles
= story
.getElementsByClass("story-title");
33 if (titles
.size() == 0) {
36 Element title
= titles
.get(0);
38 String id
= "" + title
.attr("id");
39 if (id
.startsWith("title-")) {
40 id
= id
.substring("title-".length());
43 Elements links
= title
.getElementsByTag("a");
46 if (links
.size() > 0) {
47 intUrl
= links
.get(0).absUrl("href");
49 if (links
.size() > 1) {
50 extUrl
= links
.get(1).absUrl("href");
54 Elements detailsElements
= story
.getElementsByClass("details");
55 if (detailsElements
.size() > 0) {
56 details
= detailsElements
.get(0).text();
60 Element bodyElement
= doc
.getElementById("text-" + id
);
61 if (bodyElement
!= null) {
62 body
= bodyElement
.text();
65 list
.add(new Story(getType(), id
, title
.text(), details
, intUrl
,
73 public List
<Comment
> getComments(Story story
) throws IOException
{
74 List
<Comment
> comments
= new ArrayList
<Comment
>();
76 URL url
= new URL(story
.getUrlInternal());
77 InputStream in
= open(url
);
78 Document doc
= DataUtil
.load(in
, "UTF-8", url
.toString());
79 Element listing
= doc
.getElementById("commentlisting");
80 if (listing
!= null) {
81 comments
.addAll(getComments(listing
));
87 private List
<Comment
> getComments(Element listing
) {
88 List
<Comment
> comments
= new ArrayList
<Comment
>();
89 for (Element commentElement
: listing
.children()) {
90 if (commentElement
.hasClass("comment")) {
91 Comment comment
= getComment(commentElement
);
92 if (!comment
.isEmpty()) {
93 comments
.add(comment
);
100 private Comment
getComment(Element commentElement
) {
101 String title
= firstOrEmpty(commentElement
, "title");
102 String author
= firstOrEmpty(commentElement
, "by");
103 String content
= firstOrEmpty(commentElement
, "commentBody");
104 String date
= firstOrEmpty(commentElement
, "otherdetails");
106 Comment comment
= new Comment(commentElement
.id(), author
, title
, date
,
109 for (Element child
: commentElement
.children()) {
110 if (child
.id().contains("commtree_")) {
111 comment
.addAll(getComments(child
));
118 private String
firstOrEmpty(Element element
, String className
) {
119 Elements subElements
= element
.getElementsByClass(className
);
120 if (subElements
.size() > 0) {
121 return subElements
.get(0).text();