4fec73013cb0b5f675d59279c626902e2699d653
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / watchers / PirateBayEpisodeWatcher.java
1 /*
2  * Rhynodge - PirateBayEpisodeWatcher.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.rhynodge.watchers;
19
20 import static net.pterodactylus.rhynodge.filters.BlacklistFilter.createDefaultBlacklistFilter;
21
22 import java.io.UnsupportedEncodingException;
23 import java.net.URLEncoder;
24 import java.util.List;
25
26 import net.pterodactylus.rhynodge.Filter;
27 import net.pterodactylus.rhynodge.Query;
28 import net.pterodactylus.rhynodge.Trigger;
29 import net.pterodactylus.rhynodge.Watcher;
30 import net.pterodactylus.rhynodge.filters.EpisodeFilter;
31 import net.pterodactylus.rhynodge.filters.HtmlFilter;
32 import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter;
33 import net.pterodactylus.rhynodge.filters.torrents.PirateBayFilter;
34 import net.pterodactylus.rhynodge.queries.FallbackQuery;
35 import net.pterodactylus.rhynodge.queries.HttpQuery;
36 import net.pterodactylus.rhynodge.triggers.NewEpisodeTrigger;
37
38 import com.google.common.collect.ImmutableList;
39
40 /**
41  * {@link Watcher} implementation that watches The Pirate Bay for new episodes.
42  *
43  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44  */
45 public class PirateBayEpisodeWatcher extends DefaultWatcher {
46
47         /**
48          * Creates a new Pirate Bay episode watcher.
49          *
50          * @param searchTerms
51          *            The terms to search for
52          */
53         public PirateBayEpisodeWatcher(String searchTerms, String proxy) {
54                 super(createHttpQuery(searchTerms, extractProxyHost(proxy), extractProxyPort(proxy)), createFilters(), createTrigger());
55         }
56
57         private static String extractProxyHost(String proxy) {
58                 return proxy.split(":")[0];
59         }
60
61         private static int extractProxyPort(String proxy) {
62                 return Integer.valueOf(proxy.split(":")[1]);
63         }
64
65         //
66         // STATIC METHODS
67         //
68
69         private static Query createHttpQuery(String searchTerms, String proxyHost, int proxyPort) {
70                 try {
71                         HttpQuery hiddenServiceQuery = new HttpQuery("http://uj3wazyk5u4hnvtk.onion/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort);
72                         HttpQuery torQuery = new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort);
73                         HttpQuery plainInternetQuery = new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0");
74                         return new FallbackQuery(hiddenServiceQuery, torQuery, plainInternetQuery);
75                 } catch (UnsupportedEncodingException uee1) {
76                         /* will not happen. */
77                         return null;
78                 }
79         }
80
81         /**
82          * Creates the filters of the watcher.
83          *
84          * @return The filters of the watcher
85          */
86         private static List<Filter> createFilters() {
87                 return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter(), new EpisodeFilter());
88         }
89
90         /**
91          * Creates the trigger of the watcher.
92          *
93          * @return The trigger of the watcher
94          */
95         private static Trigger createTrigger() {
96                 return new NewEpisodeTrigger();
97         }
98
99 }