Add unit test for pirate bay filter
[rhynodge.git] / src / main / java / net / pterodactylus / rhynodge / filters / torrents / PirateBayFilter.java
1 /*
2  * Rhynodge - PirateBayFilter.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.torrents;
19
20 import java.util.regex.Pattern;
21
22 import net.pterodactylus.rhynodge.filters.TorrentSiteFilter;
23
24 import org.jsoup.nodes.Document;
25 import org.jsoup.nodes.Element;
26 import org.jsoup.select.Elements;
27
28 /**
29  * {@link net.pterodactylus.rhynodge.filters.TorrentSiteFilter} implementation that can parse
30  * {@code thepiratebay.se} result pages.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class PirateBayFilter extends TorrentSiteFilter {
35
36         /**
37          * {@inheritDoc}
38          */
39         @Override
40         protected Elements getDataRows(Document document) {
41                 return document.select("table#searchResult tbody tr:has(.vertTh)");
42         }
43
44         /**
45          * {@inheritDoc}
46          */
47         @Override
48         protected String extractName(Element dataRow) {
49                 return dataRow.select(".detName a").text();
50         }
51
52         /**
53          * {@inheritDoc}
54          */
55         @Override
56         protected String extractSize(Element dataRow) {
57                 return dataRow.select(".detDesc").text().split(Pattern.quote(","))[1].split(Pattern.quote("Size"))[1].trim().replace('\u00a0', ' ');
58         }
59
60         /**
61          * {@inheritDoc}
62          */
63         @Override
64         protected String extractMagnetUri(Element dataRow) {
65                 return dataRow.select("a[href^=magnet:]").attr("href");
66         }
67
68         /**
69          * {@inheritDoc}
70          */
71         @Override
72         protected String extractDownloadUri(Element dataRow) {
73                 return dataRow.select("a[href^=//torrents.]").attr("href");
74         }
75
76         /**
77          * {@inheritDoc}
78          */
79         @Override
80         protected int extractFileCount(Element dataRow) {
81                 return 0;
82         }
83
84         /**
85          * {@inheritDoc}
86          */
87         @Override
88         protected int extractSeedCount(Element dataRow) {
89                 return Integer.valueOf(dataRow.select("td:eq(2)").text());
90         }
91
92         /**
93          * {@inheritDoc}
94          */
95         @Override
96         protected int extractLeechCount(Element dataRow) {
97                 return Integer.valueOf(dataRow.select("td:eq(3)").text());
98         }
99
100 }