Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Check filenames in the .torrent if the .torrent's filename doesn't give a quality #116

Open
wants to merge 8 commits into from

3 participants

@rmccue

Occasionally, files will be missed because they have ambiguous filenames for the torrents, e.g. (HDTV-LOL), although the filename of the actual file will be fine, e.g. HDTV.XviD-LOL

This patch fixes this by checking that first. It's somewhat inefficient, in that it requires downloading the .torrent file to check, and if it matches, that will then get downloaded again. However, this is not really a large issue.

@bardelot

Instead of adding a new parser you could use the existing one from hachoir which is already included in Sick-Beard.
e.g. to extract the filename use something like:

from lib.hachoir_parser import createParser
from lib.hachoir_metadata import extractMetadata
from lib.hachoir_core.error import HachoirError

try:
    parser = createParser(u"/home/user/file.torrent")
    metadata = extractMetadata(parser)
except HachoirError:
    return False

if not metadata.has('filename'):
    return False

filename = metadata.get('filename')
@rmccue

The problem with using that parser is that, AFAICT, we have to download the file first. Alternatively, we can use a stream from urllib2, but then we can't use helpers.getURL, and I believe we also lose the ability to parse gzipped torrents.

Should I convert this over?

@thezoggy

hachoir is for reading a stream..
https://bitbucket.org/haypo/hachoir/wiki/hachoir-parser

  • torrent: Torrent metainfo file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
30 sickbeard/providers/ezrss.py
@@ -18,6 +18,7 @@
import urllib
import re
+import urllib2
import xml.etree.cElementTree as etree
@@ -26,7 +27,7 @@
from sickbeard.common import *
from sickbeard import logger
-from sickbeard import tvcache, sceneHelpers
+from sickbeard import tvcache, sceneHelpers, torrentParser, helpers
class EZRSSProvider(generic.TorrentProvider):
@@ -52,6 +53,12 @@ def getQuality(self, item):
filename = item.findtext('{%s}torrent/{%s}fileName' %(self.ezrss_ns,self.ezrss_ns))
quality = Quality.nameQuality(filename)
+ if quality == Quality.UNKNOWN:
+ # Use the filename in the torrent instead
+ logger.log(u"Quality could not be found from the torrent name, checking actual filename", logger.MESSAGE)
+ url = item.findtext('link').replace('&','&')
+ filename = self._extract_name_from_torrent(url)
+ quality = Quality.nameQuality(filename)
return quality
def findSeasonResults(self, show, season):
@@ -154,6 +161,11 @@ def _extract_name_from_filename(self, filename):
return match.group(1)
return None
+ def _extract_name_from_torrent(self, url):
+ contents = self.getURL(url)
+ decoded = torrentParser.bdecode(contents)
+ return decoded['info']['name']
+
class EZRSSCache(tvcache.TVCache):
@@ -164,6 +176,22 @@ def __init__(self, provider):
# only poll EZRSS every 15 minutes max
self.minTime = 15
+ def getQuality(self, name, season, episodeText, tvrid, tvdbid, url):
+ quality = Quality.nameQuality(name)
+ if (tvdbid != 0 or tvrid != 0) and quality == Quality.UNKNOWN:
+ logger.log(u"Checking quality properly for " + name, logger.MESSAGE)
+ filename = self._extract_name_from_torrent(url)
+ quality = Quality.nameQuality(filename)
+ return quality
+
+ def _extract_name_from_torrent(self, url):
+ try:
+ contents = helpers.getURL(url, [])
+ except (urllib2.HTTPError, IOError), e:
+ logger.log(u"Error loading "+self.name+" URL: " + str(sys.exc_info()) + " - " + str(e), logger.ERROR)
+ return None
+ decoded = torrentParser.bdecode(contents)
+ return decoded['info']['name']
def _getRSSData(self):
url = self.provider.url + 'feed/'
View
1  sickbeard/sceneHelpers.py
@@ -300,4 +300,3 @@ def retrieve_exceptions():
for cur_tvdb_id in exception_dict:
for cur_exception in exception_dict[cur_tvdb_id]:
myDB.action("INSERT INTO scene_exceptions (tvdb_id, show_name) VALUES (?,?)", [cur_tvdb_id, cur_exception])
-
View
133 sickbeard/torrentParser.py
@@ -0,0 +1,133 @@
+# The following decoder is licensed under the MIT license:
+#
+# Copyright (c) 2010 Pedro Rodrigues
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+class BDecoder:
+ @staticmethod
+ def _decode_integer(input):
+ end = input.find('e')
+ if end>-1:
+ return (int(input[1:end]),input[end+1:])
+ end += 1
+ else:
+ raise ValueError("Missing ending delimiter 'e'")
+
+ @staticmethod
+ def _decode_string(input):
+ start = input.find(':')+1
+ size = int(input[:start-1])
+ end = start+size
+ return (input[start:end], input[end:])
+
+ @staticmethod
+ def _decode_list(input):
+ result = []
+ remainder = input[1:]
+ while True:
+ if remainder[0] == 'i':
+ r = BDecoder._decode_integer(remainder)
+ result.append(r[0])
+ remainder = r[1]
+
+ elif remainder[0].isdigit():
+ r = BDecoder._decode_string(remainder)
+ result.append(r[0])
+ remainder = r[1]
+
+ elif remainder[0] == 'l':
+ r = BDecoder._decode_list(remainder)
+ result.append(r[0])
+ remainder = r[1]
+
+ elif remainder[0] == 'd':
+ r = BDecoder._decode_dict(remainder)
+ result.append(r[0])
+ remainder = r[1]
+
+ elif remainder[0] == 'e':
+ remainder = remainder[1:]
+ break
+
+ else:
+ raise ValueError("Invalid initial delimiter '%r' found while decoding a list" % remainder[0])
+
+ return (result,remainder)
+
+ @staticmethod
+ def _decode_dict(input):
+ result = {}
+ remainder = input[1:]
+ while remainder[0] != 'e':
+ r = BDecoder._decode_string(remainder)
+ key = r[0]
+ remainder = r[1]
+
+ if remainder[0] == 'i':
+ r = BDecoder._decode_integer(remainder)
+ value = r[0]
+ result[key] = value
+ remainder = r[1]
+
+ elif remainder[0].isdigit():
+ r = BDecoder._decode_string(remainder)
+ value = r[0]
+ result[key] = value
+ remainder = r[1]
+
+ elif remainder[0] == 'l':
+ r = BDecoder._decode_list(remainder)
+ value = r[0]
+ result[key] = value
+ remainder = r[1]
+
+ elif remainder[0] == 'd':
+ r = BDecoder._decode_dict(remainder)
+ value = r[0]
+ result[key] = value
+ remainder = r[1]
+
+ else:
+ raise ValueError("Invalid initial delimiter '%r' found while decoding a dictionary" % remainder[0])
+
+ return (result,remainder[1:])
+
+def bdecode(input):
+ '''Decode strings from bencode format to python value types.
+
+ Keyword arguments:
+ input -- the input string to be decoded
+ '''
+
+ input = input.strip()
+
+ if input[0] == 'i':
+ return BDecoder._decode_integer(input)[0]
+
+ elif input[0].isdigit():
+ return BDecoder._decode_string(input)[0]
+
+ elif input[0] == 'l':
+ return BDecoder._decode_list(input)[0]
+
+ elif input[0] == 'd':
+ return BDecoder._decode_dict(input)[0]
+ else:
+ raise ValueError("Invalid initial delimiter '%s'" % input[0])
View
4 sickbeard/tvcache.py
@@ -279,11 +279,13 @@ def _addCacheEntry(self, name, url, season=None, episodes=None, tvdb_id=0, tvrag
curTimestamp = int(time.mktime(datetime.datetime.today().timetuple()))
if not quality:
- quality = Quality.nameQuality(name)
+ quality = self.getQuality(name, season, episodeText, tvrage_id, tvdb_id, url)
myDB.action("INSERT INTO "+self.providerID+" (name, season, episodes, tvrid, tvdbid, url, time, quality) VALUES (?,?,?,?,?,?,?,?)",
[name, season, episodeText, tvrage_id, tvdb_id, url, curTimestamp, quality])
+ def getQuality(name, season, episodeText, tvrid, tvdbid):
+ return Quality.nameQuality(name)
def searchCache(self, episode, manualSearch=False):
neededEps = self.findNeededEpisodes(episode, manualSearch)
Something went wrong with that request. Please try again.