6 Commits
Author SHA1 Message Date
7sDream 817d816d7f update to version 0.1.3 2017-06-21 00:52:08 +08:00
7sDream 98cd6345b8 Fix Issue #2 and do some refractor
- Fix Issue #2, use IOError instead of FileNotFoundError in Python 2
- Use another private obj to represent end of list and dict
- InvalidTorrentFileException -> InvalidTorrentDataException
- Now UnicodeDecodeError is warpped in InvalidTorrentDataException
2017-06-21 00:45:23 +08:00
7sDream 6d9a68ba59 update to version 0.1.1 2017-06-20 15:43:18 +08:00
7sDream 29b4caccec Merge branch 'dev' 2017-06-20 15:42:07 +08:00
7sDream 3e076f65cd fix issue #1 2017-06-20 15:41:54 +08:00
7sDream 94e8cd7974 add encoding arg 2017-06-05 01:37:49 +08:00
3 changed files with 68 additions and 43 deletions

No files matched your search

+6 -12
View File
@@ -26,24 +26,18 @@ class Test(unittest.TestCase):
def test_parse_correctness(self): def test_parse_correctness(self):
data = parse_torrent_file(self.TEST_FILENAME) data = parse_torrent_file(self.TEST_FILENAME)
self.assertIn(['udp://p4p.arenabg.ch:1337/announce'], self.assertIn(['udp://tracker.publicbt.com:80/announce'],
data['announce-list']) data['announce-list'])
self.assertEqual(data['comment'], self.assertEqual(data['creation date'], 1409254242)
'Torrent downloaded from https://rarbg.to')
self.assertEqual(data['creation date'], 1472762993)
def test_parse_two_times(self): def test_parse_two_times(self):
with open(self.TEST_FILENAME, 'rb') as fp: with open(self.TEST_FILENAME, 'rb') as fp:
parser = TorrentFileParser(fp) parser = TorrentFileParser(fp)
data = parser.parse() data = parser.parse()
self.assertIn(['udp://p4p.arenabg.ch:1337/announce'], self.assertIn(['udp://tracker.publicbt.com:80/announce'],
data['announce-list']) data['announce-list'])
self.assertEqual(data['comment'], self.assertEqual(data['creation date'], 1409254242)
'Torrent downloaded from https://rarbg.to')
self.assertEqual(data['creation date'], 1472762993)
data = parser.parse() data = parser.parse()
self.assertIn(['udp://p4p.arenabg.ch:1337/announce'], self.assertIn(['udp://tracker.publicbt.com:80/announce'],
data['announce-list']) data['announce-list'])
self.assertEqual(data['comment'], self.assertEqual(data['creation date'], 1409254242)
'Torrent downloaded from https://rarbg.to')
self.assertEqual(data['creation date'], 1472762993)
BIN
View File
Binary file not shown.
+62 -31
View File
@@ -7,9 +7,9 @@ A .torrent file parser for both Python 2 and 3
Usage: Usage:
data = parse_torrent_file(filename) data = parse_torrent_file(filename)
# or # or
with open(filename, 'rb') as f: # the binary mode 'b' is necessary with open(filename, 'rb') as f: # the binary mode 'b' is necessary
data = TorrentFileParser(f).parse() data = TorrentFileParser(f).parse()
""" """
@@ -22,20 +22,33 @@ import io
import json import json
import sys import sys
try:
FileNotFoundError
except NameError:
# Python 2 do not have FileNotFoundError, use IOError instead
# noinspection PyShadowingBuiltins
FileNotFoundError = IOError
__all__ = [ __all__ = [
'InvalidTorrentFileException', 'InvalidTorrentDataException',
'parse_torrent_file', 'parse_torrent_file',
'TorrentFileParser', 'TorrentFileParser',
] ]
__version__ = '0.1.0' __version__ = '0.1.3'
class InvalidTorrentFileException(Exception): class InvalidTorrentDataException(Exception):
def __init__(self, pos, msg=None): def __init__(self, pos, msg=None):
msg = msg or "Invalid torrent format when reading at pos " + str(pos) msg = msg or "Invalid torrent format when read at pos {pos}"
super(InvalidTorrentFileException, self).__init__(msg) msg = msg.format(pos=pos)
super(InvalidTorrentDataException, self).__init__(msg)
class __EndCls(object):
pass
_END = __EndCls()
class TorrentFileParser(object): class TorrentFileParser(object):
@@ -62,7 +75,7 @@ class TorrentFileParser(object):
def __init__(self, fp, use_ordered_dict=False, encoding='utf-8'): def __init__(self, fp, use_ordered_dict=False, encoding='utf-8'):
""" """
:param fp: a **binary** file-like object to parse, :param fp: a **binary** file-like object to parse,
which means need 'b' mode when use built-in open function which means need 'b' mode when use built-in open function
:param encoding: file content encoding, default utf-8 :param encoding: file content encoding, default utf-8
:param use_ordered_dict: Use collections.OrderedDict as dict container :param use_ordered_dict: Use collections.OrderedDict as dict container
@@ -88,7 +101,7 @@ class TorrentFileParser(object):
try: try:
c = self._read_byte(1, True) c = self._read_byte(1, True)
raise InvalidTorrentFileException( raise InvalidTorrentDataException(
0, 'Expect EOF, but get [{}] at pos {}'.format(c, self._pos) 0, 'Expect EOF, but get [{}] at pos {}'.format(c, self._pos)
) )
except EOFError: # expect EOF except EOFError: # expect EOF
@@ -97,7 +110,7 @@ class TorrentFileParser(object):
if isinstance(data, dict): if isinstance(data, dict):
return data return data
raise InvalidTorrentFileException('Outermost element is not a dict') raise InvalidTorrentDataException('Outermost element is not a dict')
def _read_byte(self, count=1, raise_eof=False): def _read_byte(self, count=1, raise_eof=False):
assert count >= 0 assert count >= 0
@@ -105,7 +118,7 @@ class TorrentFileParser(object):
if count != 0 and len(gotten) == 0: if count != 0 and len(gotten) == 0:
if raise_eof: if raise_eof:
raise EOFError() raise EOFError()
raise InvalidTorrentFileException( raise InvalidTorrentDataException(
self._pos, self._pos,
'Unexpected EOF when reading torrent file' 'Unexpected EOF when reading torrent file'
) )
@@ -121,12 +134,15 @@ class TorrentFileParser(object):
def _dict_items_generator(self): def _dict_items_generator(self):
while True: while True:
try: k = self._next_element()
k = self._next_element() if k is _END:
except InvalidTorrentFileException:
return return
if k == 'pieces': if k == 'pieces':
v = self._pieces() v = self._next_hash()
elif k == 'ed2k':
v = self._next_hash(16, False)
elif k == 'filehash':
v = self._next_hash(20, False)
else: else:
v = self._next_element() v = self._next_element()
if k == 'encoding': if k == 'encoding':
@@ -141,9 +157,8 @@ class TorrentFileParser(object):
def _list_items_generator(self): def _list_items_generator(self):
while True: while True:
try: element = self._next_element()
element = self._next_element() if element is _END:
except InvalidTorrentFileException:
return return
yield element yield element
@@ -156,7 +171,7 @@ class TorrentFileParser(object):
while char != end: while char != end:
# noinspection PyTypeChecker # noinspection PyTypeChecker
if not b'0' <= char <= b'9': if not b'0' <= char <= b'9':
raise InvalidTorrentFileException(self._pos) raise InvalidTorrentDataException(self._pos - 1)
value = value * 10 + int(char) - int(b'0') value = value * 10 + int(char) - int(b'0')
char = self._read_byte(1) char = self._read_byte(1)
return value return value
@@ -165,7 +180,13 @@ class TorrentFileParser(object):
length = self._next_int(b':') length = self._next_int(b':')
raw = self._read_byte(length) raw = self._read_byte(length)
if decode: if decode:
string = raw.decode(self._encoding) try:
string = raw.decode(self._encoding)
except UnicodeDecodeError as e:
raise InvalidTorrentDataException(
self._pos - length + e.start,
"Fail to decode string at pos {pos} using " + e.encoding
)
return string return string
return raw return raw
@@ -173,17 +194,25 @@ class TorrentFileParser(object):
def __to_hex(v): def __to_hex(v):
return hex(ord(v) if isinstance(v, str) else v)[2:].rjust(2, str(0)) return hex(ord(v) if isinstance(v, str) else v)[2:].rjust(2, str(0))
def _pieces(self): def _next_hash(self, p_len=20, need_list=True):
raw = self._next_string(decode=False) raw = self._next_string(decode=False)
if len(raw) % 20 != 0: if len(raw) % p_len != 0:
raise InvalidTorrentFileException(self._pos) raise InvalidTorrentDataException(
return [ self._pos - len(raw), "Hash bit length not match at pos {pos}"
)
res = [
''.join([self.__to_hex(c) for c in h]) ''.join([self.__to_hex(c) for c in h])
for h in (raw[x:x+20] for x in range(0, len(raw), 20)) for h in (raw[x:x+p_len] for x in range(0, len(raw), p_len))
] ]
if len(res) == 0 and not need_list:
return ''
if len(res) == 1 and not need_list:
return res[0]
return res
def _next_end(self): @staticmethod
raise InvalidTorrentFileException(self._pos) def _next_end():
return _END
def _next_type(self): def _next_type(self):
for (element_type, indicator) in self.TYPES: for (element_type, indicator) in self.TYPES:
@@ -192,7 +221,7 @@ class TorrentFileParser(object):
if indicator == char: if indicator == char:
return element_type return element_type
self._seek_back(indicator_length) self._seek_back(indicator_length)
raise InvalidTorrentFileException(self._pos) raise InvalidTorrentDataException(self._pos)
def _type_to_func(self, t): def _type_to_func(self, t):
return getattr(self, '_next_' + t) return getattr(self, '_next_' + t)
@@ -206,7 +235,7 @@ class TorrentFileParser(object):
def parse_torrent_file(filename, use_ordered_dict=False): def parse_torrent_file(filename, use_ordered_dict=False):
""" """
Shortcut function for parse torrent object use TorrentFileParser Shortcut function for parse torrent object use TorrentFileParser
:param string filename: torrent filename :param string filename: torrent filename
:param bool use_ordered_dict: see :any:`TorrentFileParser.__init__` :param bool use_ordered_dict: see :any:`TorrentFileParser.__init__`
:rtype: dict if ``use_ordered_dict`` is false, :rtype: dict if ``use_ordered_dict`` is false,
@@ -229,6 +258,8 @@ def __main():
parser.add_argument('--ascii', '-a', action='store_true', default=False, parser.add_argument('--ascii', '-a', action='store_true', default=False,
help='ensure output json use ascii char, ' help='ensure output json use ascii char, '
'escape other char use \\u') 'escape other char use \\u')
parser.add_argument('--coding', '-c', default='utf-8',
help='string encoding, default utf-8')
parser.add_argument('--version', '-v', action='store_true', default=False, parser.add_argument('--version', '-v', action='store_true', default=False,
help='print version and exit') help='print version and exit')
args = parser.parse_args() args = parser.parse_args()
@@ -245,11 +276,11 @@ def __main():
else: else:
target_file = open(args.file, 'rb') target_file = open(args.file, 'rb')
except FileNotFoundError: except FileNotFoundError:
sys.stderr.write('Unable to find file {}\n'.format(args.file)) sys.stderr.write('File "{}" not exist\n'.format(args.file))
exit(1) exit(1)
# noinspection PyUnboundLocalVariable # noinspection PyUnboundLocalVariable
data = TorrentFileParser(target_file, not args.dict).parse() data = TorrentFileParser(target_file, not args.dict, args.coding).parse()
data = json.dumps( data = json.dumps(
data, ensure_ascii=args.ascii, data, ensure_ascii=args.ascii,