def dbUpdate(): myDB = db.DBConnection() activeartists = myDB.select('SELECT ArtistID, ArtistName from artists WHERE Status="Active"') i = 0 while i < len(activeartists): artistid = activeartists[i][0] artistname = activeartists[i][1] logger.info(u"Updating album information for artist: " + artistname) artist = mb.getArtist(artistid) for rg in artist['releasegroups']: rgid = rg['id'] releaseid = mb.getReleaseGroup(rgid) results = mb.getRelease(releaseid) albumlist = myDB.select('SELECT AlbumID from albums WHERE ArtistID=?', [artistid]) if any(releaseid in x for x in albumlist): logger.info(results['title'] + " already exists in the database. Updating ASIN, Release Date, Tracks") myDB.action('UPDATE albums SET AlbumASIN=?, ReleaseDate=? WHERE AlbumID=?', [results['asin'], results['date'], results['id']]) for track in results['tracks']: myDB.action('UPDATE tracks SET TrackDuration=? WHERE AlbumID=? AND TrackID=?', [track['duration'], results['id'], track['id']]) else: logger.info(u"New album found! Adding "+results['title']+"to the database...") myDB.action('INSERT INTO albums VALUES( ?, ?, ?, ?, ?, CURRENT_DATE, ?, ?)', [artistid, artist['artist_name'], rg['title'], results['asin'], results['date'], results['id'], 'Skipped']) latestrelease = myDB.select('SELECT ReleaseDate, DateAdded from albums WHERE AlbumID=?', [results['id']]) if latestrelease[0][0] > latestrelease[0][1]: myDB.action('UPDATE albums SET Status = "Wanted" WHERE AlbumID=?', results['id']) else: pass for track in results['tracks']: myDB.action('INSERT INTO tracks VALUES( ?, ?, ?, ?, ?, ?, ?, ?)', [artistid, artist['artist_name'], rg['title'], results['asin'], results['id'], track['title'], track['duration'], track['id']]) i += 1
def addArtisttoDB(artistid, extrasonly=False): # Putting this here to get around the circular import. We're using this to update thumbnails for artist/albums from headphones import cache # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return # We'll use this to see if we should update the 'LastUpdated' time stamp errors = False myDB = db.DBConnection() # Delete from blacklist if it's on there myDB.action('DELETE from blacklist WHERE ArtistID=?', [artistid]) # We need the current minimal info in the database instantly # so we don't throw a 500 error when we redirect to the artistPage controlValueDict = {"ArtistID": artistid} # Don't replace a known artist name with an "Artist ID" placeholder dbartist = myDB.action('SELECT * FROM artists WHERE ArtistID=?', [artistid]).fetchone() # Only modify the Include Extras stuff if it's a new artist. We need it early so we know what to fetch if not dbartist: newValueDict = {"ArtistName": "Artist ID: %s" % (artistid), "Status": "Loading", "IncludeExtras": headphones.INCLUDE_EXTRAS, "Extras": headphones.EXTRAS } else: newValueDict = {"Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) artist = mb.getArtist(artistid, extrasonly) if not artist: logger.warn("Error fetching artist info. ID: " + artistid) if dbartist is None: newValueDict = {"ArtistName": "Fetch failed, try refreshing. (%s)" % (artistid), "Status": "Active"} else: newValueDict = {"Status": "Active"} myDB.upsert("artists", newValueDict, controlValueDict) return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = {"ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: logger.info("Now adding/updating: " + rg['title']) rgid = rg['id'] # 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: errors = True continue # This will be used later to build a hybrid release fullreleaselist = [] for release in releaselist: # 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['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'] } myDB.upsert("allalbums", newValueDict, controlValueDict) # Build the dictionary for the fullreleaselist newValueDict['ReleaseID'] = release['id'] newValueDict['Tracks'] = releasedict['tracks'] fullreleaselist.append(newValueDict) for track in releasedict['tracks']: cleanname = helpers.cleanName(artist['artist_name'] + ' ' + rg['title'] + ' ' + track['title']) controlValueDict = {"TrackID": track['id'], "ReleaseID": release['id']} newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": releasedict['asin'], "AlbumID": rg['id'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'], "CleanName": cleanname } match = myDB.action('SELECT Location, BitRate, Format from have WHERE CleanName=?', [cleanname]).fetchone() if not match: match = myDB.action('SELECT Location, BitRate, Format from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ? AND TrackTitle LIKE ?', [artist['artist_name'], rg['title'], track['title']]).fetchone() if not match: match = myDB.action('SELECT Location, BitRate, Format from have WHERE TrackID=?', [track['id']]).fetchone() if match: newValueDict['Location'] = match['Location'] newValueDict['BitRate'] = match['BitRate'] newValueDict['Format'] = match['Format'] myDB.action('UPDATE have SET Matched="True" WHERE Location=?', [match['Location']]) myDB.upsert("alltracks", newValueDict, controlValueDict)
def addArtisttoDB(artistid, extrasonly=False): # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() artist = mb.getArtist(artistid, extrasonly) if not artist: return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = {"ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading"} if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg['id']]) try: release_dict = 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 release_dict: continue logger.info(u"Now adding/updating album: " + rg['title']) controlValueDict = {"AlbumID": rg['id']} if len(rg_exists): newValueDict = {"AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], } else: newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], "DateAdded": helpers.today(), "Type": rg['type'] } if release_dict['releasedate'] > helpers.today(): newValueDict['Status'] = "Wanted" else: newValueDict['Status'] = "Skipped" myDB.upsert("albums", newValueDict, controlValueDict) try: lastfm.getAlbumDescription(rg['id'], artist['artist_name'], rg['title']) except Exception, e: logger.error('Attempt to retrieve album description from Last.fm failed: %s' % e)
def addArtisttoDB(artistid, extrasonly=False): # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() # We need the current minimal info in the database instantly # so we don't throw a 500 error when we redirect to the artistPage controlValueDict = {"ArtistID": artistid} # Don't replace a known artist name with an "Artist ID" placeholder dbartist = myDB.action('SELECT * FROM artists WHERE ArtistID=?', [artistid]).fetchone() if dbartist is None: newValueDict = {"ArtistName": "Artist ID: %s" % (artistid), "Status": "Loading"} else: newValueDict = {"Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) artist = mb.getArtist(artistid, extrasonly) if not artist: logger.warn("Error fetching artist info. ID: " + artistid) if dbartist is None: newValueDict = {"ArtistName": "Fetch failed, try refreshing. (%s)" % (artistid), "Status": "Active"} else: newValueDict = {"Status": "Active"} myDB.upsert("artists", newValueDict, controlValueDict) return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = {"ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading"} if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg['id']]) try: release_dict = 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 release_dict: continue logger.info(u"Now adding/updating album: " + rg['title']) controlValueDict = {"AlbumID": rg['id']} if len(rg_exists): newValueDict = {"AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], } else: newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], "DateAdded": helpers.today(), "Type": rg['type'] } if release_dict['releasedate'] > helpers.today(): newValueDict['Status'] = "Wanted" else: newValueDict['Status'] = "Skipped" myDB.upsert("albums", newValueDict, controlValueDict) try: lastfm.getAlbumDescription(rg['id'], artist['artist_name'], rg['title']) except Exception, e: logger.error('Attempt to retrieve album description from Last.fm failed: %s' % e)
def verify(albumid, albumpath, Kind=None, forced=False): myDB = db.DBConnection() release = myDB.action('SELECT * from albums WHERE AlbumID=?', [albumid]).fetchone() tracks = myDB.select('SELECT * from tracks WHERE AlbumID=?', [albumid]) if not release or not tracks: #the result of a manual post-process on an album that hasn't been inserted #from an RSS feed or etc #TODO: This should be a call to a class method.. copied it out of importer with only minor changes #TODO: odd things can happen when there are diacritic characters in the folder name, need to translate them? release_list = None try: release_list = mb.getReleaseGroup(albumid) except Exception, e: logger.error('Unable to get release information for manual album with rgid: %s. Error: %s' % (albumid, e)) return if not release_list: logger.error('Unable to get release information for manual album with rgid: %s' % albumid) return # Since we're just using this to create the bare minimum information to insert an artist/album combo, use the first release releaseid = release_list[0]['id'] release_dict = mb.getRelease(releaseid) if not release_dict: logger.error('Unable to get release information for manual album with rgid: %s. Cannot continue' % albumid) return logger.info(u"Now adding/updating artist: " + release_dict['artist_name']) if release_dict['artist_name'].startswith('The '): sortname = release_dict['artist_name'][4:] else: sortname = release_dict['artist_name'] controlValueDict = {"ArtistID": release_dict['artist_id']} newValueDict = {"ArtistName": release_dict['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Paused"} logger.info("ArtistID: " + release_dict['artist_id'] + " , ArtistName: " + release_dict['artist_name']) if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 newValueDict['Extras'] = headphones.EXTRAS myDB.upsert("artists", newValueDict, controlValueDict) logger.info(u"Now adding album: " + release_dict['title']) controlValueDict = {"AlbumID": albumid} newValueDict = {"ArtistID": release_dict['artist_id'], "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['date'], "DateAdded": helpers.today(), "Type": release_dict['rg_type'], "Status": "Snatched" } myDB.upsert("albums", newValueDict, controlValueDict) # Delete existing tracks associated with this AlbumID since we're going to replace them and don't want any extras myDB.action('DELETE from tracks WHERE AlbumID=?', [albumid]) for track in release_dict['tracks']: controlValueDict = {"TrackID": track['id'], "AlbumID": albumid} newValueDict = {"ArtistID": release_dict['artist_id'], "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['title'], "AlbumASIN": release_dict['asin'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'] } myDB.upsert("tracks", newValueDict, controlValueDict) controlValueDict = {"ArtistID": release_dict['artist_id']} newValueDict = {"Status": "Paused"} myDB.upsert("artists", newValueDict, controlValueDict) logger.info(u"Addition complete for: " + release_dict['title'] + " - " + release_dict['artist_name']) release = myDB.action('SELECT * from albums WHERE AlbumID=?', [albumid]).fetchone() tracks = myDB.select('SELECT * from tracks WHERE AlbumID=?', [albumid])
def addArtisttoDB(artistid): if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() artistlist = myDB.select('SELECT ArtistID, ArtistName from artists WHERE ArtistID=?', [artistid]) if any(artistid in x for x in artistlist): logger.info(artistlist[0][1] + u" is already in the database, skipping") return artist = mb.getArtist(artistid) if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] controlValueDict = {"ArtistID": artistid} newValueDict = {"ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] try: releaseid = mb.getReleaseGroup(rgid) except Exception, e: logger.info('Unable to get release information for %s - it may not be a valid release group' % rg['title']) continue release = mb.getRelease(releaseid) logger.info(u"Now adding album: " + release['title']+ " to the database") controlValueDict = {"AlbumID": release['id']} newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release['asin'], "ReleaseDate": release['date'], "DateAdded": helpers.today(), "Status": "Skipped" } myDB.upsert("albums", newValueDict, controlValueDict) latestrelease = myDB.select("SELECT ReleaseDate, DateAdded from albums WHERE AlbumID=?", [release['id']]) if latestrelease[0][0] > latestrelease[0][1]: logger.info(release['title'] + u" is an upcoming album. Setting its status to 'Wanted'...") controlValueDict = {"AlbumID": release['id']} newValueDict = {"Status": "Wanted"} myDB.upsert("albums", newValueDict, controlValueDict) for track in release['tracks']: myDB.action('INSERT INTO tracks VALUES( ?, ?, ?, ?, ?, ?, ?, ?)', [artistid, artist['artist_name'], rg['title'], release['asin'], release['id'], track['title'], track['duration'], track['id']])
def addArtisttoDB(artistid, extrasonly=False): # Putting this here to get around the circular import. We're using this to update thumbnails for artist/albums from headphones import cache # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() # Delete from blacklist if it's on there myDB.action('DELETE from blacklist WHERE ArtistID=?', [artistid]) # We need the current minimal info in the database instantly # so we don't throw a 500 error when we redirect to the artistPage controlValueDict = {"ArtistID": artistid} # Don't replace a known artist name with an "Artist ID" placeholder dbartist = myDB.action('SELECT * FROM artists WHERE ArtistID=?', [artistid]).fetchone() if dbartist is None: newValueDict = {"ArtistName": "Artist ID: %s" % (artistid), "Status": "Loading"} else: newValueDict = {"Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) artist = mb.getArtist(artistid, extrasonly) if not artist: logger.warn("Error fetching artist info. ID: " + artistid) if dbartist is None: newValueDict = {"ArtistName": "Fetch failed, try refreshing. (%s)" % (artistid), "Status": "Active"} else: newValueDict = {"Status": "Active"} myDB.upsert("artists", newValueDict, controlValueDict) return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = {"ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading"} if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg['id']]) try: release_dict = 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 release_dict: continue logger.info(u"Now adding/updating album: " + rg['title']) controlValueDict = {"AlbumID": rg['id']} newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], "Type": rg['type'] } # Only change the status & add DateAdded if the album is not already in the database if not len(rg_exists): newValueDict['DateAdded']= helpers.today() if headphones.AUTOWANT_ALL: newValueDict['Status'] = "Wanted" elif release_dict['releasedate'] > helpers.today() and headphones.AUTOWANT_UPCOMING: newValueDict['Status'] = "Wanted" else: newValueDict['Status'] = "Skipped" myDB.upsert("albums", newValueDict, controlValueDict) for track in release_dict['tracks']: cleanname = helpers.cleanName(artist['artist_name'] + ' ' + rg['title'] + ' ' + track['title']) controlValueDict = {"TrackID": track['id'], "AlbumID": rg['id']} newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'], "CleanName": cleanname } match = myDB.action('SELECT Location, BitRate, Format from have WHERE CleanName=?', [cleanname]).fetchone() if not match: match = myDB.action('SELECT Location, BitRate, Format from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ? AND TrackTitle LIKE ?', [artist['artist_name'], rg['title'], track['title']]).fetchone() if not match: match = myDB.action('SELECT Location, BitRate, Format from have WHERE TrackID=?', [track['id']]).fetchone() if match: newValueDict['Location'] = match['Location'] newValueDict['BitRate'] = match['BitRate'] newValueDict['Format'] = match['Format'] myDB.action('DELETE from have WHERE Location=?', [match['Location']]) myDB.upsert("tracks", newValueDict, controlValueDict) logger.debug(u"Updating album cache for " + rg['title']) cache.getThumb(AlbumID=rg['id'])
def addArtisttoDB(artistid, extrasonly=False): # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn("Cannot import Various Artists.") return myDB = db.DBConnection() artist = mb.getArtist(artistid, extrasonly) if not artist: return if artist["artist_name"].startswith("The "): sortname = artist["artist_name"][4:] else: sortname = artist["artist_name"] logger.info(u"Now adding/updating: " + artist["artist_name"]) controlValueDict = {"ArtistID": artistid} newValueDict = { "ArtistName": artist["artist_name"], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading", } if headphones.INCLUDE_EXTRAS: newValueDict["IncludeExtras"] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist["releasegroups"]: rgid = rg["id"] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg["id"]]) try: release_dict = 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 release_dict: continue logger.info(u"Now adding/updating album: " + rg["title"]) controlValueDict = {"AlbumID": rg["id"]} if len(rg_exists): newValueDict = {"AlbumASIN": release_dict["asin"], "ReleaseDate": release_dict["releasedate"]} else: newValueDict = { "ArtistID": artistid, "ArtistName": artist["artist_name"], "AlbumTitle": rg["title"], "AlbumASIN": release_dict["asin"], "ReleaseDate": release_dict["releasedate"], "DateAdded": helpers.today(), "Type": rg["type"], } if release_dict["releasedate"] > helpers.today(): newValueDict["Status"] = "Wanted" else: newValueDict["Status"] = "Skipped" if rg["type"] == "Album" and headphones.AUTOWANT_ALBUM: newValueDict["Status"] = "Wanted" if rg["type"] == "Compilation" and headphones.AUTOWANT_COMPILATION: newValueDict["Status"] = "Wanted" if rg["type"] == "EP" and headphones.AUTOWANT_EP: newValueDict["Status"] = "Wanted" if rg["type"] == "Remix" and headphones.AUTOWANT_REMIX: newValueDict["Status"] = "Wanted" if rg["type"] == "Single" and headphones.AUTOWANT_SINGLE: newValueDict["Status"] = "Wanted" if rg["type"] == "Live" and headphones.AUTOWANT_LIVE: newValueDict["Status"] = "Wanted" if rg["type"] == "Soundtrack" and headphones.AUTOWANT_SOUNDTRACK: newValueDict["Status"] = "Wanted" myDB.upsert("albums", newValueDict, controlValueDict) try: lastfm.getAlbumDescription(rg["id"], artist["artist_name"], rg["title"]) except Exception, e: logger.error("Attempt to retrieve album description from Last.fm failed: %s" % e)
def addArtisttoDB(artistid, extrasonly=False): # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() # We need the current minimal info in the database instantly # so we don't throw a 500 error when we redirect to the artistPage controlValueDict = {"ArtistID": artistid} # Don't replace a known artist name with an "Artist ID" placeholder dbartist = myDB.action('SELECT * FROM artists WHERE ArtistID=?', [artistid]).fetchone() if dbartist is None: newValueDict = { "ArtistName": "Artist ID: %s" % (artistid), "Status": "Loading" } else: newValueDict = {"Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) artist = mb.getArtist(artistid, extrasonly) if not artist: logger.warn("Error fetching artist info. ID: " + artistid) if dbartist is None: newValueDict = { "ArtistName": "Fetch failed, try refreshing. (%s)" % (artistid), "Status": "Active" } else: newValueDict = {"Status": "Active"} myDB.upsert("artists", newValueDict, controlValueDict) return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = { "ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading" } if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg['id']]) try: release_dict = 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 release_dict: continue logger.info(u"Now adding/updating album: " + rg['title']) controlValueDict = {"AlbumID": rg['id']} if len(rg_exists): newValueDict = { "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], } else: newValueDict = { "ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], "DateAdded": helpers.today(), "Type": rg['type'] } if headphones.AUTOWANT_ALL: newValueDict['Status'] = "Wanted" elif release_dict['releasedate'] > helpers.today( ) and headphones.AUTOWANT_UPCOMING: newValueDict['Status'] = "Wanted" else: newValueDict['Status'] = "Skipped" myDB.upsert("albums", newValueDict, controlValueDict) try: lastfm.getAlbumDescription(rg['id'], artist['artist_name'], rg['title']) except Exception, e: logger.error( 'Attempt to retrieve album description from Last.fm failed: %s' % e)
def addArtisttoDB(artistid, extrasonly=False): # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() artist = mb.getArtist(artistid, extrasonly) if not artist: return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = {"ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading"} if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg['id']]) try: release_dict = mb.getReleaseGroup(rgid) except Exception, e: logger.info('Unable to get release information for %s - it may not be a valid release group (or it might just not be tagged right in MusicBrainz)' % rg['title']) continue if not release_dict: continue logger.info(u"Now adding/updating album: " + rg['title']) controlValueDict = {"AlbumID": rg['id']} if len(rg_exists): newValueDict = {"AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], } else: newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], "DateAdded": helpers.today(), "Type": rg['type'] } if release_dict['releasedate'] > helpers.today(): newValueDict['Status'] = "Wanted" else: newValueDict['Status'] = "Skipped" myDB.upsert("albums", newValueDict, controlValueDict) # I changed the albumid from releaseid -> rgid, so might need to delete albums that have a releaseid for release in release_dict['releaselist']: myDB.action('DELETE from albums WHERE AlbumID=?', [release['releaseid']]) myDB.action('DELETE from tracks WHERE AlbumID=?', [release['releaseid']]) myDB.action('DELETE from tracks WHERE AlbumID=?', [rg['id']]) for track in release_dict['tracks']: controlValueDict = {"TrackID": track['id'], "AlbumID": rg['id']} newValueDict = {"ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'] } myDB.upsert("tracks", newValueDict, controlValueDict)
def addArtisttoDB(artistid, extrasonly=False): # Putting this here to get around the circular import. We're using this to update thumbnails for artist/albums from headphones import cache # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return # We'll use this to see if we should update the 'LastUpdated' time stamp errors = False myDB = db.DBConnection() # Delete from blacklist if it's on there myDB.action('DELETE from blacklist WHERE ArtistID=?', [artistid]) # We need the current minimal info in the database instantly # so we don't throw a 500 error when we redirect to the artistPage controlValueDict = {"ArtistID": artistid} # Don't replace a known artist name with an "Artist ID" placeholder dbartist = myDB.action('SELECT * FROM artists WHERE ArtistID=?', [artistid]).fetchone() # Only modify the Include Extras stuff if it's a new artist. We need it early so we know what to fetch if not dbartist: newValueDict = { "ArtistName": "Artist ID: %s" % (artistid), "Status": "Loading", "IncludeExtras": headphones.INCLUDE_EXTRAS, "Extras": headphones.EXTRAS } else: newValueDict = {"Status": "Loading"} myDB.upsert("artists", newValueDict, controlValueDict) artist = mb.getArtist(artistid, extrasonly) if not artist: logger.warn("Error fetching artist info. ID: " + artistid) if dbartist is None: newValueDict = { "ArtistName": "Fetch failed, try refreshing. (%s)" % (artistid), "Status": "Active" } else: newValueDict = {"Status": "Active"} myDB.upsert("artists", newValueDict, controlValueDict) return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = { "ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading" } myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: logger.info("Now adding/updating: " + rg['title']) rgid = rg['id'] # 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: errors = True continue # This will be used later to build a hybrid release fullreleaselist = [] for release in releaselist: # 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['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'] } myDB.upsert("allalbums", newValueDict, controlValueDict) # Build the dictionary for the fullreleaselist newValueDict['ReleaseID'] = release['id'] newValueDict['Tracks'] = releasedict['tracks'] fullreleaselist.append(newValueDict) for track in releasedict['tracks']: cleanname = helpers.cleanName(artist['artist_name'] + ' ' + rg['title'] + ' ' + track['title']) controlValueDict = { "TrackID": track['id'], "ReleaseID": release['id'] } newValueDict = { "ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": releasedict['asin'], "AlbumID": rg['id'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'], "CleanName": cleanname } match = myDB.action( 'SELECT Location, BitRate, Format from have WHERE CleanName=?', [cleanname]).fetchone() if not match: match = myDB.action( 'SELECT Location, BitRate, Format from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ? AND TrackTitle LIKE ?', [artist['artist_name'], rg['title'], track['title'] ]).fetchone() if not match: match = myDB.action( 'SELECT Location, BitRate, Format from have WHERE TrackID=?', [track['id']]).fetchone() if match: newValueDict['Location'] = match['Location'] newValueDict['BitRate'] = match['BitRate'] newValueDict['Format'] = match['Format'] myDB.action( 'UPDATE have SET Matched="True" WHERE Location=?', [match['Location']]) myDB.upsert("alltracks", newValueDict, controlValueDict)
def addArtisttoDB(artistid, extrasonly=False): # Can't add various artists - throws an error from MB if artistid == various_artists_mbid: logger.warn('Cannot import Various Artists.') return myDB = db.DBConnection() artist = mb.getArtist(artistid, extrasonly) if not artist: return if artist['artist_name'].startswith('The '): sortname = artist['artist_name'][4:] else: sortname = artist['artist_name'] logger.info(u"Now adding/updating: " + artist['artist_name']) controlValueDict = {"ArtistID": artistid} newValueDict = { "ArtistName": artist['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Loading" } if headphones.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) for rg in artist['releasegroups']: rgid = rg['id'] # check if the album already exists rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rg['id']]) try: release_dict = 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 release_dict: continue logger.info(u"Now adding/updating album: " + rg['title']) controlValueDict = {"AlbumID": rg['id']} if len(rg_exists): newValueDict = { "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], } else: newValueDict = { "ArtistID": artistid, "ArtistName": artist['artist_name'], "AlbumTitle": rg['title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['releasedate'], "DateAdded": helpers.today(), "Type": rg['type'] } if release_dict['releasedate'] > helpers.today(): newValueDict['Status'] = "Wanted" else: newValueDict['Status'] = "Skipped" myDB.upsert("albums", newValueDict, controlValueDict) try: lastfm.getAlbumDescription(rg['id'], artist['artist_name'], rg['title']) except Exception, e: logger.error( 'Attempt to retrieve album description from Last.fm failed: %s' % e)