From: David ‘Bombe’ Roden Date: Wed, 5 Apr 2017 17:44:14 +0000 (+0200) Subject: Add blacklist filter for certain sizes X-Git-Tag: v2~107 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f1f117e50ebdd90f061aa6b0b6567a8f17ac6ff9;p=rhynodge.git Add blacklist filter for certain sizes --- diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsEpisodeWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsEpisodeWatcher.java index e086e75..ee8c1f3 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsEpisodeWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsEpisodeWatcher.java @@ -29,6 +29,7 @@ import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.Watcher; import net.pterodactylus.rhynodge.filters.EpisodeFilter; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.KickAssTorrentsFilter; import net.pterodactylus.rhynodge.queries.HttpQuery; import net.pterodactylus.rhynodge.triggers.NewEpisodeTrigger; @@ -79,7 +80,7 @@ public class KickAssTorrentsEpisodeWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter(), new EpisodeFilter()); + return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsWatcher.java index 41c3b6c..6fa449d 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsWatcher.java @@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.Watcher; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.KickAssTorrentsFilter; import net.pterodactylus.rhynodge.queries.HttpQuery; import net.pterodactylus.rhynodge.triggers.NewTorrentTrigger; @@ -77,7 +78,7 @@ public class KickAssTorrentsWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter()); + return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java index 44b93e1..4fec730 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java @@ -29,6 +29,7 @@ import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.Watcher; import net.pterodactylus.rhynodge.filters.EpisodeFilter; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.PirateBayFilter; import net.pterodactylus.rhynodge.queries.FallbackQuery; import net.pterodactylus.rhynodge.queries.HttpQuery; @@ -83,7 +84,7 @@ public class PirateBayEpisodeWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new EpisodeFilter()); + return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java index ef2ac11..1a62e80 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java @@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.Watcher; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.PirateBayFilter; import net.pterodactylus.rhynodge.queries.FallbackQuery; import net.pterodactylus.rhynodge.queries.HttpQuery; @@ -89,7 +90,7 @@ public class PirateBayWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter()); + return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundEpisodeWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundEpisodeWatcher.java index 8d9cda0..bc22f92 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundEpisodeWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundEpisodeWatcher.java @@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.filters.EpisodeFilter; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.TorrentHoundFilter; import net.pterodactylus.rhynodge.queries.HttpQuery; import net.pterodactylus.rhynodge.triggers.NewEpisodeTrigger; @@ -78,7 +79,7 @@ public class TorrentHoundEpisodeWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter(), new EpisodeFilter()); + return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundWatcher.java index f8dd1ef..4a9fba5 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundWatcher.java @@ -27,6 +27,7 @@ import net.pterodactylus.rhynodge.Filter; import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.TorrentHoundFilter; import net.pterodactylus.rhynodge.queries.HttpQuery; import net.pterodactylus.rhynodge.triggers.NewTorrentTrigger; @@ -77,7 +78,7 @@ public class TorrentHoundWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter()); + return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuEpisodeWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuEpisodeWatcher.java index 1c8f048..920d3bc 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuEpisodeWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuEpisodeWatcher.java @@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.filters.EpisodeFilter; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.TorrentzEuFilter; import net.pterodactylus.rhynodge.queries.HttpQuery; import net.pterodactylus.rhynodge.triggers.NewEpisodeTrigger; @@ -78,7 +79,7 @@ public class TorrentzEuEpisodeWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new TorrentzEuFilter(), createDefaultBlacklistFilter(), new EpisodeFilter()); + return ImmutableList.of(new HtmlFilter(), new TorrentzEuFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter()); } /** diff --git a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuWatcher.java b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuWatcher.java index cbf6849..a5192a3 100644 --- a/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuWatcher.java +++ b/src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuWatcher.java @@ -28,6 +28,7 @@ import net.pterodactylus.rhynodge.Query; import net.pterodactylus.rhynodge.Trigger; import net.pterodactylus.rhynodge.filters.EpisodeFilter; import net.pterodactylus.rhynodge.filters.HtmlFilter; +import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter; import net.pterodactylus.rhynodge.filters.torrents.TorrentzEuFilter; import net.pterodactylus.rhynodge.queries.HttpQuery; import net.pterodactylus.rhynodge.triggers.NewEpisodeTrigger; @@ -78,7 +79,7 @@ public class TorrentzEuWatcher extends DefaultWatcher { * @return The filters of the watcher */ private static List createFilters() { - return ImmutableList.of(new HtmlFilter(), new TorrentzEuFilter(), createDefaultBlacklistFilter()); + return ImmutableList.of(new HtmlFilter(), new TorrentzEuFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter()); } /** diff --git a/src/main/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilter.kt b/src/main/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilter.kt new file mode 100644 index 0000000..f7c2cc5 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilter.kt @@ -0,0 +1,18 @@ +package net.pterodactylus.rhynodge.filters + +import net.pterodactylus.rhynodge.Filter +import net.pterodactylus.rhynodge.State +import net.pterodactylus.rhynodge.states.FailedState +import net.pterodactylus.rhynodge.states.TorrentState + +/** + * Blacklist that filters torrents with a certain size. + */ +class SizeBlacklistFilter(private val blacklistedSizes: Iterable = listOf("313.97 MiB", "331.97 MiB")): Filter { + + override fun filter(state: State): State { + val torrentState = state as? TorrentState ?: return FailedState() + return TorrentState(torrentState.torrentFiles().filterNot { it.size() in blacklistedSizes }) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilterTest.kt b/src/test/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilterTest.kt new file mode 100644 index 0000000..4ab5430 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilterTest.kt @@ -0,0 +1,42 @@ +package net.pterodactylus.rhynodge.filters + +import net.pterodactylus.rhynodge.states.FailedState +import net.pterodactylus.rhynodge.states.TorrentState +import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile +import org.hamcrest.MatcherAssert +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers +import org.hamcrest.Matchers.equalTo +import org.junit.Test + +/** + * Unit test for [SizeBlacklistFilter]. + */ +class SizeBlacklistFilterTest { + + private val filter = SizeBlacklistFilter(listOf("123 MiB", "234 GiB")) + private val notMatchingTorrentFile1 = TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-3LT0N", "123 GiB", "magnet:?xt=a", "http://", 1, 2, 3) + private val notMatchingTorrentFile2 = TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-HELLRAZ0R", "123 MB", "magnet:?xt=b", "http://", 1, 2, 3) + private val matchingTorrentFile1 = TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-Haggebulle", "123 MiB", "magnet:?xt=c", "http://", 1, 2, 3) + private val matchingTorrentFile2 = TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-FooKaS", "234 GiB", "magnet:?xt=d", "http://", 1, 2, 3) + + @Test + fun `filter removes the correct torrents`() { + val filteredState = filter.filter(TorrentState(listOf( + notMatchingTorrentFile1, + notMatchingTorrentFile2, + matchingTorrentFile1, + matchingTorrentFile2 + ))) as TorrentState + assertThat(filteredState.torrentFiles(), Matchers.containsInAnyOrder( + notMatchingTorrentFile1, + notMatchingTorrentFile2 + )) + } + + @Test + fun `filter returns failed state for a failed state`() { + assertThat(filter.filter(FailedState()).success(), equalTo(false)) + } + +}