Commit | Line | Data |
---|---|---|
eaaeae39 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 | /** | |
18 | * Support <a href='https://lwn.net/'>https://lwn.net/</a>. | |
19 | * | |
20 | * @author niki | |
21 | */ | |
22 | public class LWN extends BasicSupport { | |
23 | @Override | |
24 | public String getDescription() { | |
25 | return "LWN: Linux Weekly Newsletter"; | |
26 | } | |
27 | ||
28 | @Override | |
29 | public List<Story> list() throws IOException { | |
30 | // TODO: comments + do not get comment for [$] stories | |
31 | // + update body on getComment (global change, also LinuxToday) | |
32 | ||
33 | List<Story> list = new ArrayList<Story>(); | |
34 | ||
35 | URL url = new URL("https://lwn.net/"); | |
36 | InputStream in = open(url); | |
37 | Document doc = DataUtil.load(in, "UTF-8", url.toString()); | |
38 | Elements stories = doc.getElementsByClass("pure-u-1"); | |
39 | for (Element story : stories) { | |
40 | Elements titles = story.getElementsByClass("Headline"); | |
41 | Elements listings = story.getElementsByClass("BlurbListing"); | |
42 | if (titles.size() == 0) { | |
43 | continue; | |
44 | } | |
45 | if (listings.size() == 0) { | |
46 | continue; | |
47 | } | |
48 | ||
49 | Element listing = listings.get(0); | |
50 | if (listing.children().size() < 2) { | |
51 | continue; | |
52 | } | |
53 | ||
54 | ||
55 | String title = titles.get(0).text(); | |
56 | String details = listing.children().get(0).text(); | |
25271075 NR |
57 | String body = ""; |
58 | // All but the first and two last children | |
59 | for (int i = 1 ; i < listing.children().size() - 2; i++) { | |
60 | Element e = listing.children().get(i); | |
61 | body = body.trim() + " " + e.text().trim(); | |
62 | } | |
63 | body = body.trim(); | |
eaaeae39 NR |
64 | |
65 | String author = ""; | |
66 | int pos = details.indexOf(" by "); | |
67 | if (pos >= 0) { | |
68 | author = details.substring(pos + " by ".length()).trim(); | |
69 | } | |
70 | ||
71 | String date = ""; | |
72 | pos = details.indexOf(" Posted "); | |
73 | if (pos >= 0) { | |
74 | date = details.substring(pos + " Posted ".length()).trim(); | |
75 | } | |
76 | ||
77 | ||
78 | String id = ""; | |
79 | String intUrl = ""; | |
80 | String extUrl = ""; | |
81 | for (Element idElem : story.getElementsByTag("a")) { | |
82 | // Last link is the story link | |
83 | intUrl = idElem.absUrl("href"); | |
84 | pos = intUrl.indexOf("#Comments"); | |
85 | if (pos >= 0) { | |
86 | intUrl = intUrl.substring(0, pos -1); | |
87 | } | |
88 | id = intUrl.replaceAll("[^0-9]", ""); | |
89 | } | |
90 | ||
91 | list.add(new Story(getType(), id, title, details, intUrl, extUrl, body)); | |
92 | } | |
93 | ||
94 | return list; | |
95 | } | |
96 | ||
97 | @Override | |
98 | public List<Comment> getComments(Story story) throws IOException { | |
99 | List<Comment> comments = new ArrayList<Comment>(); | |
100 | ||
101 | /* | |
102 | URL url = new URL(story.getUrlInternal()); | |
103 | InputStream in = open(url); | |
104 | Document doc = DataUtil.load(in, "UTF-8", url.toString()); | |
105 | Elements listing = doc.getElementsByTag("main"); | |
106 | if (listing.size() > 0) { | |
107 | comments.addAll(getComments(listing.get(0))); | |
108 | } | |
109 | */ | |
110 | ||
111 | return comments; | |
112 | } | |
113 | ||
114 | private List<Comment> getComments(Element listing) { | |
115 | List<Comment> comments = new ArrayList<Comment>(); | |
116 | for (Element commentElement : listing.children()) { | |
117 | if (commentElement.hasClass("comment")) { | |
118 | Comment comment = getComment(commentElement); | |
119 | if (!comment.isEmpty()) { | |
120 | comments.add(comment); | |
121 | } | |
122 | } | |
123 | } | |
124 | return comments; | |
125 | } | |
126 | ||
127 | private Comment getComment(Element commentElement) { | |
128 | String title = firstOrEmptyTag(commentElement, "h3"); | |
129 | String author = firstOrEmpty(commentElement, "h4"); | |
130 | String content = firstOrEmpty(commentElement, "comment-body"); | |
131 | ||
132 | String date = ""; | |
133 | int pos = author.lastIndexOf(" on "); | |
134 | if (pos >= 0) { | |
135 | date = author.substring(pos + " on ".length()).trim(); | |
136 | author = author.substring(0, pos).trim(); | |
137 | } | |
138 | ||
139 | Comment comment = new Comment(commentElement.id(), author, title, date, | |
140 | content); | |
141 | ||
142 | Elements commentOutline = commentElement | |
143 | .getElementsByClass("comment-outline"); | |
144 | if (commentOutline.size() > 0) { | |
145 | comment.addAll(getComments(commentOutline.get(0))); | |
146 | } | |
147 | ||
148 | return comment; | |
149 | } | |
150 | ||
151 | /** | |
152 | * Get the first element of the given class, or an empty {@link String} if | |
153 | * none found. | |
154 | * | |
155 | * @param element | |
156 | * the element to look in | |
157 | * @param className | |
158 | * the class to look for | |
159 | * | |
160 | * @return the value or an empty {@link String} | |
161 | */ | |
162 | private String firstOrEmpty(Element element, String className) { | |
163 | Elements subElements = element.getElementsByClass(className); | |
164 | if (subElements.size() > 0) { | |
165 | return subElements.get(0).text(); | |
166 | } | |
167 | ||
168 | return ""; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Get the first element of the given tag, or an empty {@link String} if | |
173 | * none found. | |
174 | * | |
175 | * @param element | |
176 | * the element to look in | |
177 | * @param tagName | |
178 | * the tag to look for | |
179 | * | |
180 | * @return the value or an empty {@link String} | |
181 | */ | |
182 | private String firstOrEmptyTag(Element element, String tagName) { | |
183 | Elements subElements = element.getElementsByTag(tagName); | |
184 | if (subElements.size() > 0) { | |
185 | return subElements.get(0).text(); | |
186 | } | |
187 | ||
188 | return ""; | |
189 | } | |
190 | } |