diff options
| -rw-r--r-- | MAT/misc.py | 78 | ||||
| -rw-r--r-- | test/clean é.torrent | bin | 54609 -> 54565 bytes |
2 files changed, 46 insertions, 32 deletions
diff --git a/MAT/misc.py b/MAT/misc.py index e016d18..b0c22f4 100644 --- a/MAT/misc.py +++ b/MAT/misc.py | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | ''' | 1 | ''' Care about misc formats |
| 2 | Care about misc formats | ||
| 3 | ''' | 2 | ''' |
| 4 | 3 | ||
| 5 | import parser | 4 | import parser |
| @@ -8,55 +7,70 @@ from bencode import bencode | |||
| 8 | 7 | ||
| 9 | 8 | ||
| 10 | class TorrentStripper(parser.GenericParser): | 9 | class TorrentStripper(parser.GenericParser): |
| 11 | ''' | 10 | ''' Represent a torrent file with the help |
| 12 | Represent a torrent file with the help | ||
| 13 | of the bencode lib from Petru Paler | 11 | of the bencode lib from Petru Paler |
| 14 | ''' | 12 | ''' |
| 15 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): | 13 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): |
| 16 | super(TorrentStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) | 14 | super(TorrentStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) |
| 17 | self.fields = ['comment', 'creation date', 'created by'] | 15 | self.fields = frozenset(['announce', 'info', 'name', 'path', 'piece length', 'pieces', |
| 16 | 'length', 'files', 'announce-list', 'nodes', 'httpseeds', 'private', 'root hash']) | ||
| 18 | 17 | ||
| 19 | def is_clean(self): | 18 | def __get_key_recursively(self, dictionary): |
| 19 | ''' Get recursively all keys from a dict and | ||
| 20 | its subdicts | ||
| 20 | ''' | 21 | ''' |
| 21 | Check if the file is clean from harmful metadatas | 22 | for (i,j) in dictionary.items(): |
| 23 | if isinstance(j, dict): | ||
| 24 | return set([i]).union(self.__get_key_recursively(j)) | ||
| 25 | return set([i]) | ||
| 26 | |||
| 27 | def is_clean(self): | ||
| 28 | ''' Check if the file is clean from harmful metadata | ||
| 22 | ''' | 29 | ''' |
| 23 | with open(self.filename, 'r') as f: | 30 | with open(self.filename, 'r') as f: |
| 24 | decoded = bencode.bdecode(f.read()) | 31 | decoded = bencode.bdecode(f.read()) |
| 25 | for key in self.fields: | 32 | return self.fields.issuperset(self.__get_key_recursively(decoded)) |
| 26 | try: | ||
| 27 | if decoded[key]: | ||
| 28 | return False | ||
| 29 | except KeyError: | ||
| 30 | pass | ||
| 31 | return True | ||
| 32 | 33 | ||
| 33 | def get_meta(self): | 34 | def __get_meta_recursively(self, dictionary): |
| 35 | ''' Get recursively all harmful metadata | ||
| 34 | ''' | 36 | ''' |
| 35 | Return a dict with all the meta of the file | 37 | d = dict() |
| 38 | for(i,j) in dictionary.items(): | ||
| 39 | if i not in self.fields: | ||
| 40 | d[i] = j | ||
| 41 | elif isinstance(j, dict): | ||
| 42 | d = dict(d.items() + self.__get_meta_recursively(j).items()) | ||
| 43 | return d | ||
| 44 | |||
| 45 | def get_meta(self): | ||
| 46 | ''' Return a dict with all the meta of the file | ||
| 36 | ''' | 47 | ''' |
| 37 | metadata = {} | ||
| 38 | with open(self.filename, 'r') as f: | 48 | with open(self.filename, 'r') as f: |
| 39 | decoded = bencode.bdecode(f.read()) | 49 | decoded = bencode.bdecode(f.read()) |
| 40 | for key in self.fields: | 50 | return self.__get_meta_recursively(decoded) |
| 41 | try: | ||
| 42 | if decoded[key]: | ||
| 43 | metadata[key] = decoded[key] | ||
| 44 | except KeyError: | ||
| 45 | pass | ||
| 46 | return metadata | ||
| 47 | 51 | ||
| 48 | def remove_all(self): | 52 | def __remove_all_recursively(self, dictionary): |
| 53 | ''' Remove recursively all compromizing fields | ||
| 49 | ''' | 54 | ''' |
| 50 | Remove all the files that are compromising | 55 | d = dict() |
| 56 | for(i,j) in filter(lambda i: i in self.fields, dictionary.items()): | ||
| 57 | if isinstance(j, dict): | ||
| 58 | d = dict(d.items() + self.__get_meta_recursively(j).items()) | ||
| 59 | else: | ||
| 60 | d[i] = j | ||
| 61 | return d | ||
| 62 | |||
| 63 | def remove_all(self): | ||
| 64 | ''' Remove all comprimizing fields | ||
| 51 | ''' | 65 | ''' |
| 66 | decoded = '' | ||
| 52 | with open(self.filename, 'r') as f: | 67 | with open(self.filename, 'r') as f: |
| 53 | decoded = bencode.bdecode(f.read()) | 68 | decoded = bencode.bdecode(f.read()) |
| 54 | for key in self.fields: | 69 | |
| 55 | try: | 70 | cleaned = {i:j for i,j in decoded.items() if i in self.fields} |
| 56 | decoded[key] = '' | 71 | |
| 57 | except KeyError: | ||
| 58 | pass | ||
| 59 | with open(self.output, 'w') as f: # encode the decoded torrent | 72 | with open(self.output, 'w') as f: # encode the decoded torrent |
| 60 | f.write(bencode.bencode(decoded)) # and write it in self.output | 73 | f.write(bencode.bencode(cleaned)) # and write it in self.output |
| 74 | |||
| 61 | self.do_backup() | 75 | self.do_backup() |
| 62 | return True | 76 | return True |
diff --git a/test/clean é.torrent b/test/clean é.torrent index e6324f7..428a709 100644 --- a/test/clean é.torrent +++ b/test/clean é.torrent | |||
| Binary files differ | |||
