Ejemplo n.º 1
0
 def getMp3FontCover(self):
     from common import EasyMP3
     audio = common.MutagenFile(self.url, common.FORMATS)
     ext = None
     img_data = None
     if isinstance(audio, EasyMP3):
         apics = audio.tags.getID3().getall('APIC')
         if len(apics) > 0:
             apic = apics[0]
             if apic.type == 3:
                 mine = apic.mime
                 ext = mine.split('/')[-1]
                 img_data = apic.data
     return ext, img_data
Ejemplo n.º 2
0
    def saveTags(self):
        ''' Save tag information to file. '''
        if not self.isLocalFile():
            self.last_error = self.url + " " + "is not a local file"
            return False
        if not self.isExisted(self.url):
            self.last_error = self.url + " doesn't exist"
            return False
        if not os.access(self.url, os.W_OK):
            self.last_error = self.url + " doesn't have enough permission"
            return False

        TAG_KEYS = self.TAG_KEYS
        TAGS_KEYS_OVERRIDE = self.TAGS_KEYS_OVERRIDE

        try:
            audio = common.MutagenFile(self.url, common.FORMATS)
            tag_keys_override = None

            if audio is not None:
                if audio.tags is None:
                    audio.add_tags()
                tag_keys_override = TAGS_KEYS_OVERRIDE.get(
                    audio.__class__.__name__, None)

                for file_tag in TAG_KEYS:
                    if tag_keys_override and tag_keys_override.has_key(
                            file_tag):
                        file_tag = tag_keys_override[file_tag]

                    value = getattr(self, file_tag)
                    if value:
                        try:
                            value = unicode(value)
                        except Exception, e:
                            value = value.decode('utf-8')
                        # print file_tag, value, type(value)
                        audio[file_tag] = value
                    else:
                        try:
                            del (audio[file_tag])  # TEST
                        except KeyError:
                            pass
                audio.save()
            else:
Ejemplo n.º 3
0
    def getTags(self):
        path = self.url

        TAG_KEYS = self.TAG_KEYS
        TAGS_KEYS_OVERRIDE = self.TAGS_KEYS_OVERRIDE

        setattr(self, 'size', os.path.getsize(path))

        audio = common.MutagenFile(path, common.FORMATS)

        if audio is not None:
            tag_keys_override = TAGS_KEYS_OVERRIDE.get(
                audio.__class__.__name__, None)
            for file_tag in TAG_KEYS:
                if tag_keys_override and tag_keys_override.has_key(file_tag):
                    file_tag = tag_keys_override[file_tag]
                if audio.has_key(file_tag) and audio[file_tag]:
                    value = audio[file_tag]
                    if isinstance(value, list) or isinstance(value, tuple):
                        value = value[0]
                    fix_value = common.fix_charset(value)
                    if fix_value == "[Invalid Encoding]":
                        if tag == "title":
                            fix_value = self.fileName
                        else:
                            fix_value = ""

                    setattr(self, file_tag, fix_value)

            for key in ['sample_rate', 'bitrate', 'length']:
                try:
                    if hasattr(audio.info, key):
                        if key == 'length':
                            setattr(self, 'duration', getattr(audio.info, key))
                        else:
                            setattr(self, key, getattr(audio.info, key))
                except Exception, e:
                    print e