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://pipedot.org/'>https://pipedot.org/</a>.
22 public class Pipedot
extends BasicSupport
{
24 public String
getDescription() {
25 return "Pipedot: News for nerds, without the corporate slant";
29 public List
<Story
> list() throws IOException
{
30 List
<Story
> list
= new ArrayList
<Story
>();
32 URL url
= new URL("https://pipedot.org/");
33 InputStream in
= open(url
);
34 Document doc
= DataUtil
.load(in
, "UTF-8", url
.toString());
35 Elements stories
= doc
.getElementsByClass("story");
36 for (Element story
: stories
) {
37 Elements titles
= story
.getElementsByTag("h1");
38 if (titles
.size() == 0) {
42 Element title
= titles
.get(0);
45 for (Element idElem
: story
.getElementsByTag("a")) {
46 if (idElem
.attr("href").startsWith("/pipe/")) {
47 id
= idElem
.attr("href").substring("/pipe/".length());
55 Elements links
= story
.getElementsByTag("a");
56 if (links
.size() > 0) {
57 intUrl
= links
.get(0).absUrl("href");
60 // Take first ext URL as original source
61 for (Element link
: links
) {
62 String uuu
= link
.absUrl("href");
63 if (!uuu
.isEmpty() && !uuu
.contains("pipedot.org/")) {
70 Elements detailsElements
= story
.getElementsByTag("div");
71 if (detailsElements
.size() > 0) {
72 details
= detailsElements
.get(0).text();
76 for (Element elem
: story
.children()) {
77 String tag
= elem
.tag().toString();
78 if (!tag
.equals("header") && !tag
.equals("footer")) {
84 list
.add(new Story(getType(), id
, title
.text(), details
, intUrl
,
92 public List
<Comment
> getComments(Story story
) throws IOException
{
93 List
<Comment
> comments
= new ArrayList
<Comment
>();
95 URL url
= new URL(story
.getUrlInternal());
96 InputStream in
= open(url
);
97 Document doc
= DataUtil
.load(in
, "UTF-8", url
.toString());
98 Elements listing
= doc
.getElementsByTag("main");
99 if (listing
.size() > 0) {
100 comments
.addAll(getComments(listing
.get(0)));
106 private List
<Comment
> getComments(Element listing
) {
107 List
<Comment
> comments
= new ArrayList
<Comment
>();
108 for (Element commentElement
: listing
.children()) {
109 if (commentElement
.hasClass("comment")) {
110 Comment comment
= getComment(commentElement
);
111 if (!comment
.isEmpty()) {
112 comments
.add(comment
);
119 private Comment
getComment(Element commentElement
) {
120 String title
= firstOrEmptyTag(commentElement
, "h3");
121 String author
= firstOrEmpty(commentElement
, "h4");
122 String content
= firstOrEmpty(commentElement
, "comment-body");
125 int pos
= author
.lastIndexOf(" on ");
127 date
= author
.substring(pos
+ " on ".length()).trim();
128 author
= author
.substring(0, pos
).trim();
131 Comment comment
= new Comment(commentElement
.id(), author
, title
, date
,
134 Elements commentOutline
= commentElement
135 .getElementsByClass("comment-outline");
136 if (commentOutline
.size() > 0) {
137 comment
.addAll(getComments(commentOutline
.get(0)));
144 * Get the first element of the given class, or an empty {@link String} if
148 * the element to look in
150 * the class to look for
152 * @return the value or an empty {@link String}
154 private String
firstOrEmpty(Element element
, String className
) {
155 Elements subElements
= element
.getElementsByClass(className
);
156 if (subElements
.size() > 0) {
157 return subElements
.get(0).text();
164 * Get the first element of the given tag, or an empty {@link String} if
168 * the element to look in
170 * the tag to look for
172 * @return the value or an empty {@link String}
174 private String
firstOrEmptyTag(Element element
, String tagName
) {
175 Elements subElements
= element
.getElementsByTag(tagName
);
176 if (subElements
.size() > 0) {
177 return subElements
.get(0).text();