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);
73 downloadUri = new URI(((HtmlState) state).uri()).resolve(extractDownloadUri(dataRow)).toString();
74 TorrentFile torrentFile = new TorrentFile(name, size, magnetUri, downloadUri);
75 torrentState.addTorrentFile(torrentFile);
76 } catch (URISyntaxException use1) {
77 /* ignore; if uri was wrong, we wouldn’t be here. */
89 * Extracts the name from the given row.
92 * The row to extract the name from
93 * @return The extracted name
95 private static String extractName(Element dataRow) {
96 return dataRow.select("div.torrentname a.normalgrey").text();
100 * Extracts the size from the given row.
103 * The row to extract the size from
104 * @return The extracted size
106 private static String extractSize(Element dataRow) {
107 return dataRow.select("td:eq(1)").text();
111 * Extracts the magnet URI from the given row.
114 * The row to extract the magnet URI from
115 * @return The extracted magnet URI
117 private static String extractMagnetUri(Element dataRow) {
118 return dataRow.select("a.imagnet").attr("href");
122 * Extracts the download URI from the given row.
125 * The row to extract the download URI from
126 * @return The extracted download URI
128 private static String extractDownloadUri(Element dataRow) {
129 return dataRow.select("a.idownload:not(.partner1Button)").attr("href");