[Py2to3] Fix TorrentInfo metainfo dict key lookups

This commit is contained in:
Calum Lind 2018-07-28 17:19:33 +01:00
parent af2bed8a0f
commit ad20ec62f2
2 changed files with 36 additions and 33 deletions

View File

@ -930,7 +930,7 @@ def decode_bytes(byte_str, encoding='utf8'):
lambda: (encoding, 'ignore'), lambda: (encoding, 'ignore'),
] ]
if encoding is not 'utf8': if encoding.lower() not in ['utf8', 'utf-8']:
encodings.insert(0, lambda: (encoding, 'strict')) encodings.insert(0, lambda: (encoding, 'strict'))
for l in encodings: for l in encodings:

View File

@ -195,7 +195,8 @@ class TorrentInfo(object):
def __init__(self, filename='', filetree=1, metainfo=None, metadata=None): def __init__(self, filename='', filetree=1, metainfo=None, metadata=None):
# Get the torrent metainfo from the torrent file # Get the torrent metainfo from the torrent file
if metadata: if metadata:
self._metainfo_dict = {'info': bencode.bdecode(metadata)} self._metainfo_dict = {b'info': bencode.bdecode(metadata)}
self._metainfo = bencode.bencode(self._metainfo_dict) self._metainfo = bencode.bencode(self._metainfo_dict)
else: else:
self._metainfo = metainfo self._metainfo = metainfo
@ -214,50 +215,52 @@ class TorrentInfo(object):
log.warning('Failed to decode %s: %s', filename, ex) log.warning('Failed to decode %s: %s', filename, ex)
return return
info_dict = self._metainfo_dict['info'] info_dict = self._metainfo_dict[b'info']
self._info_hash = sha(bencode.bencode(info_dict)).hexdigest() self._info_hash = sha(bencode.bencode(info_dict)).hexdigest()
# Get encoding from torrent file if available # Get encoding from torrent file if available
encoding = self._metainfo_dict.get('encoding', None) encoding = self._metainfo_dict.get(b'encoding', None)
codepage = self._metainfo_dict.get('codepage', None) codepage = self._metainfo_dict.get(b'codepage', None)
if not encoding: if not encoding:
encoding = codepage if codepage else 'UTF-8' encoding = codepage if codepage else 'UTF-8'
# Decode 'name' with encoding unless 'name.utf-8' found. # Decode 'name' with encoding unless 'name.utf-8' found.
if 'name.utf-8' in info_dict: if b'name.utf-8' in info_dict:
self._name = decode_bytes(info_dict['name.utf-8']) self._name = decode_bytes(info_dict[b'name.utf-8'])
else: else:
self._name = decode_bytes(info_dict['name'], encoding) if encoding:
encoding = encoding.decode()
self._name = decode_bytes(info_dict[b'name'], encoding)
# Get list of files from torrent info # Get list of files from torrent info
if 'files' in info_dict: if b'files' in info_dict:
paths = {} paths = {}
dirs = {} dirs = {}
prefix = self._name if len(info_dict['files']) > 1 else '' prefix = self._name if len(info_dict[b'files']) > 1 else ''
for index, f in enumerate(info_dict['files']): for index, f in enumerate(info_dict[b'files']):
if 'path.utf-8' in f: if b'path.utf-8' in f:
path = decode_bytes(os.path.join(*f['path.utf-8'])) path = decode_bytes(os.path.join(*f[b'path.utf-8']))
del f['path.utf-8'] del f[b'path.utf-8']
else: else:
path = decode_bytes(os.path.join(*f['path']), encoding) path = decode_bytes(os.path.join(*f[b'path']), encoding)
if prefix: if prefix:
path = os.path.join(prefix, path) path = os.path.join(prefix, path)
f['path'] = path f[b'path'] = path
f['index'] = index f[b'index'] = index
if 'sha1' in f and len(f['sha1']) == 20: if b'sha1' in f and len(f[b'sha1']) == 20:
f['sha1'] = f['sha1'].encode('hex') f[b'sha1'] = f[b'sha1'].encode(b'hex')
if 'ed2k' in f and len(f['ed2k']) == 16: if b'ed2k' in f and len(f[b'ed2k']) == 16:
f['ed2k'] = f['ed2k'].encode('hex') f[b'ed2k'] = f['ed2k'].encode(b'hex')
if 'filehash' in f and len(f['filehash']) == 20: if b'filehash' in f and len(f[b'filehash']) == 20:
f['filehash'] = f['filehash'].encode('hex') f[b'filehash'] = f[b'filehash'].encode(b'hex')
paths[path] = f paths[path] = f
dirname = os.path.dirname(path) dirname = os.path.dirname(path)
while dirname: while dirname:
dirinfo = dirs.setdefault(dirname, {}) dirinfo = dirs.setdefault(dirname, {})
dirinfo['length'] = dirinfo.get('length', 0) + f['length'] dirinfo[b'length'] = dirinfo.get(b'length', 0) + f[b'length']
dirname = os.path.dirname(dirname) dirname = os.path.dirname(dirname)
if filetree == 2: if filetree == 2:
@ -274,7 +277,7 @@ class TorrentInfo(object):
def walk(path, item): def walk(path, item):
if isinstance(item, dict): if isinstance(item, dict):
return item return item
return [paths[path]['index'], paths[path]['length'], True] return [paths[path][b'index'], paths[path][b'length'], True]
file_tree = FileTree(paths) file_tree = FileTree(paths)
file_tree.walk(walk) file_tree.walk(walk)
@ -286,32 +289,32 @@ class TorrentInfo(object):
self._name: { self._name: {
'type': 'file', 'type': 'file',
'index': 0, 'index': 0,
'length': info_dict['length'], 'length': info_dict[b'length'],
'download': True, 'download': True,
}, },
}, },
} }
else: else:
self._files_tree = { self._files_tree = {
self._name: (0, info_dict['length'], True), self._name: (0, info_dict[b'length'], True),
} }
self._files = [] self._files = []
if 'files' in info_dict: if b'files' in info_dict:
prefix = '' prefix = ''
if len(info_dict['files']) > 1: if len(info_dict[b'files']) > 1:
prefix = self._name prefix = self._name
for f in info_dict['files']: for f in info_dict[b'files']:
self._files.append({ self._files.append({
'path': f['path'], 'path': f[b'path'],
'size': f['length'], 'size': f[b'length'],
'download': True, 'download': True,
}) })
else: else:
self._files.append({ self._files.append({
'path': self._name, 'path': self._name,
'size': info_dict['length'], 'size': info_dict[b'length'],
'download': True, 'download': True,
}) })