Source

VPlayer / tagreader.py

import os
import sys

import chardet
import mutagen

from vplayer import common
from logger import log

from PyQt4 import QtCore

class BadFile(Exception):
    pass

def decoder(tgs):
    names = ['artist', 'title', 'album']
    res = []
    for name in names:
        try:
            if not tgs.has_key(name): continue
            data = tgs[name]
            data = ''.join(chr(ord(i)) for i in data)
            det = chardet.detect(data)
            res.append(det['encoding'])
        except:
            continue

    resenc = 'ascii'
    for enc in res:
        if res.count(enc):
            resenc = enc
            break
    if resenc != 'ascii':
        sets = QtCore.QSettings()
        only_1251 = sets.value('detect_only_cp1251', QtCore.QVariant(True)).toBool()
        if only_1251:
            resenc = 'windows-1251'
        log.info('Detected %s encoding' % resenc)
    else:
        return tgs
    for name in names:
        if tgs.has_key(name):
            try:
                data = tgs[name]
                data = ''.join(chr(ord(i)) for i in data)
                tgs[name] = unicode(data.decode(resenc))
            except:
                continue
    return tgs

class TagReader:
    def read(self, filename):
        tags = { 'artist': 'Unknown', 'album': 'Unknown', 'title': 'Unknown',
                 'year': 0, 'trackno': 0, 'length': 0,
                 'url': filename}
        try:
            metadata = mutagen.File(filename)
        except:
            log.error('mutagen could not open file')
            return tags
        if not metadata:
            log.error('mutagen could not find any metadata')
            return tags
        artist = title = album = year = trackno = length = None

        vtags = {'artist': artist, 'album': album, 'title': title,
                      'date': year, 'tracknumber': trackno}

        if metadata.has_key('artist'):
            tags['artist'] = metadata['artist'][0]
        elif metadata.has_key('TPE1'):
            tags['artist'] = metadata['TPE1'][0]

        if metadata.has_key('album'):
            tags['album'] = metadata['album'][0]
        elif metadata.has_key('TALB'):
            tags['album'] = metadata['TALB'][0]

        if metadata.has_key('title'):
            tags['title'] = metadata['title'][0]
        elif metadata.has_key('TIT2'):
            tags['title'] = metadata['TIT2'][0]


        log.debug('Reading tags for %s ' % filename)
        if metadata.has_key('date') and metadata['date'][0][:4]:
            tags['year'] = int(metadata['date'][0][:4])
        elif metadata.has_key('TDRC') and metadata['TDRC'][0].year:
            tags['year'] = int(metadata['TDRC'][0].year)
        elif metadata.has_key('TDRL') and metadata['TDRL'][0].year:
            tags['year'] = int(metadata['TDRL'][0].year)
        elif metadata.has_key('TDTG') and metadata['TDTG'][0].year:
            tags['year'] = int(metadata['TDTG'][0].year)
        elif metadata.has_key('TYER'):
            tags['year'] = int(metadata['TYER'][0].year)
        else:
            log.error('metadata does not contain year tag')
            tags['year'] = 0

        if metadata.has_key('tracknumber'):
            tags['trackno'] = metadata['tracknumber'][0]
        elif metadata.has_key('TRCK'):
            tags['trackno'] = metadata['TRCK'][0]

        tags = decoder(tags)

        tags['length'] = int(metadata.info.length)
        tags['url'] = filename
        tags['local'] = True

        for tag in ['artist', 'album', 'title']:
            if tags.has_key(tag):
                tags[tag] = common.clean_spaces(tags[tag])

        return tags
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.