Skip to content

Commit

Permalink
Fix and clean up torrentproject provider
Browse files Browse the repository at this point in the history
  • Loading branch information
medariox authored and labrys committed Jun 6, 2016
1 parent 94fe8dd commit e97c6c4
Showing 1 changed file with 52 additions and 39 deletions.
91 changes: 52 additions & 39 deletions sickbeard/providers/torrentproject.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
# coding=utf-8
# Author: Gonçalo M. (aka duramato/supergonkas) <supergonkas@gmail.com>
#
# This file is part of Medusa.
#
# This file is part of SickRage.
#
# SickRage is free software: you can redistribute it and/or modify
# Medusa 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.
#
# SickRage is distributed in the hope that it will be useful,
# Medusa 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 SickRage. If not, see <http://www.gnu.org/licenses/>.
# along with Medusa. If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals

from requests.compat import urljoin
import validators
import traceback

from sickbeard import logger, tvcache
from sickbeard.common import USER_AGENT
Expand All @@ -32,7 +33,7 @@ class TorrentProjectProvider(TorrentProvider): # pylint: disable=too-many-insta
def __init__(self):

# Provider Init
TorrentProvider.__init__(self, "TorrentProject")
TorrentProvider.__init__(self, 'TorrentProject')

# Credentials
self.public = True
Expand All @@ -58,65 +59,77 @@ def search(self, search_strings, age=0, ep_obj=None): # pylint: disable=too-man
search_params = {
'out': 'json',
'filter': 2101,
'showmagnets': 'on',
'num': 150
}

for mode in search_strings: # Mode = RSS, Season, Episode
items = []
logger.log(u"Search Mode: {}".format(mode), logger.DEBUG)
logger.log('Search Mode: {0}'.format(mode), logger.DEBUG)

for search_string in search_strings[mode]:

if mode != 'RSS':
logger.log(u"Search string: {}".format(search_string.decode("utf-8")),
logger.log('Search string: {0}'.format(search_string.decode('utf-8')),
logger.DEBUG)

search_params['s'] = search_string

if self.custom_url:
if not validators.url(self.custom_url):
logger.log("Invalid custom url set, please check your settings", logger.WARNING)
logger.log('Invalid custom url set, please check your settings', logger.WARNING)
return results
search_url = self.custom_url
else:
search_url = self.url

torrents = self.get_url(search_url, params=search_params, returns='json')
if not (torrents and "total_found" in torrents and int(torrents["total_found"]) > 0):
logger.log(u"Data returned from provider does not contain any torrents", logger.DEBUG)
if not (torrents and int(torrents.pop('total_found', 0)) > 0):
logger.log('Data returned from provider does not contain any torrents', logger.DEBUG)
continue

del torrents["total_found"]

results = []
for i in torrents:
title = torrents[i].get("title")
seeders = try_int(torrents[i].get("seeds"), 1)
leechers = try_int(torrents[i].get("leechs"), 0)

# Filter unseeded torrent
if seeders < min(self.minseed, 1):
for result in torrents:
try:
title = torrents[result].get('title')
download_url = torrents[result].get('magnet')
if not all([title, download_url]):
continue

download_url += self._custom_trackers
seeders = try_int(torrents[result].get('seeds'), 1)
leechers = try_int(torrents[result].get('leechs'), 0)

# Filter unseeded torrent
if seeders < min(self.minseed, 1):
if mode != 'RSS':
logger.log("Discarding torrent because it doesn't meet the"
' minimum seeders: {0}. Seeders: {1}'.format
(title, seeders), logger.DEBUG)
continue

torrent_hash = torrents[result].get('torrent_hash')
torrent_size = torrents[result].get('torrent_size')
size = convert_size(torrent_size) or -1

item = {
'title': title,
'link': download_url,
'size': size,
'seeders': seeders,
'leechers': leechers,
'pubdate': None,
'hash': torrent_hash
}
if mode != 'RSS':
logger.log(u"Discarding torrent because it doesn't meet the minimum seeders: {0}. Seeders: {1})".format(title, seeders), logger.DEBUG)
continue
logger.log('Found result: {0} with {1} seeders and {2} leechers'.format
(title, seeders, leechers), logger.DEBUG)

t_hash = torrents[i].get("torrent_hash")
torrent_size = torrents[i].get("torrent_size")
size = convert_size(torrent_size) or -1
download_url = torrents[i].get("magnet") + self._custom_trackers
pubdate = '' #TBA

if not all([title, download_url]):
items.append(item)
except (AttributeError, TypeError, KeyError, ValueError, IndexError):
logger.log('Failed parsing provider. Traceback: {0!r}'.format
(traceback.format_exc()), logger.ERROR)
continue

item = {'title': title, 'link': download_url, 'size': size, 'seeders': seeders, 'leechers': leechers, 'pubdate': None, 'hash': t_hash}

if mode != 'RSS':
logger.log(u"Found result: {0} with {1} seeders and {2} leechers".format
(title, seeders, leechers), logger.DEBUG)

items.append(item)

# For each search mode sort all the items by seeders if available
items.sort(key=lambda d: try_int(d.get('seeders', 0)), reverse=True)
results += items
Expand Down

0 comments on commit e97c6c4

Please sign in to comment.