~ubuntu-branches/ubuntu/oneiric/rhythmbox/oneiric

« back to all changes in this revision

Viewing changes to plugins/magnatune/TrackListHandler.py

  • Committer: Bazaar Package Importer
  • Author(s): Rico Tzschichholz
  • Date: 2011-07-29 16:41:38 UTC
  • mto: This revision was merged to the branch mainline in revision 191.
  • Revision ID: james.westby@ubuntu.com-20110729164138-wwicy8nqalm18ck7
Tags: upstream-2.90.1~20110802
ImportĀ upstreamĀ versionĀ 2.90.1~20110802

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
 
2
#
 
3
# Copyright (C) 2006 Adam Zimmerman  <adam_zimmerman@sfu.ca>
 
4
# Copyright (C) 2006 James Livingston  <doclivingston@gmail.com>
 
5
#
 
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)
 
9
# any later version.
 
10
#
 
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.
 
18
#
 
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.
 
23
#
 
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.
 
27
 
 
28
import xml.sax, xml.sax.handler
 
29
import datetime, re, urllib
 
30
 
 
31
import rb
 
32
from gi.repository import RB
 
33
 
 
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)
 
37
                self.__db = db
 
38
                self.__entry_type = entry_type
 
39
                self.__sku_dict = sku_dict
 
40
                self.__home_dict = home_dict
 
41
                self.__art_dict = art_dict
 
42
                self.__track = {}
 
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)$')
 
48
 
 
49
        def startElement(self, name, attrs):
 
50
                self.__text = ""
 
51
 
 
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)
 
55
                return trackurl
 
56
 
 
57
        def endElement(self, name):
 
58
                if name == "Track":
 
59
                        try:
 
60
                                # prefer ogg streams to mp3
 
61
                                if 'oggurl' in self.__track:
 
62
                                        trackurl = self.__track['oggurl']
 
63
                                else:
 
64
                                        trackurl = self.__track['url']
 
65
                                # use ad-free tracks if available
 
66
                                if self.__account_type != 'none':
 
67
                                        trackurl = self.fix_trackurl(trackurl)
 
68
 
 
69
                                trackurl = str(trackurl)
 
70
 
 
71
                                # add the track to the source
 
72
                                entry = self.__db.entry_lookup_by_location (trackurl)
 
73
                                if entry == None:
 
74
                                        entry = RB.RhythmDBEntry.new(self.__db, self.__entry_type, trackurl)
 
75
 
 
76
                                # if year is not set, use launch date instead
 
77
                                try:
 
78
                                        year = parse_int(self.__track['year'])
 
79
                                        if year <= 0:
 
80
                                                raise ValueError
 
81
                                except ValueError:
 
82
                                        year = parse_int(self.__track['launchdate'][0:4])
 
83
 
 
84
                                date = datetime.date(year, 1, 1).toordinal()
 
85
                                try:
 
86
                                        tracknum = parse_int(self.__track['tracknum'])
 
87
                                except ValueError:
 
88
                                        tracknum = 0
 
89
                                try:
 
90
                                        duration = parse_int(self.__track['seconds'])
 
91
                                except ValueError:
 
92
                                        duration = 0
 
93
 
 
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))
 
101
 
 
102
                                key = str(trackurl)
 
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'])
 
107
 
 
108
                                self.__db.commit()
 
109
                        except Exception,e: # This happens on duplicate uris being added
 
110
                                import sys
 
111
                                sys.excepthook(*sys.exc_info())
 
112
                                print "Couldn't add %s - %s" % (self.__track['artist'], self.__track['trackname']), e
 
113
 
 
114
                        self.__track = {}
 
115
                elif name == "AllSongs":
 
116
                        pass # end of the file
 
117
                else:
 
118
                        self.__track[name] = self.__text
 
119
 
 
120
        def characters(self, content):
 
121
                self.__text = self.__text + content
 
122
 
 
123
# parses partial integers
 
124
def parse_int(s):
 
125
        news = ""
 
126
        for c in s:
 
127
                if c in '0123456789': # only positive integers allowed
 
128
                        news += c
 
129
                else:
 
130
                        break
 
131
        return int(news)