Improved addArtisttoDB, it now uses get_all_releases(releasegroupid) to get the data.

This has reduced the number of calls to the musicbrainz database by 90% in a test where i added U2, i expect similar effects for all artists.
This commit is contained in:
Patrick Speiser
2012-09-08 18:54:11 +02:00
parent b71bb6938d
commit fe8220a000
2 changed files with 31 additions and 46 deletions

View File

@@ -167,69 +167,51 @@ def addArtisttoDB(artistid, extrasonly=False):
# check if the album already exists
rg_exists = myDB.action("SELECT * from albums WHERE AlbumID=?", [rg['id']]).fetchone()
try:
releaselist = mb.getReleaseGroup(rgid)
except Exception, e:
logger.info('Unable to get release information for %s - there may not be any official releases in this release group' % rg['title'])
continue
if not releaselist:
releases = mb.get_all_releases(rgid)
if not releases:
errors = True
logger.info('Unable to get release information for %s - there may not be any official releases in this release group' % rg['title'])
continue
# This will be used later to build a hybrid release
fullreleaselist = []
for release in releaselist:
for release in releases:
# What we're doing here now is first updating the allalbums & alltracks table to the most
# current info, then moving the appropriate release into the album table and its associated
# tracks into the tracks table
releaseid = release['id']
try:
releasedict = mb.getRelease(releaseid, include_artist_info=False)
except Exception, e:
errors = True
logger.info('Unable to get release information for %s: %s' % (release['id'], e))
continue
if not releasedict:
errors = True
continue
controlValueDict = {"ReleaseID" : release['ReleaseID']}
controlValueDict = {"ReleaseID": release['id']}
newValueDict = {"ArtistID": artistid,
"ArtistName": artist['artist_name'],
"AlbumTitle": rg['title'],
"AlbumID": rg['id'],
"AlbumASIN": releasedict['asin'],
"ReleaseDate": releasedict['date'],
"Type": rg['type'],
"ReleaseCountry": releasedict['country'],
"ReleaseFormat": releasedict['format']
newValueDict = {"ArtistID": release['ArtistID'],
"ArtistName": release['ArtistName'],
"AlbumTitle": release['AlbumTitle'],
"AlbumID": release['AlbumID'],
"AlbumASIN": release['AlbumASIN'],
"ReleaseDate": release['ReleaseDate'],
"Type": release['Type'],
"ReleaseCountry": release['ReleaseCountry'],
"ReleaseFormat": release['ReleaseFormat']
}
myDB.upsert("allalbums", newValueDict, controlValueDict)
# Build the dictionary for the fullreleaselist
newValueDict['ReleaseID'] = release['id']
newValueDict['Tracks'] = releasedict['tracks']
newValueDict['ReleaseID'] = release['ReleaseID']
newValueDict['Tracks'] = release['Tracks']
fullreleaselist.append(newValueDict)
for track in releasedict['tracks']:
for track in release['Tracks']:
cleanname = helpers.cleanName(artist['artist_name'] + ' ' + rg['title'] + ' ' + track['title'])
controlValueDict = {"TrackID": track['id'],
"ReleaseID": release['id']}
"ReleaseID": release['ReleaseID']}
newValueDict = {"ArtistID": artistid,
"ArtistName": artist['artist_name'],
"AlbumTitle": rg['title'],
"AlbumASIN": releasedict['asin'],
"AlbumID": rg['id'],
newValueDict = {"ArtistID": release['ArtistID'],
"ArtistName": release['ArtistName'],
"AlbumTitle": release['AlbumTitle'],
"AlbumID": release['AlbumID'],
"AlbumASIN": release['AlbumASIN'],
"TrackTitle": track['title'],
"TrackDuration": track['duration'],
"TrackNumber": track['number'],
@@ -584,6 +566,8 @@ def getHybridRelease(fullreleaselist):
"""
Returns a dictionary of best group of tracks from the list of releases & earliest release date
"""
if len(fullreleaselist) == 0:
raise Exception("getHybridRelease was called with an empty fullreleaselist")
sortable_release_list = []
for release in fullreleaselist:

View File

@@ -387,8 +387,8 @@ def get_all_releases(rgid):
release = {}
release['AlbumTitle'] = unicode(releasedata['title'])
release['AlbumID'] = unicode(rgid)
release['AlbumASIN'] = unicode(releasedata['asin'])
release['ReleaseDate'] = unicode(releasedata['date'])
release['AlbumASIN'] = unicode(releasedata['asin']) if 'asin' in releasedata else None
release['ReleaseDate'] = unicode(releasedata['date']) if 'date' in releasedata else None
release['ReleaseID'] = releasedata['id']
if 'release-group' not in releasedata:
raise Exception('No release group associated with release id ' + releasedata['id'] + ' album id' + rgid)
@@ -400,7 +400,8 @@ def get_all_releases(rgid):
release['ArtistID'] = unicode(releasedata['artist-credit'][0]['artist']['id'])
release['ArtistName'] = unicode(releasedata['artist-credit-phrase'])
else:
raise Exception('Release ' + releasedata['id'] + ' has no Artists associated.')
logger.warn('Release ' + releasedata['id'] + ' has no Artists associated.')
return False
release['ReleaseCountry'] = unicode(releasedata['country']) if 'country' in releasedata else u'Unknown'