7 'set_caa_hostname',
'get_image_list',
'get_release_group_image_list',
8 'get_release_group_image_front',
'get_image_front',
'get_image_back',
14 from musicbrainzngs
import compat
15 from musicbrainzngs
import musicbrainz
17 hostname =
"coverartarchive.org"
21 """Set the base hostname for Cover Art Archive requests.
22 Defaults to 'coverartarchive.org'."""
24 hostname = new_hostname
27 def _caa_request(mbid, imageid=None, size=None, entitytype="release"):
28 """ Make a CAA request.
30 :param imageid: ``front``, ``back`` or a number from the listing obtained
31 with :meth:`get_image_list`.
35 :type size: str or None
37 :param entitytype: ``release`` or ``release-group``
42 path = [entitytype, mbid]
44 path.append(
"%s-%s" % (imageid, size))
47 url = compat.urlunparse((
50 '/%s' %
'/'.join(path),
55 musicbrainz._log.debug(
"GET request for %s" % (url, ))
58 httpHandler = compat.HTTPHandler(debuglevel=0)
59 handlers = [httpHandler]
61 opener = compat.build_opener(*handlers)
66 if musicbrainz._useragent !=
"":
67 req.add_header(
'User-Agent', musicbrainz._useragent)
68 musicbrainz._log.debug(
"requesting with UA %s" % musicbrainz._useragent)
70 resp = musicbrainz._safe_read(opener, req,
None)
81 return json.loads(resp)
85 """Get the list of cover art associated with a release.
87 The return value is the deserialized response of the `JSON listing
88 <http://musicbrainz.org/doc/Cover_Art_Archive/API#.2Frelease.2F.7Bmbid.7D.2F>`_
89 returned by the Cover Art Archive API.
91 If an error occurs then a :class:`~musicbrainzngs.ResponseError` will
92 be raised with one of the following HTTP codes:
94 * 400: `Releaseid` is not a valid UUID
95 * 404: No release exists with an MBID of `releaseid`
96 * 503: Ratelimit exceeded
102 """Get the list of cover art associated with a release group.
104 The return value is the deserialized response of the `JSON listing
105 <http://musicbrainz.org/doc/Cover_Art_Archive/API#.2Frelease-group.2F.7Bmbid.7D.2F>`_
106 returned by the Cover Art Archive API.
108 If an error occurs then a :class:`~musicbrainzngs.ResponseError` will
109 be raised with one of the following HTTP codes:
111 * 400: `Releaseid` is not a valid UUID
112 * 404: No release exists with an MBID of `releaseid`
113 * 503: Ratelimit exceeded
115 return _caa_request(releasegroupid, entitytype=
"release-group")
119 """Download the front cover art for a release group.
120 The `size` argument and the possible error conditions are the same as for
123 return get_image(releasegroupid,
"front", size=size,
124 entitytype=
"release-group")
128 """Download the front cover art for a release.
129 The `size` argument and the possible error conditions are the same as for
132 return get_image(releaseid,
"front", size=size)
136 """Download the back cover art for a release.
137 The `size` argument and the possible error conditions are the same as for
140 return get_image(releaseid,
"back", size=size)
143 def get_image(mbid, coverid, size=None, entitytype="release"):
144 """Download cover art for a release. The coverart file to download
145 is specified by the `coverid` argument.
147 If `size` is not specified, download the largest copy present, which can be
150 If an error occurs then a :class:`~musicbrainzngs.ResponseError`
151 will be raised with one of the following HTTP codes:
153 * 400: `Releaseid` is not a valid UUID or `coverid` is invalid
154 * 404: No release exists with an MBID of `releaseid`
155 * 503: Ratelimit exceeded
157 :param coverid: ``front``, ``back`` or a number from the listing obtained with
158 :meth:`get_image_list`
159 :type coverid: int or str
161 :param size: 250, 500 or None. If it is None, the largest available picture
162 will be downloaded. If the image originally uploaded to the
163 Cover Art Archive was smaller than the requested size, only
164 the original image will be returned.
165 :type size: str or None
167 :param entitytype: The type of entity for which to download the cover art.
168 This is either ``release`` or ``release-group``.
169 :type entitytype: str
170 :return: The binary image data
173 if isinstance(coverid, int):
174 coverid =
"%d" % (coverid, )
175 if isinstance(size, int):
176 size =
"%d" % (size, )
177 return _caa_request(mbid, coverid, size=size, entitytype=entitytype)