Add blacklist filter.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Oct 2013 13:35:16 +0000 (14:35 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 27 Oct 2013 13:39:06 +0000 (14:39 +0100)
pom.xml
src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java [new file with mode: 0644]
src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 4fd28a0..cbbb5b9 100644 (file)
--- a/pom.xml
+++ b/pom.xml
 
        <dependencies>
                <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <version>4.11</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.hamcrest</groupId>
+                       <artifactId>hamcrest-library</artifactId>
+                       <version>1.3</version>
+                       <scope>test</scope>
+               </dependency>
+               <dependency>
                        <groupId>com.google.guava</groupId>
                        <artifactId>guava</artifactId>
                        <version>14.0-rc1</version>
diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/BlacklistFilter.java
new file mode 100644 (file)
index 0000000..8ce25d6
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * rhynodge - BlacklistFilter.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.rhynodge.filters;
+
+import static com.google.common.base.Preconditions.checkState;
+import static com.google.common.collect.FluentIterable.from;
+import static java.util.Arrays.asList;
+
+import java.util.List;
+
+import net.pterodactylus.rhynodge.Filter;
+import net.pterodactylus.rhynodge.State;
+import net.pterodactylus.rhynodge.states.FailedState;
+import net.pterodactylus.rhynodge.states.TorrentState;
+import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
+
+import com.google.common.base.Predicate;
+
+/**
+ * Filter for {@link TorrentState}s that removes all {@link TorrentFile}s whose
+ * names match a list of bad words.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BlacklistFilter implements Filter {
+
+       private final Iterable<String> filterWords;
+
+       public BlacklistFilter(List<String> filterWords) {
+               this.filterWords = filterWords;
+       }
+
+       @Override
+       public State filter(State state) {
+               if (!state.success()) {
+                       return FailedState.from(state);
+               }
+               checkState(state instanceof TorrentState, "state is not a TorrentState but a %s!", state.getClass());
+
+               TorrentState torrentState = (TorrentState) state;
+               return new TorrentState(from(torrentState.torrentFiles()).filter(new Predicate<TorrentFile>() {
+                       @Override
+                       public boolean apply(TorrentFile torrentFile) {
+                               return (torrentFile == null) ? false : nameDoesNotMatchAFilterWord(torrentFile.name());
+                       }
+
+                       private boolean nameDoesNotMatchAFilterWord(final String name) {
+                               return !from(filterWords).anyMatch(new Predicate<String>() {
+                                       @Override
+                                       public boolean apply(String word) {
+                                               return name.toLowerCase().contains(word.toLowerCase());
+                                       }
+                               });
+                       }
+               }).toList());
+       }
+
+       public static BlacklistFilter createDefaultBlacklistFilter() {
+               return new BlacklistFilter(asList("-juggs", "-MAX", "-3LT0N", "-Haggebulle", "-FooKaS", "-HELLRAZ0R", "-PLAYNOW", "-UnKnOwN", "-EDAW", "-NYDIC", "-TASTE", "-AQOS", "-AMIABLE", "-NoGRP", "-TAMILROCKERS", "-ADTRG", "-LEGi0N", "-DiRTYMARY", "-SUFFiCE", "-CM", "-AXED", "-AN0NYM0US", "-S4A", "-MiLLENiUM"));
+       }
+
+}
diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/BlacklistFilterTest.java
new file mode 100644 (file)
index 0000000..ed8e3a3
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * rhynodge - BlacklistFilterTest.java - Copyright © 2013 David Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.rhynodge.filters;
+
+import static java.util.Arrays.asList;
+import static net.pterodactylus.rhynodge.filters.BlacklistFilter.createDefaultBlacklistFilter;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+import net.pterodactylus.rhynodge.State;
+import net.pterodactylus.rhynodge.states.FailedState;
+import net.pterodactylus.rhynodge.states.TorrentState;
+import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link BlacklistFilter}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class BlacklistFilterTest {
+
+       private final BlacklistFilter blacklistFilter = new BlacklistFilter(asList("-Haggebulle", "-FooKaS"));
+       private final TorrentFile notMatchingTorrentFile1 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-3LT0N", "1.1G", "magnet:?xt=a", "http://", 1, 2, 3);
+       private final TorrentFile notMatchingTorrentFile2 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-HELLRAZ0R", "1.1G", "magnet:?xt=b", "http://", 1, 2, 3);
+       private final TorrentFile matchingTorrentFile1 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-Haggebulle", "1.1G", "magnet:?xt=c", "http://", 1, 2, 3);
+       private final TorrentFile matchingTorrentFile2 = new TorrentFile("The Vampire Diaries S05E05 2013 HDRip 720p-FooKaS", "1.1G", "magnet:?xt=d", "http://", 1, 2, 3);
+
+       @Test
+       public void verifyThatMatchingTorrentsAreRemoved() {
+               TorrentState torrentState = new TorrentState();
+               torrentState.addTorrentFile(matchingTorrentFile1);
+               torrentState.addTorrentFile(notMatchingTorrentFile1);
+               torrentState.addTorrentFile(matchingTorrentFile2);
+               torrentState.addTorrentFile(notMatchingTorrentFile2);
+               TorrentState filteredState = (TorrentState) blacklistFilter.filter(torrentState);
+               assertThat(filteredState, notNullValue());
+               assertThat(filteredState.torrentFiles(), containsInAnyOrder(notMatchingTorrentFile1, notMatchingTorrentFile2));
+       }
+
+       @Test
+       public void verifyThatAFailedStateResultsInAFailedState() {
+               State state = blacklistFilter.filter(new FailedState());
+               assertThat(state, notNullValue());
+               assertThat(state.success(), is(false));
+       }
+
+       @Test
+       public void verifyThatABlacklistFilterIsCreated() {
+               assertThat(createDefaultBlacklistFilter(), notNullValue());
+       }
+
+}