Add Pirate Bay watcher for “normal” files.
[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 java.io.UnsupportedEncodingException;
21 import java.net.URLEncoder;
22 import java.util.List;
23
24 import net.pterodactylus.rhynodge.Filter;
25 import net.pterodactylus.rhynodge.Query;
26 import net.pterodactylus.rhynodge.Trigger;
27 import net.pterodactylus.rhynodge.filters.HtmlFilter;
28 import net.pterodactylus.rhynodge.filters.PirateBayFilter;
29 import net.pterodactylus.rhynodge.queries.HttpQuery;
30 import net.pterodactylus.rhynodge.triggers.NewTorrentTrigger;
31
32 import com.google.common.collect.ImmutableList;
33
34 /**
35  * {@Watcher} implementation that watches The Pirate Bay for new files.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public class PirateBayWatcher extends DefaultWatcher {
40
41         /**
42          * Creates a new Pirate Bay watcher.
43          *
44          * @param searchTerms
45          *            The terms to search for
46          */
47         public PirateBayWatcher(String searchTerms) {
48                 super(createHttpQuery(searchTerms), createFilters(), createTrigger());
49         }
50
51         //
52         // STATIC METHODS
53         //
54
55         /**
56          * Creates the query of the watcher.
57          *
58          * @param searchTerms
59          *            The search terms of the query
60          * @return The query of the watcher
61          */
62         private static Query createHttpQuery(String searchTerms) {
63                 try {
64                         return new HttpQuery("http://thepiratebay.se/search/" + URLEncoder.encode(searchTerms, "UTF-8") + "/0/3/0");
65                 } catch (UnsupportedEncodingException uee1) {
66                         /* will not happen. */
67                         return null;
68                 }
69         }
70
71         /**
72          * Creates the filters of the watcher.
73          *
74          * @return The filters of the watcher
75          */
76         private static List<Filter> createFilters() {
77                 return ImmutableList.<Filter> of(new HtmlFilter(), new PirateBayFilter());
78         }
79
80         /**
81          * Creates the trigger of the watcher.
82          *
83          * @return The trigger of the watcher
84          */
85         private static Trigger createTrigger() {
86                 return new NewTorrentTrigger();
87         }
88
89 }