2 * Reactor - KickAssTorrentsFilter.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.reactor.filters;
20 import static com.google.common.base.Preconditions.checkState;
23 import java.net.URISyntaxException;
25 import net.pterodactylus.reactor.Filter;
26 import net.pterodactylus.reactor.State;
27 import net.pterodactylus.reactor.queries.HttpQuery;
28 import net.pterodactylus.reactor.states.FailedState;
29 import net.pterodactylus.reactor.states.HtmlState;
30 import net.pterodactylus.reactor.states.TorrentState;
31 import net.pterodactylus.reactor.states.TorrentState.TorrentFile;
33 import org.jsoup.nodes.Document;
34 import org.jsoup.nodes.Element;
35 import org.jsoup.select.Elements;
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}.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class KickAssTorrentsFilter implements Filter {
50 public State filter(State state) {
51 if (!state.success()) {
52 return FailedState.from(state);
54 checkState(state instanceof HtmlState, "state is not an HtmlState but a %s", state.getClass().getName());
56 /* get result table. */
57 Document document = ((HtmlState) state).document();
58 Elements mainTable = document.select("table.data");
59 if (mainTable.isEmpty()) {
61 return new FailedState();
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);
72 int fileCount = extractFileCount(dataRow);
73 int seedCount = extractSeedCount(dataRow);
74 int leechCount = extractLeechCount(dataRow);
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. */
92 * Extracts the name from the given row.
95 * The row to extract the name from
96 * @return The extracted name
98 private static String extractName(Element dataRow) {
99 return dataRow.select("div.torrentname a.normalgrey").text();
103 * Extracts the size from the given row.
106 * The row to extract the size from
107 * @return The extracted size
109 private static String extractSize(Element dataRow) {
110 return dataRow.select("td:eq(1)").text();
114 * Extracts the magnet URI from the given row.
117 * The row to extract the magnet URI from
118 * @return The extracted magnet URI
120 private static String extractMagnetUri(Element dataRow) {
121 return dataRow.select("a.imagnet").attr("href");
125 * Extracts the download URI from the given row.
128 * The row to extract the download URI from
129 * @return The extracted download URI
131 private static String extractDownloadUri(Element dataRow) {
132 return dataRow.select("a.idownload:not(.partner1Button)").attr("href");
136 * Extracts the file count from the given row.
139 * The row to extract the file count from
140 * @return The extracted file count
142 private static int extractFileCount(Element dataRow) {
143 return Integer.valueOf(dataRow.select("td:eq(2)").text());
147 * Extracts the seed count from the given row.
150 * The row to extract the seed count from
151 * @return The extracted seed count
153 private static int extractSeedCount(Element dataRow) {
154 return Integer.valueOf(dataRow.select("td:eq(4)").text());
158 * Extracts the leech count from the given row.
161 * The row to extract the leech count from
162 * @return The extracted leech count
164 private static int extractLeechCount(Element dataRow) {
165 return Integer.valueOf(dataRow.select("td:eq(5)").text());