diff options
| author | jvoisin | 2011-08-15 00:00:58 +0200 |
|---|---|---|
| committer | jvoisin | 2011-08-15 00:00:58 +0200 |
| commit | ffaf119905d3f2bea5ca6fca7c1ec092be407aa7 (patch) | |
| tree | 53b2a02e3677bc0ca22d2aac44135af81aca52d3 /lib/misc.py | |
| parent | 772019b088f6b36df7e1d9c2a844ca5669a5e904 (diff) | |
Add support for torrent files (thanks to the nice lib from Petru Paler)
Diffstat (limited to 'lib/misc.py')
| -rw-r--r-- | lib/misc.py | 74 |
1 files changed, 41 insertions, 33 deletions
diff --git a/lib/misc.py b/lib/misc.py index 963800e..c92182a 100644 --- a/lib/misc.py +++ b/lib/misc.py | |||
| @@ -2,36 +2,33 @@ | |||
| 2 | Care about misc formats | 2 | Care about misc formats |
| 3 | ''' | 3 | ''' |
| 4 | 4 | ||
| 5 | import hachoir_core | ||
| 6 | import parser | 5 | import parser |
| 7 | 6 | ||
| 7 | import bencode | ||
| 8 | |||
| 8 | 9 | ||
| 9 | class TorrentStripper(parser.GenericParser): | 10 | class TorrentStripper(parser.GenericParser): |
| 10 | ''' | 11 | ''' |
| 11 | A torrent file looks like: | 12 | Represent a torrent file with the help |
| 12 | -root | 13 | of the bencode lib from Petru Paler |
| 13 | -start | ||
| 14 | -announce | ||
| 15 | -announce-list | ||
| 16 | -comment | ||
| 17 | -created_by | ||
| 18 | -creation_date | ||
| 19 | -encoding | ||
| 20 | -info | ||
| 21 | -end | ||
| 22 | ''' | 14 | ''' |
| 23 | def remove_all(self): | 15 | def __init__(self, filename, parser, mime, backup, add2archive): |
| 24 | for field in self.editor['root']: | 16 | super(TorrentStripper, self).__init__(filename, parser, mime, |
| 25 | if self._should_remove(field): | 17 | backup, add2archive) |
| 26 | #FIXME : hachoir does not support torrent metadata editing :< | 18 | self.fields = ['comment', 'creation date', 'created by'] |
| 27 | del self.editor['/root/' + field.name] | ||
| 28 | hachoir_core.field.writeIntoFile(self.editor, self.output) | ||
| 29 | self.do_backup() | ||
| 30 | 19 | ||
| 31 | def is_clean(self): | 20 | def is_clean(self): |
| 32 | for field in self.editor['root']: | 21 | ''' |
| 33 | if self._should_remove(field): | 22 | Check if the file is clean from harmful metadatas |
| 34 | return False | 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 | ||
| 35 | return True | 32 | return True |
| 36 | 33 | ||
| 37 | def get_meta(self): | 34 | def get_meta(self): |
| @@ -39,16 +36,27 @@ class TorrentStripper(parser.GenericParser): | |||
| 39 | Return a dict with all the meta of the file | 36 | Return a dict with all the meta of the file |
| 40 | ''' | 37 | ''' |
| 41 | metadata = {} | 38 | metadata = {} |
| 42 | for field in self.editor['root']: | 39 | with open(self.filename, 'r') as f: |
| 43 | if self._should_remove(field): | 40 | decoded = bencode.bdecode(f.read()) |
| 44 | try: # FIXME | 41 | for key in self.fields: |
| 45 | metadata[field.name] = field.value | 42 | try: |
| 46 | except: | 43 | if decoded[key] != '': |
| 47 | metadata[field.name] = 'harmful content' | 44 | metadata[key] = decoded[key] |
| 45 | except: | ||
| 46 | pass | ||
| 48 | return metadata | 47 | return metadata |
| 49 | 48 | ||
| 50 | def _should_remove(self, field): | 49 | def remove_all(self): |
| 51 | if field.name in ('comment', 'created_by', 'creation_date', 'info'): | 50 | ''' |
| 52 | return True | 51 | Remove all the files that are compromizing |
| 53 | else: | 52 | ''' |
| 54 | return False | 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() | ||
