summaryrefslogtreecommitdiff
path: root/libmat/mutagenstripper.py
blob: 692c56f0d0724b7665a8bf86c5c5e23b8beb2e0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
""" Take care of mutagen-supported formats (audio)
"""

import parser

from mutagen.flac import FLAC
from mutagen.oggvorbis import OggVorbis
from mutagen.mp3 import MP3


class MutagenStripper(parser.GenericParser):
    """ Parser using the (awesome) mutagen library. """
    def __init__(self, filename, mime, backup, is_writable, **kwargs):
        super(MutagenStripper, self).__init__(filename, mime, backup, is_writable, **kwargs)
        self.mfile = None  # This will be instanciated in self._create_mfile()
        self._create_mfile()

    def _create_mfile(self):
        """ This method must be overrriden to instanciate the `mfile` attribute."""
        raise NotImplementedError

    def is_clean(self):
        """ Check if the file is clean. """
        return not self.mfile.tags

    def remove_all(self):
        """ Remove all harmful metadata. """
        if self.backup:
            self.create_backup_copy()
        self.mfile.delete()
        self.mfile.save()
        return True

    def get_meta(self):
        """
            Return the content of the metadata block is present
        """
        metadata = {}
        if self.mfile.tags:
            for key, value in self.mfile.tags:
                metadata[key] = value
        return metadata


class MpegAudioStripper(MutagenStripper):
    """ Represent a mp3 vorbis file
    """
    def _create_mfile(self):
        self.mfile = MP3(self.filename)

    def get_meta(self):
        """
            Return the content of the metadata block is present
        """
        metadata = {}
        if self.mfile.tags:
            for key in self.mfile.tags.keys():
                meta = self.mfile.tags[key]
                try:  # Sometimes, the field has a human-redable description
                    desc = meta.desc
                except AttributeError:
                    desc = key
                text = meta.text[0]
                metadata[desc] = text
        return metadata


class OggStripper(MutagenStripper):
    """ Represent an ogg vorbis file
    """
    def _create_mfile(self):
        self.mfile = OggVorbis(self.filename)


class FlacStripper(MutagenStripper):
    """ Represent a Flac audio file
    """
    def _create_mfile(self):
        self.mfile = FLAC(self.filename)

    def remove_all(self):
        """ Remove the "metadata" block from the file
        """
        super(FlacStripper, self).remove_all()
        self.mfile.clear_pictures()
        self.mfile.save()
        return True

    def is_clean(self):
        """ Check if the "metadata" block is present in the file
        """
        return super(FlacStripper, self).is_clean() and not self.mfile.pictures

    def get_meta(self):
        """ Return the content of the metadata block if present
        """
        metadata = super(FlacStripper, self).get_meta()
        if self.mfile.pictures:
            metadata['picture:'] = 'yes'
        return metadata