();
- 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;
- }
-
-}