diff options
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/torrent.py | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/libmat2/torrent.py b/libmat2/torrent.py index ad49f47..d614136 100644 --- a/libmat2/torrent.py +++ b/libmat2/torrent.py | |||
| @@ -8,13 +8,16 @@ class TorrentParser(abstract.AbstractParser): | |||
| 8 | mimetypes = {'application/x-bittorrent', } | 8 | mimetypes = {'application/x-bittorrent', } |
| 9 | whitelist = {b'announce', b'announce-list', b'info'} | 9 | whitelist = {b'announce', b'announce-list', b'info'} |
| 10 | 10 | ||
| 11 | def __init__(self, filename): | ||
| 12 | super().__init__(filename) | ||
| 13 | with open(self.filename, 'rb') as f: | ||
| 14 | self.dict_repr = _BencodeHandler().bdecode(f.read()) | ||
| 15 | if self.dict_repr is None: | ||
| 16 | raise ValueError | ||
| 17 | |||
| 11 | def get_meta(self) -> Dict[str, str]: | 18 | def get_meta(self) -> Dict[str, str]: |
| 12 | metadata = {} | 19 | metadata = {} |
| 13 | with open(self.filename, 'rb') as f: | 20 | for k, v in self.dict_repr.items(): |
| 14 | d = _BencodeHandler().bdecode(f.read()) | ||
| 15 | if d is None: | ||
| 16 | return {'Unknown meta': 'Unable to parse torrent file "%s".' % self.filename} | ||
| 17 | for k, v in d.items(): | ||
| 18 | if k not in self.whitelist: | 21 | if k not in self.whitelist: |
| 19 | metadata[k.decode('utf-8')] = v | 22 | metadata[k.decode('utf-8')] = v |
| 20 | return metadata | 23 | return metadata |
| @@ -22,15 +25,12 @@ class TorrentParser(abstract.AbstractParser): | |||
| 22 | 25 | ||
| 23 | def remove_all(self) -> bool: | 26 | def remove_all(self) -> bool: |
| 24 | cleaned = dict() | 27 | cleaned = dict() |
| 25 | with open(self.filename, 'rb') as f: | 28 | for k, v in self.dict_repr.items(): |
| 26 | d = _BencodeHandler().bdecode(f.read()) | ||
| 27 | if d is None: | ||
| 28 | return False | ||
| 29 | for k, v in d.items(): | ||
| 30 | if k in self.whitelist: | 29 | if k in self.whitelist: |
| 31 | cleaned[k] = v | 30 | cleaned[k] = v |
| 32 | with open(self.output_filename, 'wb') as f: | 31 | with open(self.output_filename, 'wb') as f: |
| 33 | f.write(_BencodeHandler().bencode(cleaned)) | 32 | f.write(_BencodeHandler().bencode(cleaned)) |
| 33 | self.dict_repr = cleaned # since we're stateful | ||
| 34 | return True | 34 | return True |
| 35 | 35 | ||
| 36 | 36 | ||
