def addReleaseById(rid): myDB = db.DBConnection() rgid = None artistid = None release_dict = None results = myDB.select("SELECT albums.ArtistID, releases.ReleaseGroupID from releases, albums WHERE releases.ReleaseID=? and releases.ReleaseGroupID=albums.AlbumID LIMIT 1", [rid]) for result in results: rgid = result['ReleaseGroupID'] artistid = result['ArtistID'] logger.debug("Found a cached releaseid : releasegroupid relationship: " + rid + " : " + rgid) if not rgid: #didn't find it in the cache, get the information from MB logger.debug("Didn't find releaseID " + rid + " in the cache. Looking up its ReleaseGroupID") try: release_dict = mb.getRelease(rid) except Exception, e: logger.info('Unable to get release information for Release: ' + str(rid) + " " + str(e)) return if not release_dict: logger.info('Unable to get release information for Release: ' + str(rid) + " no dict") return rgid = release_dict['rgid'] artistid = release_dict['artist_id']
def addReleaseById(rid): myDB = db.DBConnection() rgid = None artistid = None release_dict = None results = myDB.select( "SELECT albums.ArtistID, releases.ReleaseGroupID from releases, albums WHERE releases.ReleaseID=? and releases.ReleaseGroupID=albums.AlbumID LIMIT 1", [rid], ) for result in results: rgid = result["ReleaseGroupID"] artistid = result["ArtistID"] logger.debug("Found a cached releaseid : releasegroupid relationship: " + rid + " : " + rgid) if not rgid: # didn't find it in the cache, get the information from MB logger.debug("Didn't find releaseID " + rid + " in the cache. Looking up its ReleaseGroupID") try: release_dict = mb.getRelease(rid) except Exception, e: logger.info("Unable to get release information for Release: " + str(rid) + " " + str(e)) return if not release_dict: logger.info("Unable to get release information for Release: " + str(rid) + " no dict") return rgid = release_dict["rgid"] artistid = release_dict["artist_id"]
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 addReleaseById(rid): myDB = db.DBConnection() #we have to make a call to get the release no matter what so we can get the RGID #need a way around this - a local cache maybe in the future maybe? try: release_dict = mb.getRelease(rid) except Exception, e: logger.info('Unable to get release information for Release: ' + str(rid) + " " + str(e)) return
def addReleaseById(rid, rgid=None): myDB = db.DBConnection() # Create minimum info upfront if added from searchresults status = '' if rgid: dbalbum = myDB.select("SELECT * from albums WHERE AlbumID=?", [rgid]) if not dbalbum: status = 'Loading' controlValueDict = {"AlbumID": rgid} newValueDict = {"AlbumTitle": rgid, "ArtistName": status, "Status": status} myDB.upsert("albums", newValueDict, controlValueDict) time.sleep(1) rgid = None artistid = None release_dict = None results = myDB.select("SELECT albums.ArtistID, releases.ReleaseGroupID from releases, albums WHERE releases.ReleaseID=? and releases.ReleaseGroupID=albums.AlbumID LIMIT 1", [rid]) for result in results: rgid = result['ReleaseGroupID'] artistid = result['ArtistID'] logger.debug("Found a cached releaseid : releasegroupid relationship: " + rid + " : " + rgid) if not rgid: #didn't find it in the cache, get the information from MB logger.debug("Didn't find releaseID " + rid + " in the cache. Looking up its ReleaseGroupID") try: release_dict = mb.getRelease(rid) except Exception, e: logger.info('Unable to get release information for Release %s: %s', rid, e) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return if not release_dict: logger.info('Unable to get release information for Release %s: no dict', rid) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return rgid = release_dict['rgid'] artistid = release_dict['artist_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 # 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 addReleaseById(rid, rgid=None): myDB = db.DBConnection() # Create minimum info upfront if added from searchresults status = '' if rgid: dbalbum = myDB.select("SELECT * from albums WHERE AlbumID=?", [rgid]) if not dbalbum: status = 'Loading' controlValueDict = {"AlbumID": rgid} newValueDict = {"AlbumTitle": rgid, "ArtistName": status, "Status": status} myDB.upsert("albums", newValueDict, controlValueDict) time.sleep(1) rgid = None artistid = None release_dict = None results = myDB.select("SELECT albums.ArtistID, releases.ReleaseGroupID from releases, albums WHERE releases.ReleaseID=? and releases.ReleaseGroupID=albums.AlbumID LIMIT 1", [rid]) for result in results: rgid = result['ReleaseGroupID'] artistid = result['ArtistID'] logger.debug("Found a cached releaseid : releasegroupid relationship: " + rid + " : " + rgid) if not rgid: #didn't find it in the cache, get the information from MB logger.debug("Didn't find releaseID " + rid + " in the cache. Looking up its ReleaseGroupID") try: release_dict = mb.getRelease(rid) except Exception as e: logger.info('Unable to get release information for Release %s: %s', rid, e) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return if not release_dict: logger.info('Unable to get release information for Release %s: no dict', rid) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return rgid = release_dict['rgid'] artistid = release_dict['artist_id'] #we don't want to make more calls to MB here unless we have to, could be happening quite a lot rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rgid]) #make sure the artist exists since I don't know what happens later if it doesn't artist_exists = myDB.select("SELECT * from artists WHERE ArtistID=?", [artistid]) if not artist_exists and release_dict: if release_dict['artist_name'].startswith('The '): sortname = release_dict['artist_name'][4:] else: sortname = release_dict['artist_name'] logger.info(u"Now manually adding: " + release_dict['artist_name'] + " - with status Paused") controlValueDict = {"ArtistID": release_dict['artist_id']} newValueDict = {"ArtistName": release_dict['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Paused"} if headphones.CONFIG.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 newValueDict['Extras'] = headphones.CONFIG.EXTRAS myDB.upsert("artists", newValueDict, controlValueDict) elif not artist_exists and not release_dict: logger.error("Artist does not exist in the database and did not get a valid response from MB. Skipping release.") if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return if not rg_exists and release_dict or status == 'Loading' and release_dict: #it should never be the case that we have an rg and not the artist #but if it is this will fail logger.info(u"Now adding-by-id album (" + release_dict['title'] + ") from id: " + rgid) controlValueDict = {"AlbumID": rgid} if status != 'Loading': status = 'Wanted' newValueDict = {"ArtistID": release_dict['artist_id'], "ReleaseID": rgid, "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['title'] if 'title' in release_dict else release_dict['rg_title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['date'], "DateAdded": helpers.today(), "Status": status, "Type": release_dict['rg_type'], "ReleaseID": rid } myDB.upsert("albums", newValueDict, controlValueDict) #keep a local cache of these so that external programs that are adding releasesByID don't hammer MB myDB.action('INSERT INTO releases VALUES( ?, ?)', [rid, release_dict['rgid']]) for track in release_dict['tracks']: cleanname = helpers.cleanName(release_dict['artist_name'] + ' ' + release_dict['rg_title'] + ' ' + track['title']) controlValueDict = {"TrackID": track['id'], "AlbumID": rgid} newValueDict = {"ArtistID": release_dict['artist_id'], "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['rg_title'], "AlbumASIN": release_dict['asin'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'], "CleanName": cleanname } match = myDB.action('SELECT Location, BitRate, Format, Matched from have WHERE CleanName=?', [cleanname]).fetchone() if not match: match = myDB.action('SELECT Location, BitRate, Format, Matched from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ? AND TrackTitle LIKE ?', [release_dict['artist_name'], release_dict['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']]) # If the album has been scanned before adding the release it will be unmatched, update to matched if match['Matched'] == 'Failed': myDB.action('UPDATE have SET Matched=? WHERE Location=?', (release_dict['rgid'], match['Location'])) myDB.upsert("tracks", newValueDict, controlValueDict) # Reset status if status == 'Loading': controlValueDict = {"AlbumID": rgid} if headphones.CONFIG.AUTOWANT_MANUALLY_ADDED: newValueDict = {"Status": "Wanted"} else: newValueDict = {"Status": "Skipped"} myDB.upsert("albums", newValueDict, controlValueDict) # Start a search for the album if headphones.CONFIG.AUTOWANT_MANUALLY_ADDED: import searcher searcher.searchforalbum(rgid, False) elif not rg_exists and not release_dict: logger.error("ReleaseGroup does not exist in the database and did not get a valid response from MB. Skipping release.") if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return else: logger.info('Release ' + str(rid) + " already exists in the database!")
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 addReleaseById(rid, rgid=None): myDB = db.DBConnection() # Create minimum info upfront if added from searchresults status = '' if rgid: dbalbum = myDB.select("SELECT * from albums WHERE AlbumID=?", [rgid]) if not dbalbum: status = 'Loading' controlValueDict = {"AlbumID": rgid} newValueDict = { "AlbumTitle": rgid, "ArtistName": status, "Status": status } myDB.upsert("albums", newValueDict, controlValueDict) time.sleep(1) rgid = None artistid = None release_dict = None results = myDB.select( "SELECT albums.ArtistID, releases.ReleaseGroupID from releases, albums WHERE releases.ReleaseID=? and releases.ReleaseGroupID=albums.AlbumID LIMIT 1", [rid]) for result in results: rgid = result['ReleaseGroupID'] artistid = result['ArtistID'] logger.debug( "Found a cached releaseid : releasegroupid relationship: " + rid + " : " + rgid) if not rgid: # didn't find it in the cache, get the information from MB logger.debug("Didn't find releaseID " + rid + " in the cache. Looking up its ReleaseGroupID") try: release_dict = mb.getRelease(rid) except Exception as e: logger.info('Unable to get release information for Release %s: %s', rid, e) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return if not release_dict: logger.info( 'Unable to get release information for Release %s: no dict', rid) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return rgid = release_dict['rgid'] artistid = release_dict['artist_id'] # we don't want to make more calls to MB here unless we have to, could be happening quite a lot rg_exists = myDB.select("SELECT * from albums WHERE AlbumID=?", [rgid]) # make sure the artist exists since I don't know what happens later if it doesn't artist_exists = myDB.select("SELECT * from artists WHERE ArtistID=?", [artistid]) if not artist_exists and release_dict: if release_dict['artist_name'].startswith('The '): sortname = release_dict['artist_name'][4:] else: sortname = release_dict['artist_name'] logger.info(u"Now manually adding: " + release_dict['artist_name'] + " - with status Paused") controlValueDict = {"ArtistID": release_dict['artist_id']} newValueDict = { "ArtistName": release_dict['artist_name'], "ArtistSortName": sortname, "DateAdded": helpers.today(), "Status": "Paused" } if headphones.CONFIG.INCLUDE_EXTRAS: newValueDict['IncludeExtras'] = 1 newValueDict['Extras'] = headphones.CONFIG.EXTRAS if 'title' in release_dict: newValueDict['LatestAlbum'] = release_dict['title'] elif 'rg_title' in release_dict: newValueDict['LatestAlbum'] = release_dict['rg_title'] if 'date' in release_dict: newValueDict['ReleaseDate'] = release_dict['date'] if rgid: newValueDict['AlbumID'] = rgid myDB.upsert("artists", newValueDict, controlValueDict) elif not artist_exists and not release_dict: logger.error( "Artist does not exist in the database and did not get a valid response from MB. Skipping release." ) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return if not rg_exists and release_dict or status == 'Loading' and release_dict: # it should never be the case that we have an rg and not the artist # but if it is this will fail logger.info(u"Now adding-by-id album (" + release_dict['title'] + ") from id: " + rgid) controlValueDict = {"AlbumID": rgid} if status != 'Loading': status = 'Wanted' newValueDict = { "ArtistID": release_dict['artist_id'], "ReleaseID": rgid, "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['title'] if 'title' in release_dict else release_dict['rg_title'], "AlbumASIN": release_dict['asin'], "ReleaseDate": release_dict['date'], "DateAdded": helpers.today(), "Status": status, "Type": release_dict['rg_type'], "ReleaseID": rid } myDB.upsert("albums", newValueDict, controlValueDict) # keep a local cache of these so that external programs that are adding releasesByID don't hammer MB myDB.action('INSERT INTO releases VALUES( ?, ?)', [rid, release_dict['rgid']]) for track in release_dict['tracks']: cleanname = helpers.clean_name(release_dict['artist_name'] + ' ' + release_dict['rg_title'] + ' ' + track['title']) controlValueDict = {"TrackID": track['id'], "AlbumID": rgid} newValueDict = { "ArtistID": release_dict['artist_id'], "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['rg_title'], "AlbumASIN": release_dict['asin'], "TrackTitle": track['title'], "TrackDuration": track['duration'], "TrackNumber": track['number'], "CleanName": cleanname } match = myDB.action( 'SELECT Location, BitRate, Format, Matched from have WHERE CleanName=?', [cleanname]).fetchone() if not match: match = myDB.action( 'SELECT Location, BitRate, Format, Matched from have WHERE ArtistName LIKE ? AND AlbumTitle LIKE ? AND TrackTitle LIKE ?', [ release_dict['artist_name'], release_dict['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']]) # If the album has been scanned before adding the release it will be unmatched, update to matched if match['Matched'] == 'Failed': myDB.action('UPDATE have SET Matched=? WHERE Location=?', (release_dict['rgid'], match['Location'])) myDB.upsert("tracks", newValueDict, controlValueDict) # Reset status if status == 'Loading': controlValueDict = {"AlbumID": rgid} if headphones.CONFIG.AUTOWANT_MANUALLY_ADDED: newValueDict = {"Status": "Wanted"} else: newValueDict = {"Status": "Skipped"} myDB.upsert("albums", newValueDict, controlValueDict) # Start a search for the album if headphones.CONFIG.AUTOWANT_MANUALLY_ADDED: import searcher searcher.searchforalbum(rgid, False) elif not rg_exists and not release_dict: logger.error( "ReleaseGroup does not exist in the database and did not get a valid response from MB. Skipping release." ) if status == 'Loading': myDB.action("DELETE FROM albums WHERE AlbumID=?", [rgid]) return else: logger.info('Release ' + str(rid) + " already exists in the database!")
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)