diff options
Diffstat (limited to 'MAT/misc.py')
| -rw-r--r-- | MAT/misc.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/MAT/misc.py b/MAT/misc.py new file mode 100644 index 0000000..d084861 --- /dev/null +++ b/MAT/misc.py | |||
| @@ -0,0 +1,63 @@ | |||
| 1 | ''' | ||
| 2 | Care about misc formats | ||
| 3 | ''' | ||
| 4 | |||
| 5 | import parser | ||
| 6 | |||
| 7 | from bencode import bencode | ||
| 8 | |||
| 9 | |||
| 10 | class TorrentStripper(parser.GenericParser): | ||
| 11 | ''' | ||
| 12 | Represent a torrent file with the help | ||
| 13 | of the bencode lib from Petru Paler | ||
| 14 | ''' | ||
| 15 | def __init__(self, filename, parser, mime, backup, add2archive): | ||
| 16 | super(TorrentStripper, self).__init__(filename, parser, mime, | ||
| 17 | backup, add2archive) | ||
| 18 | self.fields = ['comment', 'creation date', 'created by'] | ||
| 19 | |||
| 20 | def is_clean(self): | ||
| 21 | ''' | ||
| 22 | Check if the file is clean from harmful metadatas | ||
| 23 | ''' | ||
| 24 | with open(self.filename, 'r') as f: | ||
| 25 | decoded = bencode.bdecode(f.read()) | ||
| 26 | for key in self.fields: | ||
| 27 | try: | ||
| 28 | if decoded[key] != '': | ||
| 29 | return False | ||
| 30 | except: | ||
| 31 | pass | ||
| 32 | return True | ||
| 33 | |||
| 34 | def get_meta(self): | ||
| 35 | ''' | ||
| 36 | Return a dict with all the meta of the file | ||
| 37 | ''' | ||
| 38 | metadata = {} | ||
| 39 | with open(self.filename, 'r') as f: | ||
| 40 | decoded = bencode.bdecode(f.read()) | ||
| 41 | for key in self.fields: | ||
| 42 | try: | ||
| 43 | if decoded[key] != '': | ||
| 44 | metadata[key] = decoded[key] | ||
| 45 | except: | ||
| 46 | pass | ||
| 47 | return metadata | ||
| 48 | |||
| 49 | def remove_all(self): | ||
| 50 | ''' | ||
| 51 | Remove all the files that are compromizing | ||
| 52 | ''' | ||
| 53 | with open(self.filename, 'r') as f: | ||
| 54 | decoded = bencode.bdecode(f.read()) | ||
| 55 | for key in self.fields: | ||
| 56 | try: | ||
| 57 | decoded[key] = '' | ||
| 58 | except: | ||
| 59 | pass | ||
| 60 | with open(self.output, 'w') as f: # encode the decoded torrent | ||
| 61 | f.write(bencode.bencode(decoded)) # and write it in self.output | ||
| 62 | self.do_backup() | ||
| 63 | return True | ||
