1
# -*- Mode: python; coding: utf-8; tab-width: 8; indent-tabs-mode: t; -*-
3
# Copyright (C) 2009 Jonathan Matthew <jonathan@d14n.org>
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)
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.
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.
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.
27
import xml.dom.minidom as dom
30
from gi.repository import RB
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"
38
MUSICBRAINZ_VARIOUS_ARTISTS = "89ad4ac3-39f7-470e-963a-56509c546377"
41
AMAZON_IMAGE_URL = "http://images.amazon.com/images/P/%s.01.LZZZZZZZ.jpg"
43
class MusicBrainzSearch(object):
45
def get_release_cb (self, data, args):
46
(key, store, callback, cbargs) = args
48
print "musicbrainz release request returned nothing"
53
parsed = dom.parseString(data)
55
storekey = RB.ExtDBKey.create_storage('album', key.get_field('album'))
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')
65
artistname = nametags[0].firstChild.data
66
print "got musicbrainz artist name %s" % artistname
67
storekey.add_field('artist', artistname)
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
75
print "got ASIN %s" % asin
76
image_url = AMAZON_IMAGE_URL % asin
78
store.store_uri(storekey, RB.ExtDBSourceType.SEARCH, image_url)
80
print "no ASIN for this release"
84
print "exception parsing musicbrainz response: %s" % e
87
def search(self, key, last_time, store, callback, *args):
88
key = key.copy() # ugh
89
album_id = key.get_info("musicbrainz-albumid")
91
print "no musicbrainz release ID for this track"
95
if album_id.startswith(MUSICBRAINZ_RELEASE_PREFIX):
96
album_id = album_id[len(MUSICBRAINZ_RELEASE_PREFIX):]
98
if album_id.endswith(MUSICBRAINZ_RELEASE_SUFFIX):
99
album_id = album_id[:-len(MUSICBRAINZ_RELEASE_SUFFIX)]
101
print "stripped release ID: %s" % album_id
103
url = MUSICBRAINZ_RELEASE_URL % (album_id)
105
loader.get_url(url, self.get_release_cb, (key, store, callback, args))