Annotate Filter interface with @NotNull
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / TorrentSiteFilter.java
1 /*
2  * Rhynodge - KickAssTorrentsFilter.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.filters;
19
20 import static com.google.common.base.Preconditions.checkState;
21
22 import java.io.UnsupportedEncodingException;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import java.net.URLEncoder;
26
27 import net.pterodactylus.rhynodge.Filter;
28 import net.pterodactylus.rhynodge.State;
29 import net.pterodactylus.rhynodge.queries.HttpQuery;
30 import net.pterodactylus.rhynodge.states.FailedState;
31 import net.pterodactylus.rhynodge.states.HtmlState;
32 import net.pterodactylus.rhynodge.states.TorrentState;
33 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
34
35 import org.jetbrains.annotations.NotNull;
36 import org.jsoup.nodes.Document;
37 import org.jsoup.nodes.Element;
38 import org.jsoup.select.Elements;
39
40 /**
41  * {@link Filter} implementation that parses a {@link TorrentState} from an
42  * {@link HtmlState} which was generated by a {@link HttpQuery} to
43  * {@code kickasstorrents.ph}.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public abstract class TorrentSiteFilter implements Filter {
48
49         /**
50          * {@inheritDoc}
51          */
52         @NotNull
53         @Override
54         public State filter(@NotNull State state) {
55                 if (!state.success()) {
56                         return FailedState.from(state);
57                 }
58                 checkState(state instanceof HtmlState, "state is not an HtmlState but a %s", state.getClass().getName());
59
60                 /* get result table. */
61                 Document document = ((HtmlState) state).document();
62
63                 /* iterate over all rows. */
64                 Elements dataRows = getDataRows(document);
65                 TorrentState torrentState = new TorrentState();
66                 for (Element dataRow : dataRows) {
67                         String name = extractName(dataRow);
68                         String size = extractSize(dataRow);
69                         String magnetUri = extractMagnetUri(dataRow);
70                         String downloadUri = extractDownloadUri(dataRow);
71                         int fileCount = extractFileCount(dataRow);
72                         int seedCount = extractSeedCount(dataRow);
73                         int leechCount = extractLeechCount(dataRow);
74                         try {
75                                 if ((downloadUri != null) && (downloadUri.length() > 0)) {
76                                         downloadUri = new URI(((HtmlState) state).uri()).resolve(URLEncoder.encode(downloadUri, "UTF-8").replace("%2F", "/")).toString();
77                                 } else {
78                                         downloadUri = null;
79                                 }
80                                 TorrentFile torrentFile = new TorrentFile(name, size, magnetUri, downloadUri, fileCount, seedCount, leechCount);
81                                 torrentState.addTorrentFile(torrentFile);
82                         } catch (URISyntaxException use1) {
83                                 /* ignore; if uri was wrong, we wouldn’t be here. */
84                         } catch (UnsupportedEncodingException uee1) {
85                                 /* ignore, all JVMs can do UTF-8. */
86                         }
87                 }
88
89                 return torrentState;
90         }
91
92         //
93         // ABSTRACT METHODS
94         //
95
96         /**
97          * Returns the data rows from the given document.
98          *
99          * @param document
100          *            The document to get the data rows from
101          * @return The data rows
102          */
103         protected abstract Elements getDataRows(Document document);
104
105         /**
106          * Extracts the name from the given row.
107          *
108          * @param dataRow
109          *            The row to extract the name from
110          * @return The extracted name
111          */
112         protected abstract String extractName(Element dataRow);
113
114         /**
115          * Extracts the size from the given row.
116          *
117          * @param dataRow
118          *            The row to extract the size from
119          * @return The extracted size
120          */
121         protected abstract String extractSize(Element dataRow);
122
123         /**
124          * Extracts the magnet URI from the given row.
125          *
126          * @param dataRow
127          *            The row to extract the magnet URI from
128          * @return The extracted magnet URI
129          */
130         protected abstract String extractMagnetUri(Element dataRow);
131
132         /**
133          * Extracts the download URI from the given row.
134          *
135          * @param dataRow
136          *            The row to extract the download URI from
137          * @return The extracted download URI
138          */
139         protected abstract String extractDownloadUri(Element dataRow);
140
141         /**
142          * Extracts the file count from the given row.
143          *
144          * @param dataRow
145          *            The row to extract the file count from
146          * @return The extracted file count, or {@code 0} if the file count can not
147          *         be extracted
148          */
149         protected abstract int extractFileCount(Element dataRow);
150
151         /**
152          * Extracts the seed count from the given row.
153          *
154          * @param dataRow
155          *            The row to extract the seed count from
156          * @return The extracted seed count
157          */
158         protected abstract int extractSeedCount(Element dataRow);
159
160         /**
161          * Extracts the leech count from the given row.
162          *
163          * @param dataRow
164          *            The row to extract the leech count from
165          * @return The extracted leech count
166          */
167         protected abstract int extractLeechCount(Element dataRow);
168
169 }