diff --git a/app/src/main/java/org/transdroid/search/TorrentSite.java b/app/src/main/java/org/transdroid/search/TorrentSite.java index e5c4ac9..d931122 100644 --- a/app/src/main/java/org/transdroid/search/TorrentSite.java +++ b/app/src/main/java/org/transdroid/search/TorrentSite.java @@ -34,7 +34,6 @@ import org.transdroid.search.ScambioEtico.ScambioEtico; import org.transdroid.search.TorrentDay.TorrentDayAdapter; import org.transdroid.search.TorrentLeech.TorrentLeechAdapter; -import org.transdroid.search.TvTorrents.TvTorrentsAdapter; import org.transdroid.search.hdbitsorg.HdBitsOrgAdapter; import org.transdroid.search.revolutiontt.RevolutionTTAdapter; @@ -141,12 +140,6 @@ public ISearchAdapter getAdapter() { public ISearchAdapter getAdapter() { return new TorrentLeechAdapter(); } - }, - TvTorrents { - @Override - public ISearchAdapter getAdapter() { - return new TvTorrentsAdapter(); - } }; /** diff --git a/app/src/main/java/org/transdroid/search/TvTorrents/TvTorrentsAdapter.java b/app/src/main/java/org/transdroid/search/TvTorrents/TvTorrentsAdapter.java deleted file mode 100644 index 11c48b2..0000000 --- a/app/src/main/java/org/transdroid/search/TvTorrents/TvTorrentsAdapter.java +++ /dev/null @@ -1,246 +0,0 @@ -/* - * This file is part of Transdroid Torrent Search - * - * - * Transdroid Torrent Search is free software: you can redistribute - * it and/or modify it under the terms of the GNU Lesser General - * Public License as published by the Free Software Foundation, - * either version 3 of the License, or (at your option) any later - * version. - * - * Transdroid Torrent Search 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Transdroid. If not, see . - */ -package org.transdroid.search.TvTorrents; - -import java.io.InputStream; -import java.net.URLEncoder; -import java.security.InvalidParameterException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import javax.security.auth.login.LoginException; - -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.entity.UrlEncodedFormEntity; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.message.BasicNameValuePair; -import org.apache.http.params.BasicHttpParams; -import org.apache.http.params.HttpConnectionParams; -import org.apache.http.params.HttpParams; -import org.transdroid.search.ISearchAdapter; -import org.transdroid.search.SearchResult; -import org.transdroid.search.SortOrder; -import org.transdroid.search.TorrentSite; -import org.transdroid.search.gui.SettingsHelper; -import org.transdroid.util.HttpHelper; - -import android.content.Context; - -/** - * An adapter that provides access to TvTorrents searches by parsing the raw HTML output. - */ -public class TvTorrentsAdapter implements ISearchAdapter { - - private static final String LOGINURL = "http://www.tvtorrents.com/login.do"; - private static final String QUERYURL = "http://www.tvtorrents.com/loggedin/search.do?search=%1$s"; - private static final int CONNECTION_TIMEOUT = 8000; - - private DefaultHttpClient prepareRequest(Context context) throws Exception { - - String username = SettingsHelper.getSiteUser(context, TorrentSite.TvTorrents); - String password = SettingsHelper.getSitePass(context, TorrentSite.TvTorrents); - if (username == null || password == null) { - throw new InvalidParameterException( - "No username or password was provided, while this is required for this private site."); - } - - // Setup request using GET - HttpParams httpparams = new BasicHttpParams(); - HttpConnectionParams.setConnectionTimeout(httpparams, CONNECTION_TIMEOUT); - HttpConnectionParams.setSoTimeout(httpparams, CONNECTION_TIMEOUT); - DefaultHttpClient httpclient = new DefaultHttpClient(httpparams); - - // First log in - HttpPost loginPost = new HttpPost(LOGINURL); - loginPost.setEntity(new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair[] { - new BasicNameValuePair("username", username), new BasicNameValuePair("password", password), - new BasicNameValuePair("posted", "true"), new BasicNameValuePair("cookie", "Y") }))); - HttpResponse loginResult = httpclient.execute(loginPost); - if (loginResult.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { - // Failed to sign in - throw new LoginException("Login failure for TvTorrents with user " + username); - } - - return httpclient; - - } - - @Override - public List search(Context context, String query, SortOrder order, int maxResults) throws Exception { - - DefaultHttpClient httpclient = prepareRequest(context); - - // Build a search request parameters - String encodedQuery = URLEncoder.encode(query, "UTF-8"); - - // NOTE No sorting supported by the site - final String url = String.format(QUERYURL, encodedQuery); - - // Start synchronous search - - // Make request - HttpGet httpget = new HttpGet(url); - HttpResponse response = httpclient.execute(httpget); - - // Read HTML response - InputStream instream = response.getEntity().getContent(); - String html = HttpHelper.convertStreamToString(instream); - instream.close(); - return parseHtml(html, maxResults); - - } - - @Override - public InputStream getTorrentFile(Context context, String url) throws Exception { - - // Provide an authenticated file handle to the requested url - DefaultHttpClient httpclient = prepareRequest(context); - HttpResponse response = httpclient.execute(new HttpGet(url)); - return response.getEntity().getContent(); - - } - - protected List parseHtml(String html, int maxResults) throws Exception { - - try { - - // Texts to find subsequently - final String NOTORRENTS = "Not found"; - final String RESULTS = "

SEARCH - "; - final String TORRENT = " results = new ArrayList(); - if (html.indexOf(NOTORRENTS) >= 0) - return results; // Success, but no results for this query - - int hashStart = html.indexOf(HASH) + HASH.length(); - String hash = html.substring(hashStart, html.indexOf(HASH_END, hashStart)); - int digestStart = html.indexOf(DIGEST, hashStart) + DIGEST.length(); - String digest = html.substring(digestStart, html.indexOf(DIGEST_END, digestStart)); - int resultsStart = html.indexOf(RESULTS) + RESULTS.length(); - int torStart = html.indexOf(TORRENT, resultsStart); - while (torStart >= 0 && results.size() < maxResults) { - int nextTorrentIndex = html.indexOf(TORRENT, torStart + TORRENT.length()); - if (nextTorrentIndex >= 0) { - results.add(parseHtmlItem(html.substring(torStart + TORRENT.length(), nextTorrentIndex), digest, - hash)); - } else { - results.add(parseHtmlItem(html.substring(torStart + TORRENT.length()), digest, hash)); - } - torStart = nextTorrentIndex; - } - return results; - - } catch (OutOfMemoryError e) { - throw new Exception(e); - } catch (Exception e) { - throw new Exception(e); - } - } - - private SearchResult parseHtmlItem(String htmlItem, String digest, String hash) { - - // Texts to find subsequently - final String SHOWNAME = ""; - final String SHOWNAME_END = "
"; - final String NAME_END = ""; - final String SEEDERS = "kB/s\">"; - final String SEEDERS_END = "
"; - final String LEECHERS = "kB/s\">"; - final String LEECHERS_END = ""; - final String SIZE = "title=\"Torrent is "; - final String SIZE_END = "\">"; - final String LINK = "loadTorrent('"; - final String LINK_END = "')"; - String prefix = "http://www.tvtorrents.com"; - String linkBase = "http://torrent.tvtorrents.com/FetchTorrentServlet?info_hash=%1$s&digest=%2$s&hash=%3$s"; - - int shownameStart = htmlItem.indexOf(SHOWNAME) + SHOWNAME.length(); - String showname = htmlItem.substring(shownameStart, htmlItem.indexOf(SHOWNAME_END, shownameStart)).trim(); - - // Details link starts right after the show name of an item - int detailsStart = htmlItem.indexOf(SHOWNAME_END, shownameStart) + SHOWNAME_END.length(); - String details = htmlItem.substring(detailsStart, htmlItem.indexOf(DETAILS_END, detailsStart)); - details = prefix + details; - - // Name starts right after the link of an item - int nameStart = htmlItem.indexOf(DETAILS_END, detailsStart) + DETAILS_END.length(); - String name = htmlItem.substring(nameStart, htmlItem.indexOf(NAME_END, nameStart)); - name = showname + " " + name; - - int seedersStart = htmlItem.indexOf(SEEDERS, nameStart) + SEEDERS.length(); - int seeders = 0; - if (seedersStart >= 0) { - try { - String seedersText = htmlItem.substring(seedersStart, htmlItem.indexOf(SEEDERS_END, seedersStart)); - seeders = Integer.parseInt(seedersText); - } catch (Exception e) { - // Number of seeders not found; ignore - } - } - - int leechersStart = htmlItem.indexOf(LEECHERS, seedersStart) + LEECHERS.length(); - int leechers = 0; - if (leechersStart >= 0) { - try { - String leechersText = htmlItem.substring(leechersStart, htmlItem.indexOf(LEECHERS_END, leechersStart)); - leechers = Integer.parseInt(leechersText); - } catch (Exception e) { - // Number of seeders not found; ignore - } - } - - int sizeStart = htmlItem.indexOf(SIZE, leechersStart) + SIZE.length(); - String size = htmlItem.substring(sizeStart, htmlItem.indexOf(SIZE_END, sizeStart)); - - int infohashStart = htmlItem.indexOf(LINK, sizeStart) + LINK.length(); - String infohash = htmlItem.substring(infohashStart, htmlItem.indexOf(LINK_END, infohashStart)); - String link = String.format(linkBase, infohash, digest, hash); - - return new SearchResult(name, link, details, size, null, seeders, leechers); - - } - - @Override - public String buildRssFeedUrlFromSearch(String query, SortOrder order) { - // TvTorrents doesn't support RSS feed-based searches - return null; - } - - @Override - public String getSiteName() { - return "TvTorrents"; - } - - @Override - public boolean isPrivateSite() { - return true; - } - -}