diff --git a/API_REFERENCE b/API_REFERENCE index 75e00d95..8f19fe1f 100644 --- a/API_REFERENCE +++ b/API_REFERENCE @@ -12,7 +12,9 @@ $commands¶meters[&optionalparameters]: getIndex (fetch data from index page. Returns: ArtistName, ArtistSortName, ArtistID, Status, DateAdded, [LatestAlbum, ReleaseDate, AlbumID], HaveTracks, TotalTracks, - IncludeExtras, LastUpdated, [ArtworkURL, ThumbURL]: a remote url to the artwork/thumbnail. To get the cached image path, see getArtistArt command) + IncludeExtras, LastUpdated, [ArtworkURL, ThumbURL]: a remote url to the artwork/thumbnail. To get the cached image path, see getArtistArt command. + ThumbURL is added/updated when an artist is added/updated. If your using the database method to get the artwork, + it's more reliable to use the ThumbURL than the ArtworkURL) getArtist&id=$artistid (fetch artist data. returns the artist object (see above) and album info: Status, AlbumASIN, DateAdded, AlbumTitle, ArtistName, ReleaseDate, AlbumID, ArtistID, Type, ArtworkURL: hosted image path. For cached image, see getAlbumArt command) diff --git a/headphones/__init__.py b/headphones/__init__.py index ca184949..0e063353 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -678,7 +678,7 @@ def dbcheck(): c.execute('CREATE TABLE IF NOT EXISTS snatched (AlbumID TEXT, Title TEXT, Size INTEGER, URL TEXT, DateAdded TEXT, Status TEXT, FolderName TEXT)') 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, Location TEXT, CleanName TEXT, Format 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 descriptions (ArtistID TEXT, ReleaseGroupID TEXT, ReleaseID TEXT, Summary TEXT, Content TEXT)') c.execute('CREATE TABLE IF NOT EXISTS releases (ReleaseID TEXT, ReleaseGroupID TEXT, UNIQUE(ReleaseID, ReleaseGroupID))') c.execute('CREATE INDEX IF NOT EXISTS tracks_albumid ON tracks(AlbumID ASC)') c.execute('CREATE INDEX IF NOT EXISTS album_artistid_reldate ON albums(ArtistID ASC, ReleaseDate DESC)') @@ -788,6 +788,11 @@ def dbcheck(): c.execute('SELECT ThumbURL from albums') except sqlite3.OperationalError: c.execute('ALTER TABLE albums ADD COLUMN ThumbURL TEXT DEFAULT NULL') + + try: + c.execute('SELECT ArtistID from descriptions') + except sqlite3.OperationalError: + c.execute('ALTER TABLE descriptions ADD COLUMN ArtistID TEXT DEFAULT NULL') conn.commit() c.close() diff --git a/headphones/cache.py b/headphones/cache.py index 5fd9e64d..6941e66e 100644 --- a/headphones/cache.py +++ b/headphones/cache.py @@ -110,6 +110,22 @@ class Cache(object): return True else: return False + + def _get_thumb_url(self, data): + + thumb_url = None + + try: + images = data[self.id_type]['image'] + except KeyError: + return None + + for image in images: + if image['size'] == 'medium': + thumb_url = image['#text'] + break + + return thumb_url def get_artwork_from_cache(self, ArtistID=None, AlbumID=None): ''' @@ -223,18 +239,22 @@ class Cache(object): try: info = data['artist']['bio']['summary'] except KeyError: - logger.debug('No artist bio found on url: ' + url) + logger.debug('No artist bio summary found on url: ' + url) info = None + try: + info_full = data['artist']['bio']['content'] + except KeyError: + logger.debug('No artist bio found on url: ' + url) + info_full = None try: image_url = data['artist']['image'][-1]['#text'] except KeyError: logger.debug('No artist image found on url: ' + url) image_url = None - try: - thumb_url = data['artist']['image'][-3]['#text'] # - except KeyError: + + thumb_url = self._get_thumb_url(data) + if not thumb_url: logger.debug('No artist thumbnail image found on url: ' + url) - thumb_url = None else: myDB = db.DBConnection() @@ -265,20 +285,35 @@ class Cache(object): try: info = data['album']['wiki']['summary'] except KeyError: - logger.debug('No album infomation found from: ' + url) + logger.debug('No album summary found from: ' + url) info = None + try: + info_full = data['album']['wiki']['content'] + except KeyError: + logger.debug('No album infomation found from: ' + url) + info_full = None try: image_url = data['album']['image'][-1]['#text'] except KeyError: logger.debug('No album image link found on url: ' + url) image_url = None - try: - thumb_url = data['album']['image'][-3]['#text'] - except KeyError: - logger.debug('No album thumbnail image link found on url: ' + url) - thumb_url = None + + thumb_url = self._get_thumb_url(data) + if not thumb_url: + logger.debug('No album thumbnail image found on url: ' + url) + + #Save the content & summary to the database no matter what if we've opened up the url + if info or info_full: + + myDB = db.DBConnection() + + if self.id_type == 'artist': + myDB.action('UPDATE descriptions SET Summary=?, Content=? WHERE ArtistID=?', [info, info_full, self.id]) + else: + myDB.action('UPDATE descriptions SET Summary=?, Content=? WHERE ReleaseGroupID=?', [info, info_full, self.id]) - # Save the info no matter what the query type if it's outdated/missing + # Save the info no matter what the query type if it's outdated/missing (maybe it's redundant to save + # the info files since we're already saving them to the database??? Especially since the DB has summary & content if info and not (self.info_files and self._is_current(self.info_files[0])): # Make sure the info dir exists: @@ -311,6 +346,15 @@ class Cache(object): # just so it doesn't check it every time elif not info and not (self.info_files and self._is_current(self.info_files[0])): + # Make sure the info dir exists: + if not os.path.isdir(self.path_to_info_cache): + try: + os.makedirs(self.path_to_info_cache) + except Exception, e: + logger.error('Unable to create info cache dir. Error: ' + str(e)) + self.info_errors = True + self.info = info + new_info_file_path = os.path.join(self.path_to_info_cache, self.id + '.' + helpers.today() + '.txt') if len(self.info_files) == 1: diff --git a/headphones/importer.py b/headphones/importer.py index 400fc303..fa541970 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -82,6 +82,9 @@ def addArtistIDListToDB(artistidlist): def addArtisttoDB(artistid, extrasonly=False): + # Putting this here to get around the circular import + 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.') @@ -179,11 +182,6 @@ def addArtisttoDB(artistid, extrasonly=False): 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) # I changed the albumid from releaseid -> rgid, so might need to delete albums that have a releaseid for release in release_dict['releaselist']: @@ -220,6 +218,9 @@ def addArtisttoDB(artistid, extrasonly=False): myDB.upsert("tracks", newValueDict, controlValueDict) + logger.info(u"Updating album cache for " + rg['title']) + cache.getThumb(AlbumID=rg['id']) + latestalbum = myDB.action('SELECT AlbumTitle, ReleaseDate, AlbumID from albums WHERE ArtistID=? order by ReleaseDate DESC', [artistid]).fetchone() totaltracks = len(myDB.select('SELECT TrackTitle from tracks WHERE ArtistID=?', [artistid])) havetracks = len(myDB.select('SELECT TrackTitle from tracks WHERE ArtistID=? AND Location IS NOT NULL', [artistid])) + len(myDB.select('SELECT TrackTitle from have WHERE ArtistName like ?', [artist['artist_name']])) @@ -241,6 +242,10 @@ def addArtisttoDB(artistid, extrasonly=False): newValueDict['LastUpdated'] = helpers.now() myDB.upsert("artists", newValueDict, controlValueDict) + + logger.info(u"Updating cache for: " + artist['artist_name']) + cache.getThumb(ArtistID=artistid) + logger.info(u"Updating complete for: " + artist['artist_name']) def addReleaseById(rid):