From 6159ca61edf2f881e6cc4db64907c0d62e39a3e7 Mon Sep 17 00:00:00 2001 From: Remy Date: Mon, 8 Aug 2011 13:18:10 -0700 Subject: [PATCH] Changed the way album descriptions are retrieved from lastfm --- headphones/importer.py | 2 +- headphones/lastfm.py | 47 +++++++++++++++++++++++++++++++++++++++++- headphones/mb.py | 2 +- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/headphones/importer.py b/headphones/importer.py index 27e3bfbe..4f8dae3b 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -215,7 +215,7 @@ def addArtisttoDB(artistid, extrasonly=False): myDB.upsert("albums", newValueDict, controlValueDict) - lastfm.getAlbumDescription(rg['id'], release_dict['releaselist']) + lastfm.getAlbumDescription(rg['id'], artist['artist_name'], rg['title']) # I changed the albumid from releaseid -> rgid, so might need to delete albums that have a releaseid for release in release_dict['releaselist']: diff --git a/headphones/lastfm.py b/headphones/lastfm.py index 0e39a110..814a35e8 100644 --- a/headphones/lastfm.py +++ b/headphones/lastfm.py @@ -91,7 +91,52 @@ def getArtists(): for artistid in artistlist: importer.addArtisttoDB(artistid) -def getAlbumDescription(rgid, releaselist): +def getAlbumDescription(rgid, artist, album): + + myDB = db.DBConnection() + result = myDB.select('SELECT Summary from descriptions WHERE ReleaseGroupID=?', [rgid]) + + if result: + return + + params = { "method": 'album.getInfo', + "api_key": api_key, + "artist": artist.encode('utf-8'), + "album": album.encode('utf-8') + } + + searchURL = 'http://ws.audioscrobbler.com/2.0/?' + urllib.urlencode(params) + data = urllib.urlopen(searchURL).read() + + if data == 'Album not found': + return + + try: + d = minidom.parseString(data) + + albuminfo = d.getElementsByTagName("album") + + for item in albuminfo: + summarynode = item.getElementsByTagName("summary")[0].childNodes + contentnode = item.getElementsByTagName("content")[0].childNodes + for node in summarynode: + summary = node.data + for node in contentnode: + content = node.data + + controlValueDict = {'ReleaseGroupID': rgid} + newValueDict = {'Summary': summary, + 'Content': content} + myDB.upsert("descriptions", newValueDict, controlValueDict) + + except: + return + +def getAlbumDescriptionOld(rgid, releaselist): + """ + This was a dumb way to do it - going to just use artist & album name but keeping this here + because I may use it to fetch and cache album art + """ myDB = db.DBConnection() result = myDB.select('SELECT Summary from descriptions WHERE ReleaseGroupID=?', [rgid]) diff --git a/headphones/mb.py b/headphones/mb.py index 92723fda..1d6b20cd 100644 --- a/headphones/mb.py +++ b/headphones/mb.py @@ -306,7 +306,7 @@ def getReleaseGroup(rgid): releaselist.append(release_dict) - average_tracks = sum(x['trackscount'] for x in releaselist) / len(releaselist) + average_tracks = sum(x['trackscount'] for x in releaselist) / float(len(releaselist)) for item in releaselist: item['trackscount_delta'] = abs(average_tracks - item['trackscount'])