02a356af9e039f4729cb6f8f4c19ad768bcd699f
[rhynodge.git] / src / main / java / net / pterodactylus / reactor / filters / KickAssTorrentsFilter.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.net.URI;
23 import java.net.URISyntaxException;
24
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;
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                         try {
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. */
78                         }
79                 }
80
81                 return torrentState;
82         }
83
84         //
85         // STATIC METHODS
86         //
87
88         /**
89          * Extracts the name from the given row.
90          *
91          * @param dataRow
92          *            The row to extract the name from
93          * @return The extracted name
94          */
95         private static String extractName(Element dataRow) {
96                 return dataRow.select("div.torrentname a.normalgrey").text();
97         }
98
99         /**
100          * Extracts the size from the given row.
101          *
102          * @param dataRow
103          *            The row to extract the size from
104          * @return The extracted size
105          */
106         private static String extractSize(Element dataRow) {
107                 return dataRow.select("td:eq(1)").text();
108         }
109
110         /**
111          * Extracts the magnet URI from the given row.
112          *
113          * @param dataRow
114          *            The row to extract the magnet URI from
115          * @return The extracted magnet URI
116          */
117         private static String extractMagnetUri(Element dataRow) {
118                 return dataRow.select("a.imagnet").attr("href");
119         }
120
121         /**
122          * Extracts the download URI from the given row.
123          *
124          * @param dataRow
125          *            The row to extract the download URI from
126          * @return The extracted download URI
127          */
128         private static String extractDownloadUri(Element dataRow) {
129                 return dataRow.select("a.idownload:not(.partner1Button)").attr("href");
130         }
131
132 }