~ubuntu-branches/ubuntu/lucid/exaile/lucid

« back to all changes in this revision

Viewing changes to plugins/daapserver/spydaap/parser/mp3.py

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Starr-Bochicchio
  • Date: 2010-02-12 19:51:01 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20100212195101-8jt3tculxcl92e6v
Tags: 0.3.1~b1-0ubuntu1
* New upstream release.
* Adjust exaile.install for new plugins.
* debian/control:
 - Drop unneeded python-dev Build-Dep.
 - Bump Standards-Version to 3.8.4 
* debian/rules: No empty po files to delete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#Copyright (C) 2008 Erik Hetzner
 
2
 
 
3
#This file is part of Spydaap. Spydaap is free software: you can
 
4
#redistribute it and/or modify it under the terms of the GNU General
 
5
#Public License as published by the Free Software Foundation, either
 
6
#version 3 of the License, or (at your option) any later version.
 
7
 
 
8
#Spydaap is distributed in the hope that it will be useful, but
 
9
#WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
11
#General Public License for more details.
 
12
 
 
13
#You should have received a copy of the GNU General Public License
 
14
#along with Spydaap. If not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
import mutagen.id3, mutagen.mp3, re, spydaap, re, os, sys
 
17
from spydaap.daap import do
 
18
mutagen.id3.ID3.PEDANTIC = False
 
19
 
 
20
class Mp3Parser(spydaap.parser.Parser):
 
21
    mp3_string_map = {
 
22
        'TIT1': 'daap.songgrouping',
 
23
        'TIT2': 'dmap.itemname',
 
24
        'TPE1': 'daap.songartist',
 
25
        'TCOM': 'daap.songcomposer',
 
26
        'TCON': 'daap.songgenre',
 
27
        'TPE1': 'daap.songartist',
 
28
        'TALB': 'daap.songalbum',
 
29
        }
 
30
 
 
31
    mp3_int_map = {
 
32
        'TBPM': 'daap.songbeatsperminute',
 
33
        'TDRC': 'daap.songyear',
 
34
        #'TLEN': 'daap.songtime',
 
35
        }
 
36
#do('daap.songdiscnumber', 1),
 
37
#        do('daap.songgenre', 'Test'),
 
38
#        do('daap.songdisccount', 1),
 
39
#        do('daap.songcompilation', False),
 
40
#        do('daap.songuserrating', 1),
 
41
                
 
42
    def handle_rating(self, mp3, d):
 
43
        try:
 
44
            popm = mp3.tags.getall('POPM')
 
45
            if popm != None:
 
46
                rating = int(popm[0].rating * (0.39215686274509803))
 
47
                d.append(do('daap.songuserrating', rating))
 
48
        except: pass
 
49
 
 
50
    def handle_track(self, mp3, d):
 
51
        try:
 
52
            if mp3.tags.has_key('TRCK'):
 
53
                t = str(mp3.tags['TRCK']).split('/')
 
54
                d.append(do('daap.songtracknumber', int(t[0])))
 
55
                if (len(t) == 2):
 
56
                    d.append(do('daap.songtrackcount', int(t[1])))
 
57
        except: pass
 
58
 
 
59
    def handle_disc(self, mp3, d):
 
60
        try:
 
61
            if mp3.tags.has_key('TPOS'):
 
62
                t = str(mp3.tags['TPOS']).split('/')
 
63
                d.append(do('daap.songdiscnumber', int(t[0])))
 
64
                if (len(t) == 2):
 
65
                    d.append(do('daap.songdisccount', int(t[1])))
 
66
        except: pass
 
67
 
 
68
    file_re = re.compile(".*\\.[mM][pP]3$")
 
69
    def understands(self, filename):
 
70
        return self.file_re.match(filename)
 
71
 
 
72
    def parse(self, filename):
 
73
        try:
 
74
            mp3 = mutagen.mp3.MP3(filename)
 
75
            d = []
 
76
            if mp3.tags != None:
 
77
                self.handle_string_tags(self.mp3_string_map, mp3, d)
 
78
                self.handle_int_tags(self.mp3_int_map, mp3, d)
 
79
                self.handle_rating(mp3, d)
 
80
                self.handle_track(mp3, d)
 
81
                self.handle_disc(mp3, d)
 
82
            self.add_file_info(filename, d)
 
83
            d.extend([do('daap.songtime', mp3.info.length * 1000),
 
84
                      do('daap.songbitrate', mp3.info.bitrate / 1000),
 
85
                      do('daap.songsamplerate', mp3.info.sample_rate),
 
86
                      do('daap.songformat', 'mp3'),
 
87
                      do('daap.songdescription', 'MPEG Audio File'),
 
88
                      ])
 
89
            name = self.set_itemname_if_unset(os.path.basename(filename), d)
 
90
            return (d, name)
 
91
        except Exception, e:
 
92
            sys.stderr.write("Caught exception: while processing %s: %s " % (filename, str(e)) )
 
93
            return (None, None)