diff options
Diffstat (limited to 'MAT')
| -rw-r--r-- | MAT/audio.py | 74 | ||||
| -rw-r--r-- | MAT/mutagenpowered.py | 35 | ||||
| -rw-r--r-- | MAT/strippers.py | 1 |
3 files changed, 52 insertions, 58 deletions
diff --git a/MAT/audio.py b/MAT/audio.py index ed849ee..4df33dd 100644 --- a/MAT/audio.py +++ b/MAT/audio.py | |||
| @@ -1,15 +1,15 @@ | |||
| 1 | ''' | 1 | ''' |
| 2 | Care about audio fileformat | 2 | Care about audio fileformat |
| 3 | ''' | 3 | ''' |
| 4 | |||
| 4 | try: | 5 | try: |
| 5 | from mutagen.flac import FLAC | 6 | from mutagen.flac import FLAC |
| 6 | from mutagen.oggvorbis import OggVorbis | 7 | from mutagen.oggvorbis import OggVorbis |
| 7 | except ImportError: | 8 | except ImportError: |
| 8 | pass | 9 | pass |
| 9 | 10 | ||
| 10 | |||
| 11 | import parser | 11 | import parser |
| 12 | import shutil | 12 | import mutagenpowered |
| 13 | 13 | ||
| 14 | 14 | ||
| 15 | class MpegAudioStripper(parser.GenericParser): | 15 | class MpegAudioStripper(parser.GenericParser): |
| @@ -17,84 +17,44 @@ class MpegAudioStripper(parser.GenericParser): | |||
| 17 | Represent mpeg audio file (mp3, ...) | 17 | Represent mpeg audio file (mp3, ...) |
| 18 | ''' | 18 | ''' |
| 19 | def _should_remove(self, field): | 19 | def _should_remove(self, field): |
| 20 | if field.name in ("id3v1", "id3v2"): | 20 | return field.name in ("id3v1", "id3v2") |
| 21 | return True | ||
| 22 | else: | ||
| 23 | return False | ||
| 24 | 21 | ||
| 25 | 22 | ||
| 26 | class OggStripper(parser.GenericParser): | 23 | class OggStripper(mutagenpowered.MutagenStripper): |
| 27 | ''' | 24 | ''' |
| 28 | Represent an ogg vorbis file | 25 | Represent an ogg vorbis file |
| 29 | ''' | 26 | ''' |
| 30 | def remove_all(self): | 27 | def _create_mfile(self): |
| 31 | if self.backup is True: | 28 | self.mfile = OggVorbis(self.filename) |
| 32 | shutil.copy2(self.filename, self.output) | ||
| 33 | self.filename = self.output | ||
| 34 | |||
| 35 | mfile = OggVorbis(self.filename) | ||
| 36 | mfile.delete() | ||
| 37 | mfile.save() | ||
| 38 | return True | ||
| 39 | |||
| 40 | def is_clean(self): | ||
| 41 | ''' | ||
| 42 | Check if the "metadata" block is present in the file | ||
| 43 | ''' | ||
| 44 | mfile = OggVorbis(self.filename) | ||
| 45 | if mfile.tags == []: | ||
| 46 | return True | ||
| 47 | else: | ||
| 48 | return False | ||
| 49 | |||
| 50 | def get_meta(self): | ||
| 51 | ''' | ||
| 52 | Return the content of the metadata block if present | ||
| 53 | ''' | ||
| 54 | metadata = {} | ||
| 55 | mfile = OggVorbis(self.filename) | ||
| 56 | for key, value in mfile.tags: | ||
| 57 | metadata[key] = value | ||
| 58 | return metadata | ||
| 59 | 29 | ||
| 60 | 30 | ||
| 61 | class FlacStripper(parser.GenericParser): | 31 | class FlacStripper(mutagenpowered.MutagenStripper): |
| 62 | ''' | 32 | ''' |
| 63 | Represent a Flac audio file | 33 | Represent a Flac audio file |
| 64 | ''' | 34 | ''' |
| 35 | def _create_mfile(self): | ||
| 36 | self.mfile = FLAC(self.filename) | ||
| 37 | |||
| 65 | def remove_all(self): | 38 | def remove_all(self): |
| 66 | ''' | 39 | ''' |
| 67 | Remove the "metadata" block from the file | 40 | Remove the "metadata" block from the file |
| 68 | ''' | 41 | ''' |
| 69 | if self.backup is True: | 42 | super(FlacStripper, self).remove_all() |
| 70 | shutil.copy2(self.filename, self.output) | 43 | self.mfile.clear_pictures() |
| 71 | self.filename = self.output | 44 | self.mfile.save() |
| 72 | |||
| 73 | mfile = FLAC(self.filename) | ||
| 74 | mfile.delete() | ||
| 75 | mfile.clear_pictures() | ||
| 76 | mfile.save() | ||
| 77 | return True | 45 | return True |
| 78 | 46 | ||
| 79 | def is_clean(self): | 47 | def is_clean(self): |
| 80 | ''' | 48 | ''' |
| 81 | Check if the "metadata" block is present in the file | 49 | Check if the "metadata" block is present in the file |
| 82 | ''' | 50 | ''' |
| 83 | mfile = FLAC(self.filename) | 51 | return super(FlacStripper, self).is_clean() and not self.mfile.pictures |
| 84 | if mfile.tags is None and mfile.pictures == []: | ||
| 85 | return True | ||
| 86 | else: | ||
| 87 | return False | ||
| 88 | 52 | ||
| 89 | def get_meta(self): | 53 | def get_meta(self): |
| 90 | ''' | 54 | ''' |
| 91 | Return the content of the metadata block if present | 55 | Return the content of the metadata block if present |
| 92 | ''' | 56 | ''' |
| 93 | metadata = {} | 57 | metadata = super(FlacStripper, self).get_meta() |
| 94 | mfile = FLAC(self.filename) | 58 | if self.mfile.pictures: |
| 95 | if mfile.tags is not None: | 59 | metadata['picture:'] = 'yes' |
| 96 | if mfile.pictures != []: | ||
| 97 | metadata['picture :'] = 'yes' | ||
| 98 | for key, value in mfile.tags: | ||
| 99 | metadata[key] = value | ||
| 100 | return metadata | 60 | return metadata |
diff --git a/MAT/mutagenpowered.py b/MAT/mutagenpowered.py new file mode 100644 index 0000000..d44bbdb --- /dev/null +++ b/MAT/mutagenpowered.py | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | import parser | ||
| 2 | import shutil | ||
| 3 | |||
| 4 | |||
| 5 | class MutagenStripper(parser.GenericParser): | ||
| 6 | def __init__(self, filename, parser, mime, backup, **kwargs): | ||
| 7 | super(MutagenStripper, self).__init__(filename, parser, mime, backup, **kwargs) | ||
| 8 | self._create_mfile() | ||
| 9 | |||
| 10 | def _create_mfile(self): | ||
| 11 | raise NotImplemented | ||
| 12 | |||
| 13 | def is_clean(self): | ||
| 14 | return not self.mfile.tags | ||
| 15 | |||
| 16 | def remove_all(self): | ||
| 17 | if self.backup: | ||
| 18 | shutil.copy2(self.filename, self.output) | ||
| 19 | self.mfile.filename = self.output | ||
| 20 | else: | ||
| 21 | self.mfile.filename = self.filename | ||
| 22 | |||
| 23 | self.mfile.delete() | ||
| 24 | self.mfile.save() | ||
| 25 | return True | ||
| 26 | |||
| 27 | def get_meta(self): | ||
| 28 | ''' | ||
| 29 | Return the content of the metadata block is present | ||
| 30 | ''' | ||
| 31 | metadata = {} | ||
| 32 | if self.mfile.tags: | ||
| 33 | for key, value in self.mfile.tags: | ||
| 34 | metadata[key] = value | ||
| 35 | return metadata | ||
diff --git a/MAT/strippers.py b/MAT/strippers.py index 4b673fe..2bd17e1 100644 --- a/MAT/strippers.py +++ b/MAT/strippers.py | |||
| @@ -64,4 +64,3 @@ except OSError: # if exiftool is not installed, use hachoir | |||
| 64 | print('Unable to find exiftool: limited images support') | 64 | print('Unable to find exiftool: limited images support') |
| 65 | STRIPPERS['image/jpeg'] = images.JpegStripper | 65 | STRIPPERS['image/jpeg'] = images.JpegStripper |
| 66 | STRIPPERS['image/png'] = images.PngStripper | 66 | STRIPPERS['image/png'] = images.PngStripper |
| 67 | |||
