From: David ‘Bombe’ Roden Date: Sun, 21 Dec 2014 12:03:38 +0000 (+0100) Subject: Add filter to torrentz.eu result pages. X-Git-Tag: v2~217 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=71d2db823b40e782b0c89fc084fe9565d7f4f5e8;p=rhynodge.git Add filter to torrentz.eu result pages. --- diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilter.java new file mode 100644 index 0000000..57be6f0 --- /dev/null +++ b/src/main/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilter.java @@ -0,0 +1,57 @@ +package net.pterodactylus.rhynodge.filters.torrents; + +import net.pterodactylus.rhynodge.Filter; +import net.pterodactylus.rhynodge.filters.TorrentSiteFilter; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +/** + * {@link Filter} for {@code torrentz.eu} result pages. + * + * @author David ‘Bombe’ Roden + */ +public class TorrentzEuFilter extends TorrentSiteFilter { + + @Override + protected Elements getDataRows(Document document) { + return document.select("div.results dl"); + } + + @Override + protected String extractName(Element dataRow) { + return dataRow.select("dt a").text(); + } + + @Override + protected String extractSize(Element dataRow) { + return dataRow.select("dd span.s").text(); + } + + @Override + protected String extractMagnetUri(Element dataRow) { + return null; + } + + @Override + protected String extractDownloadUri(Element dataRow) { + return null; + } + + @Override + protected int extractFileCount(Element dataRow) { + return 0; + } + + @Override + protected int extractSeedCount(Element dataRow) { + return Integer.valueOf(dataRow.select("dd span.u").text()); + } + + @Override + protected int extractLeechCount(Element dataRow) { + return Integer.valueOf(dataRow.select("dd span.d").text()); + } + +} diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java new file mode 100644 index 0000000..4720d9b --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java @@ -0,0 +1,76 @@ +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 net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.Test; + +/** + * Unit test for {@link TorrentzEuFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class TorrentzEuFilterTest { + + private final TorrentzEuFilter torrentzEuFilter = new TorrentzEuFilter(); + + @Test + public void filterCanExtractAllTorrents() throws IOException { + Document document = Jsoup.parse(getClass().getResourceAsStream("torrentz-eu-results.html"), "UTF-8", "http://torrentz.eu/searchA?f=Ubuntu+ISO"); + HtmlState htmlState = new HtmlState("http://torrentz.eu/searchA?f=Ubuntu+ISO", document); + State newState = torrentzEuFilter.filter(htmlState); + MatcherAssert.assertThat(newState, Matchers.notNullValue()); + MatcherAssert.assertThat(newState, Matchers.instanceOf(TorrentState.class)); + TorrentState torrentState = (TorrentState) newState; + MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.hasSize(2)); + MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.contains( + isTorrent("Ubuntu 14 04 64Bit ISO File", "981 MB", 0, 1), + isTorrent("Ubuntu 14 04 LTS Desktop 64bit ISO", "964 MB", 75, 5) + )); + } + + private Matcher isTorrent(String name, String size, int seedCount, int leechCount) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(TorrentFile torrentFile, Description mismatchDescription) { + if (!torrentFile.name().equals(name)) { + mismatchDescription.appendText("name is ").appendValue(torrentFile.name()); + return false; + } + if (!torrentFile.size().equals(size)) { + mismatchDescription.appendText("size is ").appendValue(torrentFile.size()); + return false; + } + if (torrentFile.seedCount() != seedCount) { + mismatchDescription.appendText("seed count is ").appendValue(torrentFile.seedCount()); + return false; + } + if (torrentFile.leechCount() != leechCount) { + mismatchDescription.appendText("leech count is ").appendValue(torrentFile.leechCount()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("torrent named ").appendValue(name); + description.appendText(", size ").appendValue(size); + description.appendText(", seeds ").appendValue(seedCount); + description.appendText(", leechs ").appendValue(leechCount); + } + }; + } + +} diff --git a/src/test/resources/net/pterodactylus/rhynodge/filters/torrents/torrentz-eu-results.html b/src/test/resources/net/pterodactylus/rhynodge/filters/torrents/torrentz-eu-results.html new file mode 100644 index 0000000..0e0e17b --- /dev/null +++ b/src/test/resources/net/pterodactylus/rhynodge/filters/torrents/torrentz-eu-results.html @@ -0,0 +1,51 @@ + + + + + Ubuntu 14.04 64Bit ISO torrent + + + + + + + + + + + + + + + +
Age: 1d | 3d | 7d | 1m         Safety & Quality: any | good | verified

2 Torrents (0.008s) Save

Order by rating | date | size | peers

Ubuntu 14 04 64Bit ISO File » applications
01 month981 MB 01
+
Ubuntu 14 04 LTS Desktop 64bit ISO » applications
08 months964 MB 755
+
Not enough torrents?
- Check your spelling
- Try less or different keywords
- Try lower quality torrents