| 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://pipedot.org/'>https://pipedot.org/</a>. |
| 19 | * |
| 20 | * @author niki |
| 21 | */ |
| 22 | public class Pipedot extends BasicSupport { |
| 23 | @Override |
| 24 | public String getDescription() { |
| 25 | return "Pipedot: News for nerds, without the corporate slant"; |
| 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://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) { |
| 39 | continue; |
| 40 | } |
| 41 | |
| 42 | Element title = titles.get(0); |
| 43 | |
| 44 | String id = ""; |
| 45 | for (Element idElem : story.getElementsByTag("a")) { |
| 46 | if (idElem.attr("href").startsWith("/pipe/")) { |
| 47 | id = idElem.attr("href").substring("/pipe/".length()); |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | String intUrl = null; |
| 53 | String extUrl = null; |
| 54 | |
| 55 | Elements links = story.getElementsByTag("a"); |
| 56 | if (links.size() > 0) { |
| 57 | intUrl = links.get(0).absUrl("href"); |
| 58 | } |
| 59 | |
| 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/")) { |
| 64 | extUrl = uuu; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | String details = ""; |
| 70 | Elements detailsElements = story.getElementsByTag("div"); |
| 71 | if (detailsElements.size() > 0) { |
| 72 | details = detailsElements.get(0).text(); |
| 73 | } |
| 74 | |
| 75 | String body = ""; |
| 76 | for (Element elem : story.children()) { |
| 77 | String tag = elem.tag().toString(); |
| 78 | if (!tag.equals("header") && !tag.equals("footer")) { |
| 79 | body = elem.text(); |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | list.add(new Story(getType(), id, title.text(), details, intUrl, |
| 85 | extUrl, body)); |
| 86 | } |
| 87 | |
| 88 | return list; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public List<Comment> getComments(Story story) throws IOException { |
| 93 | List<Comment> comments = new ArrayList<Comment>(); |
| 94 | |
| 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))); |
| 101 | } |
| 102 | |
| 103 | return comments; |
| 104 | } |
| 105 | |
| 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); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | return comments; |
| 117 | } |
| 118 | |
| 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"); |
| 123 | |
| 124 | String date = ""; |
| 125 | int pos = author.lastIndexOf(" on "); |
| 126 | if (pos >= 0) { |
| 127 | date = author.substring(pos + " on ".length()).trim(); |
| 128 | author = author.substring(0, pos).trim(); |
| 129 | } |
| 130 | |
| 131 | Comment comment = new Comment(commentElement.id(), author, title, date, |
| 132 | content); |
| 133 | |
| 134 | Elements commentOutline = commentElement |
| 135 | .getElementsByClass("comment-outline"); |
| 136 | if (commentOutline.size() > 0) { |
| 137 | comment.addAll(getComments(commentOutline.get(0))); |
| 138 | } |
| 139 | |
| 140 | return comment; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the first element of the given class, or an empty {@link String} if |
| 145 | * none found. |
| 146 | * |
| 147 | * @param element |
| 148 | * the element to look in |
| 149 | * @param className |
| 150 | * the class to look for |
| 151 | * |
| 152 | * @return the value or an empty {@link String} |
| 153 | */ |
| 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(); |
| 158 | } |
| 159 | |
| 160 | return ""; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get the first element of the given tag, or an empty {@link String} if |
| 165 | * none found. |
| 166 | * |
| 167 | * @param element |
| 168 | * the element to look in |
| 169 | * @param tagName |
| 170 | * the tag to look for |
| 171 | * |
| 172 | * @return the value or an empty {@link String} |
| 173 | */ |
| 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(); |
| 178 | } |
| 179 | |
| 180 | return ""; |
| 181 | } |
| 182 | } |