From 19a89d453c790e51d10172b3aaeb3ddefefcd696 Mon Sep 17 00:00:00 2001 From: echel0n Date: Mon, 28 Jul 2014 12:19:41 -0700 Subject: [PATCH] We now check if a torrent url links to a valid file before adding as a verified result to get snatched, this helps prevent issues when attempting to add torrent to client later on to find the url returned nothing resulting in a error. --- sickbeard/clients/generic.py | 24 +++++++++++------------- sickbeard/helpers.py | 4 ++-- sickbeard/providers/generic.py | 10 ++++++++-- sickbeard/search.py | 7 ------- sickbeard/tvcache.py | 9 +++++++++ 5 files changed, 30 insertions(+), 24 deletions(-) diff --git a/sickbeard/clients/generic.py b/sickbeard/clients/generic.py index 91c0539d..4f5081ec 100644 --- a/sickbeard/clients/generic.py +++ b/sickbeard/clients/generic.py @@ -142,16 +142,17 @@ class GenericClient(object): def _get_torrent_hash(self, result): - torrent_hash = None if result.url.startswith('magnet'): - torrent_hash = re.findall('urn:btih:([\w]{32,40})', result.url)[0] - if len(torrent_hash) == 32: - torrent_hash = b16encode(b32decode(torrent_hash)).lower() - elif result.content: - info = bdecode(result.content)["info"] - torrent_hash = sha1(bencode(info)).hexdigest() + result.hash = re.findall('urn:btih:([\w]{32,40})', result.url)[0] + if len(result.hash) == 32: + result.hash = b16encode(b32decode(result.hash)).lower() + else: + result.content = result.provider.getURL(result.url) + if result.content: + info = bdecode(result.content)["info"] + result.hash = sha1(bencode(info)).hexdigest() - return torrent_hash + return result def sendTORRENT(self, result): @@ -164,11 +165,8 @@ class GenericClient(object): return r_code try: - - result.hash = self._get_torrent_hash(result) - if not result.hash: - logger.log(self.name + u': Unable to get hash for Torrent', logger.DEBUG) - return False + # Sets per provider seed ratio + result.ratio = result.provider.seedRatio() if result.url.startswith('magnet'): r_code = self._add_torrent_uri(result) diff --git a/sickbeard/helpers.py b/sickbeard/helpers.py index ddbbdcb2..c9a84067 100644 --- a/sickbeard/helpers.py +++ b/sickbeard/helpers.py @@ -1235,7 +1235,7 @@ def getURL(url, post_data=None, params=None, headers=None, timeout=30, session=N resp = session.get(url, data=post_data, timeout=timeout) if not resp.ok: logger.log(u"Requested url " + url + " returned status code is " + str( - resp.status_code) + ': ' + clients.http_error_code[resp.status_code], logger.WARNING) + resp.status_code) + ': ' + clients.http_error_code[resp.status_code], logger.DEBUG) return except requests.exceptions.HTTPError, e: @@ -1281,7 +1281,7 @@ def download_file(url, filename, session=None): resp = session.get(url) if not resp.ok: logger.log(u"Requested url " + url + " returned status code is " + str( - resp.status_code) + ': ' + clients.http_error_code[resp.status_code], logger.WARNING) + resp.status_code) + ': ' + clients.http_error_code[resp.status_code], logger.DEBUG) return False with open(filename, 'wb') as fp: diff --git a/sickbeard/providers/generic.py b/sickbeard/providers/generic.py index 50f28ad6..ea2f2cb3 100644 --- a/sickbeard/providers/generic.py +++ b/sickbeard/providers/generic.py @@ -33,6 +33,7 @@ from sickbeard import encodingKludge as ek from sickbeard.exceptions import ex from sickbeard.name_parser.parser import NameParser, InvalidNameException, InvalidShowException from sickbeard.common import Quality +from sickbeard import clients from hachoir_parser import createParser @@ -405,8 +406,13 @@ class GenericProvider: epNum = SEASON_RESULT logger.log(u"Separating full season result to check for later", logger.DEBUG) - if not result: - continue + # validate torrent file if not magnet link to avoid invalid torrent links + if self.providerType == sickbeard.providers.generic.GenericProvider.TORRENT: + client = clients.getClientIstance(sickbeard.TORRENT_METHOD)() + result = client._get_torrent_hash(result) + if not result.hash: + logger.log(u'Unable to get torrent hash for ' + title + ', skipping it', logger.DEBUG) + continue if epNum not in results: results[epNum] = [result] diff --git a/sickbeard/search.py b/sickbeard/search.py index cb035ef2..9fbcf42c 100644 --- a/sickbeard/search.py +++ b/sickbeard/search.py @@ -129,12 +129,6 @@ def snatchEpisode(result, endStatus=SNATCHED): if sickbeard.TORRENT_METHOD == "blackhole": dlResult = _downloadResult(result) else: - # Sets per provider seed ratio - result.ratio = result.provider.seedRatio() - - # Gets torrent file contents if not magnet link - result.content = result.provider.getURL(result.url) if not result.url.startswith('magnet') else None - # Snatches torrent with client client = clients.getClientIstance(sickbeard.TORRENT_METHOD)() dlResult = client.sendTORRENT(result) @@ -333,7 +327,6 @@ def filterSearchResults(show, season, results): return foundResults - def searchForNeededEpisodes(show, episodes): foundResults = {} diff --git a/sickbeard/tvcache.py b/sickbeard/tvcache.py index 887f39b5..553853f9 100644 --- a/sickbeard/tvcache.py +++ b/sickbeard/tvcache.py @@ -31,6 +31,7 @@ from sickbeard.exceptions import MultipleShowObjectsException from sickbeard.exceptions import AuthException from name_parser.parser import NameParser, InvalidNameException, InvalidShowException from sickbeard.rssfeeds import RSSFeeds +from sickbeard import clients class CacheDBConnection(db.DBConnection): def __init__(self, providerName): @@ -360,6 +361,14 @@ class TVCache(): if self.provider.providerType == sickbeard.providers.generic.GenericProvider.TORRENT \ and not url.startswith('magnet') else None + # validate torrent file if not magnet link to avoid invalid torrent links + if self.provider.providerType == sickbeard.providers.generic.GenericProvider.TORRENT: + client = clients.getClientIstance(sickbeard.TORRENT_METHOD)() + result = client._get_torrent_hash(result) + if not result.hash: + logger.log(u'Unable to get torrent hash for ' + title + ', skipping it', logger.DEBUG) + continue + # add it to the list if epObj not in neededEps: neededEps[epObj] = [result]