diff --git a/rtorrent/lib/torrentparser.py b/rtorrent/lib/torrentparser.py index b59ae22..e61f578 100644 --- a/rtorrent/lib/torrentparser.py +++ b/rtorrent/lib/torrentparser.py @@ -23,6 +23,9 @@ import re import rtorrent.lib.bencode as bencode import hashlib +import urlparse +import base64 +import binascii if is_py3(): from urllib.request import urlopen # @UnresolvedImport @UnusedImport @@ -89,11 +92,27 @@ def _decode_torrent(self, raw_torrent=None): def _calc_info_hash(self): self.info_hash = None - if "info" in self._torrent_decoded.keys(): - info_encoded = bencode.encode(self._torrent_decoded["info"]) - - if info_encoded: - self.info_hash = hashlib.sha1(info_encoded).hexdigest().upper() + if "magnet-uri" in self._torrent_decoded.keys(): + m = self._torrent_decoded['magnet-uri'] + mqs=urlparse.parse_qs( urlparse.urlparse(m).query ) + if 'xt' in mqs: + xts = mqs['xt'][0] + if xts.startswith( 'urn:btih:' ): + btih = xts[9:] + if len(btih) == 40: + btih = btih.upper() + elif len(btih) == 32: + btih = binascii.b2a_hex(base64.b32decode(btih)).upper() + else: + btih = None + self.info_hash = btih + + if self.info_hash is None: + if "info" in self._torrent_decoded.keys(): + info_encoded = bencode.encode(self._torrent_decoded["info"]) + + if info_encoded: + self.info_hash = hashlib.sha1(info_encoded).hexdigest().upper() return(self.info_hash)