1
#Copyright (C) 2008 Erik Hetzner
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.
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.
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/>.
16
import mutagen.id3, mutagen.mp3, re, spydaap, re, os, sys
17
from spydaap.daap import do
18
mutagen.id3.ID3.PEDANTIC = False
20
class Mp3Parser(spydaap.parser.Parser):
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',
32
'TBPM': 'daap.songbeatsperminute',
33
'TDRC': 'daap.songyear',
34
#'TLEN': 'daap.songtime',
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),
42
def handle_rating(self, mp3, d):
44
popm = mp3.tags.getall('POPM')
46
rating = int(popm[0].rating * (0.39215686274509803))
47
d.append(do('daap.songuserrating', rating))
50
def handle_track(self, mp3, d):
52
if mp3.tags.has_key('TRCK'):
53
t = str(mp3.tags['TRCK']).split('/')
54
d.append(do('daap.songtracknumber', int(t[0])))
56
d.append(do('daap.songtrackcount', int(t[1])))
59
def handle_disc(self, mp3, d):
61
if mp3.tags.has_key('TPOS'):
62
t = str(mp3.tags['TPOS']).split('/')
63
d.append(do('daap.songdiscnumber', int(t[0])))
65
d.append(do('daap.songdisccount', int(t[1])))
68
file_re = re.compile(".*\\.[mM][pP]3$")
69
def understands(self, filename):
70
return self.file_re.match(filename)
72
def parse(self, filename):
74
mp3 = mutagen.mp3.MP3(filename)
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'),
89
name = self.set_itemname_if_unset(os.path.basename(filename), d)
92
sys.stderr.write("Caught exception: while processing %s: %s " % (filename, str(e)) )