1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
3
# Copyright (C) 2006 Adam Zimmerman <adam_zimmerman@sfu.ca>
4
# Copyright (C) 2006 James Livingston <doclivingston@gmail.com>
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2, or (at your option)
11
# The Rhythmbox authors hereby grant permission for non-GPL compatible
12
# GStreamer plugins to be used and distributed together with GStreamer
13
# and Rhythmbox. This permission is above and beyond the permissions granted
14
# by the GPL license by which Rhythmbox is covered. If you modify this code
15
# you may extend this exception to your version of the code, but you are not
16
# obligated to do so. If you do not wish to do so, delete this exception
17
# statement from your version.
19
# This program is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
# GNU General Public License for more details.
24
# You should have received a copy of the GNU General Public License
25
# along with this program; if not, write to the Free Software
26
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28
import xml.sax, xml.sax.handler
29
import datetime, re, urllib
32
from gi.repository import RB
34
class TrackListHandler(xml.sax.handler.ContentHandler):
35
def __init__(self, db, entry_type, sku_dict, home_dict, art_dict, account_type, username, password):
36
xml.sax.handler.ContentHandler.__init__(self)
38
self.__entry_type = entry_type
39
self.__sku_dict = sku_dict
40
self.__home_dict = home_dict
41
self.__art_dict = art_dict
43
self.__account_type = account_type
44
self.__user = urllib.quote(username)
45
self.__pw = urllib.quote(password)
46
self.__URIre = re.compile(r'^http://[^.]+\.magnatune\.com/')
47
self.__nsre = re.compile(r'\.(mp3|ogg)$')
49
def startElement(self, name, attrs):
52
def fix_trackurl(self, trackurl):
53
trackurl = self.__URIre.sub("http://%s:%s@%s.magnatune.com/" % (self.__user, self.__pw, self.__account_type), trackurl)
54
trackurl = self.__nsre.sub(r"_nospeech.\1", trackurl)
57
def endElement(self, name):
60
# prefer ogg streams to mp3
61
if 'oggurl' in self.__track:
62
trackurl = self.__track['oggurl']
64
trackurl = self.__track['url']
65
# use ad-free tracks if available
66
if self.__account_type != 'none':
67
trackurl = self.fix_trackurl(trackurl)
69
trackurl = str(trackurl)
71
# add the track to the source
72
entry = self.__db.entry_lookup_by_location (trackurl)
74
entry = RB.RhythmDBEntry.new(self.__db, self.__entry_type, trackurl)
76
# if year is not set, use launch date instead
78
year = parse_int(self.__track['year'])
82
year = parse_int(self.__track['launchdate'][0:4])
84
date = datetime.date(year, 1, 1).toordinal()
86
tracknum = parse_int(self.__track['tracknum'])
90
duration = parse_int(self.__track['seconds'])
94
self.__db.entry_set(entry, RB.RhythmDBPropType.ARTIST, str(self.__track['artist']))
95
self.__db.entry_set(entry, RB.RhythmDBPropType.ALBUM, str(self.__track['albumname']))
96
self.__db.entry_set(entry, RB.RhythmDBPropType.TITLE, str(self.__track['trackname']))
97
self.__db.entry_set(entry, RB.RhythmDBPropType.GENRE, str(self.__track['magnatunegenres']))
98
self.__db.entry_set(entry, RB.RhythmDBPropType.TRACK_NUMBER, long(tracknum))
99
self.__db.entry_set(entry, RB.RhythmDBPropType.DATE, long(date))
100
self.__db.entry_set(entry, RB.RhythmDBPropType.DURATION, long(duration))
103
sku = intern(str(self.__track['albumsku']))
104
self.__sku_dict[key] = sku
105
self.__home_dict[sku] = str(self.__track['home'])
106
self.__art_dict[sku] = str(self.__track['cover_small'])
109
except Exception,e: # This happens on duplicate uris being added
111
sys.excepthook(*sys.exc_info())
112
print "Couldn't add %s - %s" % (self.__track['artist'], self.__track['trackname']), e
115
elif name == "AllSongs":
116
pass # end of the file
118
self.__track[name] = self.__text
120
def characters(self, content):
121
self.__text = self.__text + content
123
# parses partial integers
127
if c in '0123456789': # only positive integers allowed