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