1
0
Fork 0
mat2/libmat2/torrent.py

128 lines
4.0 KiB
Python
Raw Normal View History

2018-06-04 22:54:01 +02:00
from typing import Union, Tuple, Dict
2018-04-22 23:48:01 +02:00
from . import abstract
2018-04-22 22:02:00 +02:00
class TorrentParser(abstract.AbstractParser):
2018-06-04 22:54:01 +02:00
mimetypes = {'application/x-bittorrent', }
2018-04-22 22:02:00 +02:00
whitelist = {b'announce', b'announce-list', b'info'}
2018-06-04 22:54:01 +02:00
def get_meta(self) -> Dict[str, str]:
2018-04-22 22:02:00 +02:00
metadata = {}
with open(self.filename, 'rb') as f:
2018-04-22 23:48:01 +02:00
d = _BencodeHandler().bdecode(f.read())
if d is None:
return {'Unknown meta': 'Unable to parse torrent file "%s".' % self.filename}
2018-05-16 22:36:59 +02:00
for k, v in d.items():
2018-04-22 22:02:00 +02:00
if k not in self.whitelist:
metadata[k.decode('utf-8')] = v
return metadata
2018-04-22 23:48:01 +02:00
def remove_all(self) -> bool:
2018-04-22 22:02:00 +02:00
cleaned = dict()
with open(self.filename, 'rb') as f:
2018-04-22 23:48:01 +02:00
d = _BencodeHandler().bdecode(f.read())
if d is None:
return False
2018-05-16 22:36:59 +02:00
for k, v in d.items():
2018-04-22 22:02:00 +02:00
if k in self.whitelist:
cleaned[k] = v
with open(self.output_filename, 'wb') as f:
2018-04-22 23:48:01 +02:00
f.write(_BencodeHandler().bencode(cleaned))
2018-04-22 22:02:00 +02:00
return True
2018-04-22 23:48:01 +02:00
class _BencodeHandler(object):
"""
Since bencode isn't that hard to parse,
MAT2 comes with its own parser, based on the spec
https://wiki.theory.org/index.php/BitTorrentSpecification#Bencoding
"""
def __init__(self):
self.__decode_func = {
2018-05-16 22:36:59 +02:00
ord('d'): self.__decode_dict,
ord('i'): self.__decode_int,
ord('l'): self.__decode_list,
}
2018-04-22 23:48:01 +02:00
for i in range(0, 10):
self.__decode_func[ord(str(i))] = self.__decode_string
self.__encode_func = {
2018-05-16 22:36:59 +02:00
bytes: self.__encode_string,
dict: self.__encode_dict,
int: self.__encode_int,
list: self.__encode_list,
2018-04-22 23:48:01 +02:00
}
2018-05-16 22:36:59 +02:00
@staticmethod
2018-06-04 22:54:01 +02:00
def __decode_int(s: bytes) -> Tuple[int, bytes]:
2018-04-22 22:02:00 +02:00
s = s[1:]
next_idx = s.index(b'e')
if s.startswith(b'-0'):
raise ValueError # negative zero doesn't exist
2018-04-22 23:48:01 +02:00
elif s.startswith(b'0') and next_idx != 1:
2018-04-22 22:02:00 +02:00
raise ValueError # no leading zero except for zero itself
return int(s[:next_idx]), s[next_idx+1:]
2018-05-16 22:36:59 +02:00
@staticmethod
2018-06-04 22:54:01 +02:00
def __decode_string(s: bytes) -> Tuple[bytes, bytes]:
2018-04-22 23:48:01 +02:00
sep = s.index(b':')
str_len = int(s[:sep])
if str_len < 0:
2018-04-22 22:02:00 +02:00
raise ValueError
2018-04-22 23:48:01 +02:00
elif s[0] == b'0' and sep != 1:
raise ValueError
s = s[1:]
return s[sep:sep+str_len], s[sep+str_len:]
2018-04-22 22:02:00 +02:00
2018-06-04 22:54:01 +02:00
def __decode_list(self, s: bytes) -> Tuple[list, bytes]:
2018-04-22 22:02:00 +02:00
r = list()
s = s[1:] # skip leading `l`
while s[0] != ord('e'):
v, s = self.__decode_func[s[0]](s)
r.append(v)
return r, s[1:]
2018-06-04 22:54:01 +02:00
def __decode_dict(self, s: bytes) -> Tuple[dict, bytes]:
2018-04-22 22:02:00 +02:00
r = dict()
2018-04-22 23:48:01 +02:00
s = s[1:] # skip leading `d`
2018-04-22 22:02:00 +02:00
while s[0] != ord(b'e'):
k, s = self.__decode_string(s)
r[k], s = self.__decode_func[s[0]](s)
return r, s[1:]
@staticmethod
2018-06-04 22:54:01 +02:00
def __encode_int(x: bytes) -> bytes:
2018-04-22 22:02:00 +02:00
return b'i' + bytes(str(x), 'utf-8') + b'e'
@staticmethod
2018-06-04 22:54:01 +02:00
def __encode_string(x: bytes) -> bytes:
2018-04-22 22:02:00 +02:00
return bytes((str(len(x))), 'utf-8') + b':' + x
2018-05-16 22:36:59 +02:00
def __encode_list(self, x: str) -> bytes:
2018-04-22 22:02:00 +02:00
ret = b''
for i in x:
ret += self.__encode_func[type(i)](i)
return b'l' + ret + b'e'
2018-06-04 22:54:01 +02:00
def __encode_dict(self, x: dict) -> bytes:
2018-04-22 22:02:00 +02:00
ret = b''
for k, v in sorted(x.items()):
ret += self.__encode_func[type(k)](k)
ret += self.__encode_func[type(v)](v)
return b'd' + ret + b'e'
2018-06-04 22:54:01 +02:00
def bencode(self, s: Union[dict, list, bytes, int]) -> bytes:
2018-04-22 23:48:01 +02:00
return self.__encode_func[type(s)](s)
2018-04-22 22:02:00 +02:00
2018-06-04 22:54:01 +02:00
def bdecode(self, s: bytes) -> Union[dict, None]:
2018-04-22 23:48:01 +02:00
try:
r, l = self.__decode_func[s[0]](s)
except (IndexError, KeyError, ValueError) as e:
print("not a valid bencoded string: %s" % e)
return None
if l != b'':
print("invalid bencoded value (data after valid prefix)")
return None
return r