Add blacklist filter for certain sizes
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Apr 2017 17:44:14 +0000 (19:44 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 5 Apr 2017 17:44:14 +0000 (19:44 +0200)
src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsEpisodeWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/KickAssTorrentsWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayEpisodeWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/PirateBayWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundEpisodeWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/TorrentHoundWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuEpisodeWatcher.java
src/main/java/net/pterodactylus/rhynodge/watchers/TorrentzEuWatcher.java
src/main/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilter.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/rhynodge/filters/SizeBlacklistFilterTest.kt [new file with mode: 0644]

index e086e75..ee8c1f3 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter(), new EpisodeFilter());
+               return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter());
        }
 
        /**
index 41c3b6c..6fa449d 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter());
+               return ImmutableList.of(new HtmlFilter(), new KickAssTorrentsFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter());
        }
 
        /**
index 44b93e1..4fec730 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new EpisodeFilter());
+               return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter());
        }
 
        /**
index ef2ac11..1a62e80 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter());
+               return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter());
        }
 
        /**
index 8d9cda0..bc22f92 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter(), new EpisodeFilter());
+               return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter());
        }
 
        /**
index f8dd1ef..4a9fba5 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter());
+               return ImmutableList.of(new HtmlFilter(), new TorrentHoundFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter());
        }
 
        /**
index 1c8f048..920d3bc 100644 (file)
@@ -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<Filter> createFilters() {
-               return ImmutableList.of(new HtmlFilter(), new TorrentzEuFilter(), createDefaultBlacklistFilter(), new EpisodeFilter());
+               return ImmutableList.of(new HtmlFilter(), new TorrentzEuFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter());
        }
 
        /**
index cbf6849..a5192a3 100644 (file)
@@ -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<Filter> 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 (file)
index 0000000..f7c2cc5
--- /dev/null
@@ -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<String> = 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 (file)
index 0000000..4ab5430
--- /dev/null
@@ -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))
+       }
+
+}