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 | ||
70b18499 NR |
17 | /** |
18 | * Support <a href='https://slashdot.org/'>https://slashdot.org/</a>. | |
19 | * | |
20 | * @author niki | |
21 | */ | |
73785268 NR |
22 | public class Slashdot extends BasicSupport { |
23 | @Override | |
24 | public String getDescription() { | |
25 | return "Slashdot: News for nerds, stuff that matters!"; | |
26 | } | |
27 | ||
28 | @Override | |
29 | public List<Story> list() throws IOException { | |
30 | List<Story> list = new ArrayList<Story>(); | |
31 | ||
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) { | |
39 | continue; | |
40 | } | |
70b18499 | 41 | |
73785268 NR |
42 | Element title = titles.get(0); |
43 | ||
44 | String id = "" + title.attr("id"); | |
45 | if (id.startsWith("title-")) { | |
46 | id = id.substring("title-".length()); | |
47 | } | |
48 | ||
49 | Elements links = title.getElementsByTag("a"); | |
50 | String intUrl = null; | |
51 | String extUrl = null; | |
52 | if (links.size() > 0) { | |
53 | intUrl = links.get(0).absUrl("href"); | |
54 | } | |
55 | if (links.size() > 1) { | |
56 | extUrl = links.get(1).absUrl("href"); | |
57 | } | |
58 | ||
59 | String details = ""; | |
60 | Elements detailsElements = story.getElementsByClass("details"); | |
61 | if (detailsElements.size() > 0) { | |
62 | details = detailsElements.get(0).text(); | |
63 | } | |
64 | ||
65 | String body = ""; | |
66 | Element bodyElement = doc.getElementById("text-" + id); | |
67 | if (bodyElement != null) { | |
68 | body = bodyElement.text(); | |
69 | } | |
70 | ||
71 | list.add(new Story(getType(), id, title.text(), details, intUrl, | |
72 | extUrl, body)); | |
73 | } | |
74 | ||
75 | return list; | |
76 | } | |
77 | ||
78 | @Override | |
5c056aad | 79 | public void fetch(Story story) throws IOException { |
73785268 NR |
80 | List<Comment> comments = new ArrayList<Comment>(); |
81 | ||
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)); | |
88 | } | |
89 | ||
5c056aad | 90 | story.setComments(comments); |
73785268 NR |
91 | } |
92 | ||
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); | |
100 | } | |
101 | } | |
102 | } | |
103 | return comments; | |
104 | } | |
105 | ||
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"); | |
111 | ||
112 | Comment comment = new Comment(commentElement.id(), author, title, date, | |
113 | content); | |
114 | ||
115 | for (Element child : commentElement.children()) { | |
116 | if (child.id().contains("commtree_")) { | |
117 | comment.addAll(getComments(child)); | |
118 | } | |
119 | } | |
120 | ||
121 | return comment; | |
122 | } | |
123 | ||
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(); | |
128 | } | |
129 | ||
130 | return ""; | |
131 | } | |
132 | } |