From: David ‘Bombe’ Roden Date: Sun, 21 Dec 2014 20:43:39 +0000 (+0100) Subject: Add filter for TorrentHound results page. X-Git-Tag: v2~214 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e44fbb6956244559cd960d2ac2e0dabea047bdf1;p=rhynodge.git Add filter for TorrentHound results page. --- diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/torrents/TorrentHoundFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/torrents/TorrentHoundFilter.java new file mode 100644 index 0000000..4016a45 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/filters/torrents/TorrentHoundFilter.java @@ -0,0 +1,74 @@ +package net.pterodactylus.rhynodge.filters.torrents; + +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; + +import net.pterodactylus.rhynodge.filters.TorrentSiteFilter; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.nodes.Node; +import org.jsoup.parser.Tag; +import org.jsoup.select.Elements; + +/** + * {@link TorrentSiteFilter} implementation that can parse TorrentHound + * results. + * + * @author David ‘Bombe’ Roden + */ +public class TorrentHoundFilter extends TorrentSiteFilter { + + @Override + protected Elements getDataRows(Document document) { + return document.select("table.searchtable:gt(0) tr:gt(0)"); + } + + @Override + protected String extractName(Element dataRow) { + return dataRow.select("td > a").stream().map(element -> { + AtomicBoolean foundBr = new AtomicBoolean(false); + List nodes = element.childNodes().stream().filter(textNode -> { + if (textNode.nodeName().equals("br")) { + foundBr.set(true); + } + return !foundBr.get(); + }).collect(Collectors.toList()); + Element e = new Element(Tag.valueOf("span"), ""); + nodes.stream().forEach(node -> e.appendChild(node)); + return e.text(); + }).findFirst().get(); + } + + @Override + protected String extractSize(Element dataRow) { + return dataRow.select("span.size").text(); + } + + @Override + protected String extractMagnetUri(Element dataRow) { + return dataRow.select("div.sfloat a[title=Magnet download]").attr("href"); + } + + @Override + protected String extractDownloadUri(Element dataRow) { + return dataRow.select("div.sfloat a[title=.torrent download]").attr("href"); + } + + @Override + protected int extractFileCount(Element dataRow) { + return 0; + } + + @Override + protected int extractSeedCount(Element dataRow) { + return Integer.valueOf(dataRow.select("span.seeds").text().replaceAll("[^0-9]", "")); + } + + @Override + protected int extractLeechCount(Element dataRow) { + return Integer.valueOf(dataRow.select("span.leeches").text().replaceAll("[^0-9]", "")); + } + +} diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentHoundFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentHoundFilterTest.java new file mode 100644 index 0000000..499999b --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentHoundFilterTest.java @@ -0,0 +1,40 @@ +package net.pterodactylus.rhynodge.filters.torrents; + +import java.io.IOException; + +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.states.HtmlState; +import net.pterodactylus.rhynodge.states.TorrentState; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.Test; + +/** + * @author David ‘Bombe’ Roden + */ +public class TorrentHoundFilterTest { + + private final TorrentHoundFilter filter = new TorrentHoundFilter(); + + @Test + public void canParseResults() throws IOException { + Document document = Jsoup.parse(getClass().getResourceAsStream("torrent-hound-results.html"), "UTF-8", "http://www.torrenthound.com/search/1/Ubuntu+14+04+amd64+ISO/added:desc"); + HtmlState htmlState = new HtmlState("http://www.torrenthound.com/search/1/Ubuntu+14+04+amd64+ISO/added:desc", document); + State newState = filter.filter(htmlState); + MatcherAssert.assertThat(newState, Matchers.notNullValue()); + MatcherAssert.assertThat(newState, Matchers.instanceOf(TorrentState.class)); + TorrentState torrentState = (TorrentState) newState; + MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.hasSize(5)); + MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.contains( + TorrentMatcher.isTorrent("Ubuntu 14 04 1 server amd64 ISO", "572.0 Mb", "magnet:?xl=599785472&xt=urn:btih:2d066c94480adcf52bfd1185a75eb4ddc1777673&dn=ubuntu+14+04+1+server+amd64+iso&tr=udp%3A%2F%2Ftracker.istole.it%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80", 5, 3), + TorrentMatcher.isTorrent("Ubuntu 14 04 1 desktop amd64 ISO", "981.0 Mb", 34, 23), + TorrentMatcher.isTorrent("Ubuntu 14 04 1 desktop amd64+mac ISO", "979.0 Mb", 1, 0), + TorrentMatcher.isTorrent("Ubuntu gnome 14 04 1 desktop amd64 ISO", "935.0 Mb", 1, 1), + TorrentMatcher.isTorrent("Ubuntu gnome 14.04 beta1 desktop amd64.ISO", "890.0 Mb", 2, 0) + )); + } + +} diff --git a/src/test/resources/net/pterodactylus/rhynodge/filters/torrents/torrent-hound-results.html b/src/test/resources/net/pterodactylus/rhynodge/filters/torrents/torrent-hound-results.html new file mode 100644 index 0000000..c1ac471 --- /dev/null +++ b/src/test/resources/net/pterodactylus/rhynodge/filters/torrents/torrent-hound-results.html @@ -0,0 +1,260 @@ + + + + + + + + + + + + + + + Ubuntu 14 04 Amd64 ISO Torrent results + + + +
+ +
+ + +
+ + + + + +
+ layout

Ubuntu 14 04 amd64 ISO Torrents

5 torrents foundlayout
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Direct Downloads Sort DescendingAddedRatiotHR
Ubuntu 14 04 1 server amd64 ISO
Sponsored > New Releases
Wednesday
3.6 days old
Direct98%
Ubuntu 14 04 1 desktop amd64 ISO
Sponsored > New Releases
Today
54.6 mins old
Direct100%
Ubuntu 14 04 1 desktop amd64+mac ISO
Sponsored > New Releases
Today
9.6 hrs old
Direct99%
Ubuntu gnome 14 04 1 desktop amd64 ISO
Sponsored > New Releases
Tuesday
4.6 days old
Direct100%
Ubuntu 14 04 amd64 ISO torrent
Sponsored > Unsorted
Today
7.3 hrs old
812↑ 194↓75%
*sponsored results shown above

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Torrent Title
Magnet - Torrent
Sort DescendingAddedSizeRatiotHR
+
+ + +
Ubuntu 14 04 1 server amd64 ISO
Applications
25th Jul, 2014
21.3 wks old
572.0 Mb5↑ 3↓NA
+
+ + +
Ubuntu 14 04 1 desktop amd64 ISO
Applications
25th Jul, 2014
21.3 wks old
981.0 Mb34↑ 23↓NA
+
+ + +
Ubuntu 14 04 1 desktop amd64+mac ISO
Applications
25th Jul, 2014
21.3 wks old
979.0 Mb1↑ 0↓NA
+
+ + +
Ubuntu gnome 14 04 1 desktop amd64 ISO
Applications
25th Jul, 2014
21.3 wks old
935.0 Mb1↑ 1↓NA
+
+ + +
Ubuntu gnome 14.04 beta1 desktop amd64.ISO
Applications
14th Apr, 2014
35.8 wks old
890.0 Mb2↑ 0↓NA
+ +
+
+
    +
  • « Previous
  •     [ 1 ]    
  • Next »
  • +
+
+
+ +
+ + +
+ +
+
+ + +
+ +
+
+ + + + + +