2 * Rhynodge - PirateBayWatcher.java - Copyright © 2013 David Roden
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.
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.
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/>.
18 package net.pterodactylus.rhynodge.watchers;
20 import static net.pterodactylus.rhynodge.filters.BlacklistFilter.createDefaultBlacklistFilter;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URLEncoder;
24 import java.util.List;
26 import net.pterodactylus.rhynodge.Filter;
27 import net.pterodactylus.rhynodge.Query;
28 import net.pterodactylus.rhynodge.Watcher;
29 import net.pterodactylus.rhynodge.filters.HtmlFilter;
30 import net.pterodactylus.rhynodge.filters.SizeBlacklistFilter;
31 import net.pterodactylus.rhynodge.filters.torrents.PirateBayFilter;
32 import net.pterodactylus.rhynodge.mergers.TorrentMerger;
33 import net.pterodactylus.rhynodge.queries.FallbackQuery;
34 import net.pterodactylus.rhynodge.queries.HttpQuery;
36 import com.google.common.collect.ImmutableList;
39 * {@link Watcher} implementation that watches The Pirate Bay for new files.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class PirateBayWatcher extends DefaultWatcher {
46 * Creates a new Pirate Bay watcher.
49 * The terms to search for
51 public PirateBayWatcher(String searchTerms, String proxy) {
52 super(createHttpQuery(searchTerms, extractProxyHost(proxy), extractProxyPort(proxy)), createFilters(), new TorrentMerger());
55 private static String extractProxyHost(String proxy) {
56 return proxy.split(":")[0];
59 private static int extractProxyPort(String proxy) {
60 return Integer.valueOf(proxy.split(":")[1]);
68 * Creates the query of the watcher.
71 * The search terms of the query
72 * @return The query of the watcher
74 private static Query createHttpQuery(String searchTerms, String proxyHost, int proxyPort) {
76 HttpQuery hiddenServiceQuery = new HttpQuery("http://uj3wazyk5u4hnvtk.onion/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort);
77 HttpQuery torQuery = new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0", proxyHost, proxyPort);
78 HttpQuery plainInternetQuery = new HttpQuery("http://thepiratebay.org/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0");
79 return new FallbackQuery(hiddenServiceQuery, torQuery, plainInternetQuery);
80 } catch (UnsupportedEncodingException uee1) {
81 /* will not happen. */
87 * Creates the filters of the watcher.
89 * @return The filters of the watcher
91 private static List<Filter> createFilters() {
92 return ImmutableList.of(new HtmlFilter(), new PirateBayFilter(), createDefaultBlacklistFilter(), new SizeBlacklistFilter());