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.

This commit is contained in:
echel0n 2014-07-28 12:19:41 -07:00
parent 60a6569064
commit 19a89d453c
5 changed files with 30 additions and 24 deletions

View file

@ -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)

View file

@ -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:

View file

@ -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]

View file

@ -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 = {}

View file

@ -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]