diff options
Diffstat (limited to 'lib/misc.py')
| -rw-r--r-- | lib/misc.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/misc.py b/lib/misc.py new file mode 100644 index 0000000..ce14313 --- /dev/null +++ b/lib/misc.py | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | import hachoir_core | ||
| 2 | import parser | ||
| 3 | |||
| 4 | |||
| 5 | class TorrentStripper(parser.Generic_parser): | ||
| 6 | ''' | ||
| 7 | A torrent file looks like: | ||
| 8 | -root | ||
| 9 | -start | ||
| 10 | -announce | ||
| 11 | -announce-list | ||
| 12 | -comment | ||
| 13 | -created_by | ||
| 14 | -creation_date | ||
| 15 | -encoding | ||
| 16 | -info | ||
| 17 | -end | ||
| 18 | ''' | ||
| 19 | def remove_all(self): | ||
| 20 | for field in self.editor['root']: | ||
| 21 | if self._should_remove(field): | ||
| 22 | #FIXME : hachoir does not support torrent metadata editing :< | ||
| 23 | del self.editor['/root/' + field.name] | ||
| 24 | hachoir_core.field.writeIntoFile(self.editor, | ||
| 25 | self.filename + parser.POSTFIX) | ||
| 26 | self.do_backup() | ||
| 27 | |||
| 28 | def is_clean(self): | ||
| 29 | for field in self.editor['root']: | ||
| 30 | if self._should_remove(field): | ||
| 31 | return False | ||
| 32 | return True | ||
| 33 | |||
| 34 | def get_meta(self): | ||
| 35 | metadata = {} | ||
| 36 | for field in self.editor['root']: | ||
| 37 | if self._should_remove(field): | ||
| 38 | try: # FIXME | ||
| 39 | metadata[field.name] = field.value | ||
| 40 | except: | ||
| 41 | metadata[field.name] = 'harmful content' | ||
| 42 | return metadata | ||
| 43 | |||
| 44 | def _should_remove(self, field): | ||
| 45 | if field.name in ('comment', 'created_by', 'creation_date', 'info'): | ||
| 46 | return True | ||
| 47 | else: | ||
| 48 | return False | ||
