diff --git a/headphones/__init__.py b/headphones/__init__.py index 5c128dda..e916057a 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -441,6 +441,7 @@ def dbcheck(): c.execute('CREATE TABLE IF NOT EXISTS have (ArtistName TEXT, AlbumTitle TEXT, TrackNumber TEXT, TrackTitle TEXT, TrackLength TEXT, BitRate TEXT, Genre TEXT, Date TEXT, TrackID TEXT)') c.execute('CREATE TABLE IF NOT EXISTS lastfmcloud (ArtistName TEXT, ArtistID TEXT, Count INTEGER)') c.execute('CREATE TABLE IF NOT EXISTS descriptions (ReleaseGroupID TEXT, ReleaseID TEXT, Summary TEXT, Content TEXT)') + c.execute('CREATE TABLE IF NOT EXISTS releases (ReleaseID TEXT, ReleaseGroupID TEXT, UNIQUE(ReleaseID, ReleaseGroupID))') try: c.execute('SELECT IncludeExtras from artists') diff --git a/headphones/importer.py b/headphones/importer.py index aa22b690..d0841bfc 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -259,29 +259,39 @@ def addArtisttoDB(artistid, extrasonly=False): logger.info(u"Updating complete for: " + artist['artist_name']) 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 - if not release_dict: - logger.info('Unable to get release information for Release: ' + str(rid) + " no dict") - return - - rgid = release_dict['rgid'] + + 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'] #we don't want to make more calls to MB here unless we have to, could be happening quite a lot - #TODO: why do I have to str() this here? I don't get it. 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=?", [release_dict['artist_id']]) - if not artist_exists: + 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: @@ -299,8 +309,13 @@ def addReleaseById(rid): newValueDict['IncludeExtras'] = 1 myDB.upsert("artists", newValueDict, controlValueDict) - - if not rg_exists: + + 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.") + return + + if not rg_exists 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} @@ -315,11 +330,14 @@ def addReleaseById(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']: controlValueDict = {"TrackID": track['id'], - "AlbumID": release_dict['rgid']} + "AlbumID": rgid} newValueDict = {"ArtistID": release_dict['artist_id'], "ArtistName": release_dict['artist_name'], "AlbumTitle": release_dict['rg_title'], @@ -330,10 +348,12 @@ def addReleaseById(rid): } myDB.upsert("tracks", newValueDict, controlValueDict) - - + #start a search for the album import searcher searcher.searchNZB(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.") + return else: logger.info('Release ' + str(rid) + " already exists in the database!") \ No newline at end of file diff --git a/headphones/searcher.py b/headphones/searcher.py index c472ac17..763e6326 100644 --- a/headphones/searcher.py +++ b/headphones/searcher.py @@ -443,7 +443,7 @@ def verifyresult(title, term): return True def getresultNZB(result): - if result[3] == 'Newzbin': + if result[3] == 'newzbin': params = urllib.urlencode({"username": headphones.NEWZBIN_UID, "password": headphones.NEWZBIN_PASSWORD, "reportid": result[2]}) url = "https://www.newzbin.com" + "/api/dnzb/" urllib._urlopener = NewzbinDownloader() @@ -453,9 +453,9 @@ def getresultNZB(result): logger.warn('Error fetching nzb from url: %s. Error: %s' % (url, e)) else: try: - nzb = urllib2.urlopen(result[2], timeout=20).read() - except Exception, e: - logger.warn('Error fetching nzb from url: %s. Error: %s' % (result[2], e)) + nzb = urllib2.urlopen(result[2], timeout=30).read() + except urllib2.URLError, e: + logger.warn('Error fetching nzb from url: ' + result[2] + ' %s' % e) return nzb def preprocess(resultlist): @@ -478,4 +478,4 @@ def preprocess(resultlist): return nzb, result else: logger.error("Couldn't retrieve the best nzb. Skipping.") - return (False, False) + return (False, False) \ No newline at end of file