From 19aa63253254472495cf05be28fbb41161aa3e35 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 9 Jan 2013 22:19:29 +0100 Subject: [PATCH] Add base implementation for torrent site filters. --- .../reactor/filters/TorrentSiteFilter.java | 163 +++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/main/java/net/pterodactylus/reactor/filters/TorrentSiteFilter.java diff --git a/src/main/java/net/pterodactylus/reactor/filters/TorrentSiteFilter.java b/src/main/java/net/pterodactylus/reactor/filters/TorrentSiteFilter.java new file mode 100644 index 0000000..968d671 --- /dev/null +++ b/src/main/java/net/pterodactylus/reactor/filters/TorrentSiteFilter.java @@ -0,0 +1,163 @@ +/* + * Reactor - KickAssTorrentsFilter.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.reactor.filters; + +import static com.google.common.base.Preconditions.checkState; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLEncoder; + +import net.pterodactylus.reactor.Filter; +import net.pterodactylus.reactor.State; +import net.pterodactylus.reactor.queries.HttpQuery; +import net.pterodactylus.reactor.states.FailedState; +import net.pterodactylus.reactor.states.HtmlState; +import net.pterodactylus.reactor.states.TorrentState; +import net.pterodactylus.reactor.states.TorrentState.TorrentFile; + +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +/** + * {@link Filter} implementation that parses a {@link TorrentState} from an + * {@link HtmlState} which was generated by a {@link HttpQuery} to + * {@code kickasstorrents.ph}. + * + * @author David ‘Bombe’ Roden + */ +public abstract class TorrentSiteFilter implements Filter { + + /** + * {@inheritDoc} + */ + @Override + public State filter(State state) { + if (!state.success()) { + return FailedState.from(state); + } + checkState(state instanceof HtmlState, "state is not an HtmlState but a %s", state.getClass().getName()); + + /* get result table. */ + Document document = ((HtmlState) state).document(); + + /* iterate over all rows. */ + Elements dataRows = getDataRows(document); + TorrentState torrentState = new TorrentState(); + for (Element dataRow : dataRows) { + String name = extractName(dataRow); + String size = extractSize(dataRow); + String magnetUri = extractMagnetUri(dataRow); + String downloadUri; + int fileCount = extractFileCount(dataRow); + int seedCount = extractSeedCount(dataRow); + int leechCount = extractLeechCount(dataRow); + try { + downloadUri = new URI(((HtmlState) state).uri()).resolve(URLEncoder.encode(extractDownloadUri(dataRow), "UTF-8").replace("%2F", "/")).toString(); + TorrentFile torrentFile = new TorrentFile(name, size, magnetUri, downloadUri, fileCount, seedCount, leechCount); + torrentState.addTorrentFile(torrentFile); + } catch (URISyntaxException use1) { + /* ignore; if uri was wrong, we wouldn’t be here. */ + } catch (UnsupportedEncodingException uee1) { + /* ignore, all JVMs can do UTF-8. */ + } + } + + return torrentState; + } + + // + // ABSTRACT METHODS + // + + /** + * Returns the data rows from the given document. + * + * @param document + * The document to get the data rows from + * @return The data rows + */ + protected abstract Elements getDataRows(Document document); + + /** + * Extracts the name from the given row. + * + * @param dataRow + * The row to extract the name from + * @return The extracted name + */ + protected abstract String extractName(Element dataRow); + + /** + * Extracts the size from the given row. + * + * @param dataRow + * The row to extract the size from + * @return The extracted size + */ + protected abstract String extractSize(Element dataRow); + + /** + * Extracts the magnet URI from the given row. + * + * @param dataRow + * The row to extract the magnet URI from + * @return The extracted magnet URI + */ + protected abstract String extractMagnetUri(Element dataRow); + + /** + * Extracts the download URI from the given row. + * + * @param dataRow + * The row to extract the download URI from + * @return The extracted download URI + */ + protected abstract String extractDownloadUri(Element dataRow); + + /** + * Extracts the file count from the given row. + * + * @param dataRow + * The row to extract the file count from + * @return The extracted file count, or {@code 0} if the file count can not + * be extracted + */ + protected abstract int extractFileCount(Element dataRow); + + /** + * Extracts the seed count from the given row. + * + * @param dataRow + * The row to extract the seed count from + * @return The extracted seed count + */ + protected abstract int extractSeedCount(Element dataRow); + + /** + * Extracts the leech count from the given row. + * + * @param dataRow + * The row to extract the leech count from + * @return The extracted leech count + */ + protected abstract int extractLeechCount(Element dataRow); + +} -- 2.7.4