Commit | Line | Data |
---|---|---|
73785268 NR |
1 | package be.nikiroo.gofetch.support; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
5 | import java.net.URL; | |
6 | import java.util.ArrayList; | |
7 | import java.util.List; | |
8 | ||
9 | import org.jsoup.helper.DataUtil; | |
10 | import org.jsoup.nodes.Document; | |
11 | import org.jsoup.nodes.Element; | |
12 | import org.jsoup.select.Elements; | |
13 | ||
14 | import be.nikiroo.gofetch.data.Comment; | |
15 | import be.nikiroo.gofetch.data.Story; | |
16 | ||
17 | public class Slashdot extends BasicSupport { | |
18 | @Override | |
19 | public String getDescription() { | |
20 | return "Slashdot: News for nerds, stuff that matters!"; | |
21 | } | |
22 | ||
23 | @Override | |
24 | public List<Story> list() throws IOException { | |
25 | List<Story> list = new ArrayList<Story>(); | |
26 | ||
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) { | |
34 | continue; | |
35 | } | |
36 | Element title = titles.get(0); | |
37 | ||
38 | String id = "" + title.attr("id"); | |
39 | if (id.startsWith("title-")) { | |
40 | id = id.substring("title-".length()); | |
41 | } | |
42 | ||
43 | Elements links = title.getElementsByTag("a"); | |
44 | String intUrl = null; | |
45 | String extUrl = null; | |
46 | if (links.size() > 0) { | |
47 | intUrl = links.get(0).absUrl("href"); | |
48 | } | |
49 | if (links.size() > 1) { | |
50 | extUrl = links.get(1).absUrl("href"); | |
51 | } | |
52 | ||
53 | String details = ""; | |
54 | Elements detailsElements = story.getElementsByClass("details"); | |
55 | if (detailsElements.size() > 0) { | |
56 | details = detailsElements.get(0).text(); | |
57 | } | |
58 | ||
59 | String body = ""; | |
60 | Element bodyElement = doc.getElementById("text-" + id); | |
61 | if (bodyElement != null) { | |
62 | body = bodyElement.text(); | |
63 | } | |
64 | ||
65 | list.add(new Story(getType(), id, title.text(), details, intUrl, | |
66 | extUrl, body)); | |
67 | } | |
68 | ||
69 | return list; | |
70 | } | |
71 | ||
72 | @Override | |
73 | public List<Comment> getComments(Story story) throws IOException { | |
74 | List<Comment> comments = new ArrayList<Comment>(); | |
75 | ||
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)); | |
82 | } | |
83 | ||
84 | return comments; | |
85 | } | |
86 | ||
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); | |
94 | } | |
95 | } | |
96 | } | |
97 | return comments; | |
98 | } | |
99 | ||
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"); | |
105 | ||
106 | Comment comment = new Comment(commentElement.id(), author, title, date, | |
107 | content); | |
108 | ||
109 | for (Element child : commentElement.children()) { | |
110 | if (child.id().contains("commtree_")) { | |
111 | comment.addAll(getComments(child)); | |
112 | } | |
113 | } | |
114 | ||
115 | return comment; | |
116 | } | |
117 | ||
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(); | |
122 | } | |
123 | ||
124 | return ""; | |
125 | } | |
126 | } |