~matttbe/ubuntu/raring/rhythmbox/lp1010619

« back to all changes in this revision

Viewing changes to plugins/artsearch/musicbrainz.py

  • Committer: Package Import Robot
  • Author(s): Sebastien Bacher, Rico Tzschichholz
  • Date: 2012-01-10 17:05:11 UTC
  • mfrom: (1.1.61) (2.1.23 sid)
  • Revision ID: package-import@ubuntu.com-20120110170511-2i5lktgf2uulpnu7
Tags: 2.90.1~git20120108.9a9e21b9-0ubuntu1
* Sponsor ricotz's update and merge from Debian
[ Rico Tzschichholz ]
* New upstream Git snapshot

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) 2009 Jonathan Matthew  <jonathan@d14n.org>
 
4
#
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2, or (at your option)
 
8
# any later version.
 
9
#
 
10
# The Rhythmbox authors hereby grant permission for non-GPL compatible
 
11
# GStreamer plugins to be used and distributed together with GStreamer
 
12
# and Rhythmbox. This permission is above and beyond the permissions granted
 
13
# by the GPL license by which Rhythmbox is covered. If you modify this code
 
14
# you may extend this exception to your version of the code, but you are not
 
15
# obligated to do so. If you do not wish to do so, delete this exception
 
16
# statement from your version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
26
 
 
27
import xml.dom.minidom as dom
 
28
 
 
29
import rb
 
30
from gi.repository import RB
 
31
 
 
32
# musicbrainz URLs
 
33
MUSICBRAINZ_RELEASE_URL = "http://musicbrainz.org/ws/2/release/%s?inc=artists"
 
34
MUSICBRAINZ_RELEASE_PREFIX = "http://musicbrainz.org/release/"
 
35
MUSICBRAINZ_RELEASE_SUFFIX = ".html"
 
36
 
 
37
# musicbrainz IDs
 
38
MUSICBRAINZ_VARIOUS_ARTISTS = "89ad4ac3-39f7-470e-963a-56509c546377"
 
39
 
 
40
# Amazon URL bits
 
41
AMAZON_IMAGE_URL = "http://images.amazon.com/images/P/%s.01.LZZZZZZZ.jpg"
 
42
 
 
43
class MusicBrainzSearch(object):
 
44
 
 
45
        def get_release_cb (self, data, args):
 
46
                (key, store, callback, cbargs) = args
 
47
                if data is None:
 
48
                        print "musicbrainz release request returned nothing"
 
49
                        callback(*cbargs)
 
50
                        return
 
51
 
 
52
                try:
 
53
                        parsed = dom.parseString(data)
 
54
 
 
55
                        storekey = RB.ExtDBKey.create_storage('album', key.get_field('album'))
 
56
 
 
57
                        # check that there's an artist that isn't 'various artists'
 
58
                        artist_tags = parsed.getElementsByTagName('artist')
 
59
                        if len(artist_tags) > 0:
 
60
                                artist_id = artist_tags[0].attributes['id'].firstChild.data
 
61
                                if artist_id != MUSICBRAINZ_VARIOUS_ARTISTS:
 
62
                                        # add the artist name (as album-artist) to the storage key
 
63
                                        nametags = artist_tags[0].getElementsByTagName('name')
 
64
                                        if len(nametags) > 0:
 
65
                                                artistname = nametags[0].firstChild.data
 
66
                                                print "got musicbrainz artist name %s" % artistname
 
67
                                                storekey.add_field('artist', artistname)
 
68
 
 
69
 
 
70
                        # look for an ASIN tag
 
71
                        asin_tags = parsed.getElementsByTagName('asin')
 
72
                        if len(asin_tags) > 0:
 
73
                                asin = asin_tags[0].firstChild.data
 
74
 
 
75
                                print "got ASIN %s" % asin
 
76
                                image_url = AMAZON_IMAGE_URL % asin
 
77
 
 
78
                                store.store_uri(storekey, RB.ExtDBSourceType.SEARCH, image_url)
 
79
                        else:
 
80
                                print "no ASIN for this release"
 
81
 
 
82
                        callback(*cbargs)
 
83
                except Exception, e:
 
84
                        print "exception parsing musicbrainz response: %s" % e
 
85
                        callback(*cbargs)
 
86
 
 
87
        def search(self, key, last_time, store, callback, *args):
 
88
                key = key.copy()        # ugh
 
89
                album_id = key.get_info("musicbrainz-albumid")
 
90
                if album_id is None:
 
91
                        print "no musicbrainz release ID for this track"
 
92
                        callback(*args)
 
93
                        return
 
94
 
 
95
                if album_id.startswith(MUSICBRAINZ_RELEASE_PREFIX):
 
96
                        album_id = album_id[len(MUSICBRAINZ_RELEASE_PREFIX):]
 
97
 
 
98
                if album_id.endswith(MUSICBRAINZ_RELEASE_SUFFIX):
 
99
                        album_id = album_id[:-len(MUSICBRAINZ_RELEASE_SUFFIX)]
 
100
 
 
101
                print "stripped release ID: %s" % album_id
 
102
 
 
103
                url = MUSICBRAINZ_RELEASE_URL % (album_id)
 
104
                loader = rb.Loader()
 
105
                loader.get_url(url, self.get_release_cb, (key, store, callback, args))