1a62e8053e19b3c8b0c5b70e2dd4f2d0a514f87e
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / watchers / PirateBayWatcher.java
1 /*
2  * Rhynodge - PirateBayWatcher.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.HtmlFilter;
31 import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter;
32 import net.pterodactylus.rhynodge.filters.torrents.PirateBayFilter;
33 import net.pterodactylus.rhynodge.queries.FallbackQuery;
34 import net.pterodactylus.rhynodge.queries.HttpQuery;
35 import net.pterodactylus.rhynodge.triggers.NewTorrentTrigger;
36
37 import com.google.common.collect.ImmutableList;
38
39 /**
40  * {@link Watcher} implementation that watches The Pirate Bay for new files.
41  *
42  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43  */
44 public class PirateBayWatcher extends DefaultWatcher {
45
46         /**
47          * Creates a new Pirate Bay watcher.
48          *
49          * @param searchTerms
50          *            The terms to search for
51          */
52         public PirateBayWatcher(String searchTerms, String proxy) {
53                 super(createHttpQuery(searchTerms, extractProxyHost(proxy), extractProxyPort(proxy)), createFilters(), createTrigger());
54         }
55
56         private static String extractProxyHost(String proxy) {
57                 return proxy.split(":")[0];
58         }
59
60         private static int extractProxyPort(String proxy) {
61                 return Integer.valueOf(proxy.split(":")[1]);
62         }
63
64         //
65         // STATIC METHODS
66         //
67
68         /**
69          * Creates the query of the watcher.
70          *
71          * @param searchTerms
72          *            The search terms of the query
73          * @return The query of the watcher
74          */
75         private static Query createHttpQuery(String searchTerms, String proxyHost, int proxyPort) {
76                 try {
77                         HttpQuery hiddenServiceQuery = new HttpQuery("http://uj3wazyk5u4hnvtk.onion/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort);
78                         HttpQuery torQuery = new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort);
79                         HttpQuery plainInternetQuery = new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0");
80                         return new FallbackQuery(hiddenServiceQuery, torQuery, plainInternetQuery);
81                 } catch (UnsupportedEncodingException uee1) {
82                         /* will not happen. */
83                         return null;
84                 }
85         }
86
87         /**
88          * Creates the filters of the watcher.
89          *
90          * @return The filters of the watcher
91          */
92         private static List<Filter> createFilters() {
93                 return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter());
94         }
95
96         /**
97          * Creates the trigger of the watcher.
98          *
99          * @return The trigger of the watcher
100          */
101         private static Trigger createTrigger() {
102                 return new NewTorrentTrigger();
103         }
104
105 }