Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions rtorrent/lib/torrentparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down