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