def find_coverart_releasegroup(releaseGroup): from lxml import etree root = etree.XML(u'<coverart></coverart>') try: data = musicbrainzngs.get_release_group_image_list(releaseGroup) except musicbrainzngs.ResponseError as err: if err.cause.code == 404: etree.SubElement(root, "error").text = "Release ID not found" log(True, convert_etree(etree.tostring(root, encoding='UTF-8', pretty_print=True, xml_declaration=True))) sys.exit(1) else: etree.SubElement(root, "error").text = "Received bad response from the MB server" log(True, convert_etree(etree.tostring(root, encoding='UTF-8', pretty_print=True, xml_declaration=True))) sys.exit(1) if debug: pprint.pprint(data) for image in data["images"]: imageNode = etree.SubElement(root, "image") etree.SubElement(imageNode, "image").text = image["image"] etree.SubElement(imageNode, "approved").text = str(image["approved"]) etree.SubElement(imageNode, "front").text = str(image["front"]) etree.SubElement(imageNode, "back").text = str(image["back"]) #etree.SubElement(imageNode, "types").text = image["types"] etree.SubElement(imageNode, "thumb-small").text = image["thumbnails"]["small"] etree.SubElement(imageNode, "thumb-large").text = image["thumbnails"]["large"] log(True, convert_etree(etree.tostring(root, encoding='UTF-8', pretty_print=True, xml_declaration=True))) sys.exit(0)
def get_data(self, data): self.result = mbngs.search_release_groups(('%s' % data['Title']), artist=('%s'%data['Artist']), primarytype='album') x = 0 while True: topResult = self.result['release-group-list'][x] topID = topResult['id'] try: pics = mbngs.get_release_group_image_list(topID) self.releaseGroupID = topID break except: x += 1 self.url = pics['images'][0]['image'] self.filename = self.url.split('/')[-1] # Used for testing / devlopment. Save the cover art to a local directory. # urllib.urlretrieve(self.url, self.filename) self.returnData['Art'] = self.url self.releaseResult = mbngs.get_release_group_by_id(self.releaseGroupID) self.returnData['Date'] = self.releaseResult['release-group']['first-release-date'] print self.returnData return self.returnData
def getCoverArt(self, known_data): ''' Retrieve Cover Art URL Retruns a list of URLs (limited to 1 at the mo) ''' self.Logger.msg('Trying Cover Art for Album - {}'.format( known_data['album_id']) ) # Blocking Call to MBrainz cover_result = musicbrainzngs.get_release_group_image_list( known_data['album_id'] ) covers = [] for image in cover_result["images"]: if "Front" in image["types"] and image["approved"]: self.Logger.msg('Approved front image found {} :)'.format( image["thumbnails"]["large"]) ) covers.append(image["thumbnails"]["large"]) # Returns a single cover as of now but this can be edited to # grab more covers # self.Logger.msg(covers[0]) return covers
def find_coverart_releasegroup(releaseGroup): from lxml import etree root = etree.XML(u'<coverart></coverart>') try: data = musicbrainzngs.get_release_group_image_list(releaseGroup) except musicbrainzngs.ResponseError as err: if err.cause.code == 404: etree.SubElement(root, "error").text = "Release ID not found" log(True, etree.tostring(root, encoding='UTF-8', pretty_print=True, xml_declaration=True)) sys.exit(1) else: etree.SubElement(root, "error").text = "Received bad response from the MB server" log(True, etree.tostring(root, encoding='UTF-8', pretty_print=True, xml_declaration=True)) sys.exit(1) if debug: pprint.pprint(data) for image in data["images"]: imageNode = etree.SubElement(root, "image") etree.SubElement(imageNode, "image").text = image["image"] etree.SubElement(imageNode, "approved").text = str(image["approved"]) etree.SubElement(imageNode, "front").text = str(image["front"]) etree.SubElement(imageNode, "back").text = str(image["back"]) #etree.SubElement(imageNode, "types").text = image["types"] etree.SubElement(imageNode, "thumb-small").text = image["thumbnails"]["small"] etree.SubElement(imageNode, "thumb-large").text = image["thumbnails"]["large"] log(True, etree.tostring(root, encoding='UTF-8', pretty_print=True, xml_declaration=True)) sys.exit(0)
def get_release_group_cover(release_group_id): try: data = musicbrainzngs.get_release_group_image_list(release_group_id) except musicbrainzngs.musicbrainz.ResponseError: return None for image in data["images"]: if "Front" in image["types"] and image["approved"]: return image["thumbnails"]["large"]
def _get_mb_album(query: str) -> dict: musicbrainzngs.set_useragent("Kinobot Search", "0.0.1") results = musicbrainzngs.search_release_groups(query, limit=1, strict=True) try: album = results["release-group-list"][0] except (KeyError, IndexError): raise exceptions.NothingFound from None images = musicbrainzngs.get_release_group_image_list(album["id"]) album.update(images) return album
def api_get_cover_image(id): try: mbz.set_useragent("Artistics", "v1.0", "*****@*****.**") data = mbz.get_release_group_image_list(id) # There can be multiple, but the one we're looking for is the front cover. for image in data["images"]: if "Front" in image["types"] and image["approved"]: # Redirect the browser's request to the image URL the API provided. return redirect(image["thumbnails"]["large"], code=302) except mbz.WebServiceError as exc: pass # If we encounter an error with the API (eg 404, or only back image etc), return a placeholder image. return redirect("/images/question.png", code=302)
def fetchAlbumArt(name, artist): debug("GET " + name + " " + artist) try: results = musicbrainzngs.search_release_groups(name, limit=3, artistname=artist, primarytype="album") #pp.pprint(results['release-group-list']) results = results['release-group-list'] resultid = results[0]['id'] debug("ID = " + resultid) imagelist = musicbrainzngs.get_release_group_image_list(resultid) except ResponseError: debug("No image found.") return None except NetworkError: debug("Cannot connect to MB server") return None return imagelist['images'][0]['thumbnails']['large']
def album_art(self, search_term: str, cache_key: str): """ Change the consume playback state Parameters ---------- search_term : string Search term when looking for album art cache_name : string Name to store any cache item under """ if os.path.isfile(self.art_cache + str(cache_key)): LOGGER.debug(f"Found album art with cache key: {cache_key}.") return self.art_cache + str(cache_key) else: try: mb_search = musicbrainzngs.search_releases(search_term) LOGGER.info( f"Trying to query music branz album art with search term: {search_term}." ) if isinstance( mb_search['release-list'][0]['release-group']['id'], str): image_list = musicbrainzngs.get_release_group_image_list( mb_search['release-list'][0]['release-group']['id']) if isinstance( image_list['images'][0]['thumbnails']['small'], str): thumb_file = self.art_cache + str(cache_key) urllib.request.urlretrieve( image_list['images'][0]['thumbnails']['small'], thumb_file) return thumb_file except Exception as e: LOGGER.error("Unable to get album art: " + str(e)) return self.default_art()
def musicBrainz(artistName, aDict): musicbrainzngs.set_useragent(123, 123, contact=None) someID = musicbrainzngs.search_artists(artistName).get( 'artist-list')[0].get('id') allTapes = musicbrainzngs.get_artist_by_id( someID, 'release-groups').get('artist').get('release-group-list') # print(allTapes) for key in allTapes: if key.get('secondary-type-list') is None or key.get( 'secondary-type-list')[0] == 'Compilation': try: image = Image.open( urllib.request.urlopen( musicbrainzngs.get_release_group_image_list( key.get('id')).get('images')[0].get( 'thumbnails').get('small'))) albumName = key.get('title') aDict[albumName] = [pixelAverage(image), image] except: pass
def album_page(request, MBID): album_info = {} data = musicbrainzngs.get_release_group_by_id( id=MBID, includes=['releases', 'media', 'artists', 'tags'])['release-group'] album_info['date'] = data['first-release-date'] artist = (data['artist-credit'][0]['artist']['id'], data['artist-credit'][0]['artist']['name']) data = data['release-list'] album_info['dbpedia_ID'] = None album_info['artist_name'] = artist[1] def get_tracklist(data): tracklist = [] for release in data: if release.get('date') == album_info['date']: initial_release = musicbrainzngs.get_release_by_id( id=release['id'], includes=['recordings'])['release']['medium-list'] break #elif release['medium-list'][0]['format'] == 'Digital Media': #initial_release = musicbrainzngs.get_release_by_id(id=release['id'], includes=['recordings'])['release']['medium-list'] for track in initial_release[0]['track-list']: tracklist.append(track['recording']['title']) return tracklist def get_producers(album): global dbpedia global prefixes labels = [] relationships = ['dbo:producer', 'dbp:producer'] for item in relationships: dbpedia.setQuery(""" %s SELECT ?label WHERE { <%s> %s ?producer. ?producer rdfs:label ?label. filter langMatches(lang(?label),"en") } """ % (prefixes, album, item)) dbpedia.setReturnFormat(JSON) results = dbpedia.query().convert() for result in results["results"]["bindings"]: labels.append(result["label"]["value"]) return labels def get_award(album): global dbpedia labels = [] dbpedia.setQuery(""" PREFIX dbp: <http://dbpedia.org/property/> SELECT ?award WHERE { <%s> dbp:award ?award.} """ % (album)) dbpedia.setReturnFormat(JSON) results = dbpedia.query().convert() for result in results["results"]["bindings"]: labels.append(result["award"]["value"]) return labels def get_bbc_review(MBID): global bbcmusic global prefixes reviews = [] try: bbcmusic.setQuery(""" %s SELECT DISTINCT ?r_name, ?rev WHERE { <http://www.bbc.co.uk/music/artists/%s#artist> foaf:made ?r1 . ?r1 a mo:Record . ?r1 dc:title ?r_name . ?r1 rev:hasReview ?rev } """ % (prefixes, MBID)) bbcmusic.setReturnFormat(JSON) results = bbcmusic.query().convert() for result in results["results"]["bindings"]: reviews.append( (result["r_name"]["value"], result["rev"]["value"])) except EndPointInternalError: pass return reviews album_info['name'] = data[0]['title'] album_info['MBID'] = MBID IDs = [ "http://dbpedia.org/resource/" + album_info['name'].replace(" ", "_"), "http://dbpedia.org/resource/" + album_info['name'].replace(" ", "_") + "_(album)", "http://dbpedia.org/resource/" + album_info['name'].replace(" ", "_") + "_(" + artist[1].replace(" ", "_") + "_album)", "http://dbpedia.org/resource/" + album_info['name'].replace(" ", "_") + "_(" + artist[1].replace("The ", "").replace(" ", "_") + "_album)" ] for url in IDs: if get_description(url[28:]) is not "" and "album" in get_description( url[28:]): album_info['dbpedia_ID'] = url album_info['description'] = get_description(url[28:]) try: album_info['image'] = musicbrainzngs.get_release_group_image_list( MBID)['images'][0]['thumbnails']['small'] except (HTTPError, musicbrainzngs.musicbrainz.ResponseError): pass album_info['tracklist'] = get_tracklist(data) if album_info['dbpedia_ID'] is not None: album_info['producers'] = get_producers(album_info['dbpedia_ID']) album_info['genres'] = get_dbpedia_genres(album_info['dbpedia_ID']) album_info['award'] = get_award(album_info['dbpedia_ID']) for item in get_bbc_review(artist[0]): if item[0] == data[0]['title']: album_info['review'] = item[1] if 'award' in album_info: for item in album_info['award']: if item not in BPI_certifications: album_info['award'].remove(item) create_RDF(album_info, False) return render(request, 'home/album.html', {"info": album_info})