diff options
| author | jvoisin | 2011-07-23 00:36:13 +0200 |
|---|---|---|
| committer | jvoisin | 2011-07-23 00:36:13 +0200 |
| commit | 8862480570367359ae50759d60bc9b4486868600 (patch) | |
| tree | 0506a7fd50fb9fc919106b694178f59247a676d2 /lib/misc.py | |
| parent | a0c1d5576ccbd8f879f7d9cc59145f0a126f1b7c (diff) | |
Preliminary support for torrent (btw, I still hate hachoir)
Diffstat (limited to '')
| -rw-r--r-- | lib/misc.py | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/lib/misc.py b/lib/misc.py index a0e4a5b..de38129 100644 --- a/lib/misc.py +++ b/lib/misc.py | |||
| @@ -1,12 +1,63 @@ | |||
| 1 | import parser | ||
| 2 | import pdfrw | ||
| 3 | import os | 1 | import os |
| 4 | import mimetypes | 2 | import mimetypes |
| 5 | import mat | ||
| 6 | import subprocess | 3 | import subprocess |
| 7 | import tempfile | 4 | import tempfile |
| 8 | import glob | 5 | import glob |
| 9 | 6 | ||
| 7 | import hachoir_core | ||
| 8 | |||
| 9 | import pdfrw | ||
| 10 | import mat | ||
| 11 | import parser | ||
| 12 | |||
| 13 | class TorrentStripper(parser.Generic_parser): | ||
| 14 | ''' | ||
| 15 | A torrent file looks like: | ||
| 16 | -root | ||
| 17 | -start | ||
| 18 | -announce | ||
| 19 | -announce-list | ||
| 20 | -comment | ||
| 21 | -created_by | ||
| 22 | -creation_date | ||
| 23 | -encoding | ||
| 24 | -info | ||
| 25 | -end | ||
| 26 | ''' | ||
| 27 | def remove_all(self): | ||
| 28 | for field in self.editor['root']: | ||
| 29 | if self._should_remove(field): | ||
| 30 | #FIXME : hachoir does not support torrent metadata editing :< | ||
| 31 | del self.editor['/root/' + field.name] | ||
| 32 | hachoir_core.field.writeIntoFile(self.editor, | ||
| 33 | self.filename + parser.POSTFIX) | ||
| 34 | if self.backup is False: | ||
| 35 | mat.secure_remove(self.filename) #remove the old file | ||
| 36 | os.rename(self.filename + parser.POSTFIX, self.filename) | ||
| 37 | |||
| 38 | def is_clean(self): | ||
| 39 | for field in self.editor['root']: | ||
| 40 | if self._should_remove(field): | ||
| 41 | return False | ||
| 42 | return True | ||
| 43 | |||
| 44 | def get_meta(self): | ||
| 45 | metadata = {} | ||
| 46 | for field in self.editor['root']: | ||
| 47 | if self._should_remove(field): | ||
| 48 | try:#FIXME | ||
| 49 | metadata[field.name] = field.value | ||
| 50 | except: | ||
| 51 | metadata[field.name] = 'harmful content' | ||
| 52 | return metadata | ||
| 53 | |||
| 54 | def _should_remove(self, field): | ||
| 55 | if field.name in ('comment', 'created_by', 'creation_date', 'info'): | ||
| 56 | return True | ||
| 57 | else: | ||
| 58 | return False | ||
| 59 | |||
| 60 | |||
| 10 | class PdfStripper(parser.Generic_parser): | 61 | class PdfStripper(parser.Generic_parser): |
| 11 | ''' | 62 | ''' |
| 12 | Represent a pdf file, with the help of pdfrw | 63 | Represent a pdf file, with the help of pdfrw |
