diff options
| -rw-r--r-- | lib/mat.py | 2 | ||||
| -rw-r--r-- | lib/misc.py | 74 | ||||
| -rw-r--r-- | test/clean.torrent | bin | 0 -> 54609 bytes | |||
| -rw-r--r-- | test/dirty.torrent | bin | 0 -> 54622 bytes |
4 files changed, 43 insertions, 33 deletions
| @@ -17,6 +17,7 @@ import images | |||
| 17 | import audio | 17 | import audio |
| 18 | import office | 18 | import office |
| 19 | import archive | 19 | import archive |
| 20 | import misc | ||
| 20 | 21 | ||
| 21 | __version__ = '0.1' | 22 | __version__ = '0.1' |
| 22 | __author__ = 'jvoisin' | 23 | __author__ = 'jvoisin' |
| @@ -33,6 +34,7 @@ STRIPPERS = { | |||
| 33 | 'audio/mpeg': audio.MpegAudioStripper, | 34 | 'audio/mpeg': audio.MpegAudioStripper, |
| 34 | 'image/jpeg': images.JpegStripper, | 35 | 'image/jpeg': images.JpegStripper, |
| 35 | 'image/png': images.PngStripper, | 36 | 'image/png': images.PngStripper, |
| 37 | 'application/x-bittorrent': misc.TorrentStripper, | ||
| 36 | 'application/opendocument': office.OpenDocumentStripper, | 38 | 'application/opendocument': office.OpenDocumentStripper, |
| 37 | 'application/officeopenxml': office.OpenXmlStripper, | 39 | 'application/officeopenxml': office.OpenXmlStripper, |
| 38 | } | 40 | } |
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() | ||
diff --git a/test/clean.torrent b/test/clean.torrent new file mode 100644 index 0000000..e6324f7 --- /dev/null +++ b/test/clean.torrent | |||
| Binary files differ | |||
diff --git a/test/dirty.torrent b/test/dirty.torrent new file mode 100644 index 0000000..bd056a0 --- /dev/null +++ b/test/dirty.torrent | |||
| Binary files differ | |||
