From e2f0354deb8542835f33df0ac4a08b7b7249843d Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Wed, 13 Aug 2014 18:20:29 +0000 Subject: [PATCH 1/4] Raised Last.FM timeout to 60 seconds --- headphones/importer.py | 3 ++- headphones/lastfm.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/headphones/importer.py b/headphones/importer.py index a94bf35c..f8008b1a 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -98,9 +98,10 @@ def artistlist_to_mbids(artistlist, forced=False): # Update the similar artist tag cloud: logger.info('Updating artist information from Last.fm') + try: lastfm.getSimilar() - except Exception, e: + except Exception as e: logger.warn('Failed to update arist information from Last.fm: %s' % e) def addArtistIDListToDB(artistidlist): diff --git a/headphones/lastfm.py b/headphones/lastfm.py index 9f12601c..fdb456b3 100644 --- a/headphones/lastfm.py +++ b/headphones/lastfm.py @@ -21,8 +21,9 @@ from headphones import db, logger, request from collections import defaultdict -ENTRY_POINT = 'http://ws.audioscrobbler.com/2.0/' -API_KEY = '395e6ec6bb557382fc41fde867bce66f' +TIMEOUT = 60 # seconds +ENTRY_POINT = "http://ws.audioscrobbler.com/2.0/" +API_KEY = "395e6ec6bb557382fc41fde867bce66f" def request_lastfm(method, **kwargs): """ @@ -40,7 +41,7 @@ def request_lastfm(method, **kwargs): # Send request logger.debug("Calling Last.FM method: %s", method) - data = request.request_json(ENTRY_POINT, timeout=20, params=kwargs) + data = request.request_json(ENTRY_POINT, timeout=TIMEOUT, params=kwargs) # Parse response and check for errors. if not data: @@ -55,13 +56,13 @@ def request_lastfm(method, **kwargs): def getSimilar(): myDB = db.DBConnection() - results = myDB.select('SELECT ArtistID from artists ORDER BY HaveTracks DESC') + results = myDB.select("SELECT ArtistID from artists ORDER BY HaveTracks DESC") logger.info("Fetching similar artists from Last.FM for tag cloud") artistlist = [] for result in results[:12]: - data = request_lastfm("artist.getsimilar", mbid=result['ArtistId']) + data = request_lastfm("artist.getsimilar", mbid=result["ArtistId"]) time.sleep(10) if data and "similarartists" in data: @@ -94,13 +95,13 @@ def getSimilar(): artist_name, artist_mbid = item[0] count = item[1] - myDB.action('INSERT INTO lastfmcloud VALUES( ?, ?, ?)', [artist_name, artist_mbid, count]) + myDB.action("INSERT INTO lastfmcloud VALUES( ?, ?, ?)", [artist_name, artist_mbid, count]) logger.debug("Inserted %d artists into Last.FM tag cloud", len(top_list)) def getArtists(): myDB = db.DBConnection() - results = myDB.select('SELECT ArtistID from artists') + results = myDB.select("SELECT ArtistID from artists") if not headphones.LASTFM_USERNAME: logger.warn("Last.FM username not set, not importing artists.") @@ -129,7 +130,7 @@ def getArtists(): def getTagTopArtists(tag, limit=50): myDB = db.DBConnection() - results = myDB.select('SELECT ArtistID from artists') + results = myDB.select("SELECT ArtistID from artists") logger.info("Fetching top artists from Last.FM for tag: %s", tag) data = request_lastfm("tag.gettopartists", limit=limit, tag=tag) From 922cca96526e5cc90b649422a8825f0962675c04 Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Wed, 13 Aug 2014 18:21:51 +0000 Subject: [PATCH 2/4] Constants should be in caps --- headphones/cache.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/headphones/cache.py b/headphones/cache.py index 4eba6034..0194600e 100644 --- a/headphones/cache.py +++ b/headphones/cache.py @@ -20,7 +20,7 @@ import headphones from headphones import db, helpers, logger, lastfm, request -lastfm_apikey = "690e1ed3bc00bc91804cd8f7fe5ed6d4" +LASTFM_API_KEY = "690e1ed3bc00bc91804cd8f7fe5ed6d4" class Cache(object): """ @@ -59,7 +59,6 @@ class Cache(object): info_content = None def __init__(self): - pass def _findfilesstartingwith(self,pattern,folder): @@ -209,7 +208,7 @@ class Cache(object): if ArtistID: self.id_type = 'artist' - data = lastfm.request_lastfm("artist.getinfo", mbid=ArtistID, api_key=lastfm_apikey) + data = lastfm.request_lastfm("artist.getinfo", mbid=ArtistID, api_key=LASTFM_API_KEY) if not data: return @@ -227,7 +226,7 @@ class Cache(object): else: self.id_type = 'album' - data = lastfm.request_lastfm("album.getinfo", mbid=AlbumID, api_key=lastfm_apikey) + data = lastfm.request_lastfm("album.getinfo", mbid=AlbumID, api_key=LASTFM_API_KEY) if not data: return @@ -254,7 +253,7 @@ class Cache(object): # Since lastfm uses release ids rather than release group ids for albums, we have to do a artist + album search for albums if self.id_type == 'artist': - data = lastfm.request_lastfm("artist.getinfo", mbid=self.id, api_key=lastfm_apikey) + data = lastfm.request_lastfm("artist.getinfo", mbid=self.id, api_key=LASTFM_API_KEY) if not data: return @@ -282,7 +281,7 @@ class Cache(object): else: dbartist = myDB.action('SELECT ArtistName, AlbumTitle FROM albums WHERE AlbumID=?', [self.id]).fetchone() - data = lastfm.request_lastfm("album.getinfo", artist=dbartist['ArtistName'], album=dbartist['AlbumTitle'], api_key=lastfm_apikey) + data = lastfm.request_lastfm("album.getinfo", artist=dbartist['ArtistName'], album=dbartist['AlbumTitle'], api_key=LASTFM_API_KEY) if not data: return From 9178740c265f4692448c0680ad4c2842ab51ae84 Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Wed, 13 Aug 2014 18:23:55 +0000 Subject: [PATCH 3/4] Added license info to requests.py --- headphones/request.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/headphones/request.py b/headphones/request.py index 4e6b827f..756584ce 100644 --- a/headphones/request.py +++ b/headphones/request.py @@ -1,3 +1,18 @@ +# This file is part of Headphones. +# +# Headphones is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Headphones is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Headphones. If not, see . + from headphones import logger from xml.dom import minidom From 8c9b834bc4f34737620c8d3303b69b4d6955d836 Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Wed, 13 Aug 2014 18:31:08 +0000 Subject: [PATCH 4/4] Check for total number of tracks before continuing. Prevents ZeroDivisonError's --- headphones/importer.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/headphones/importer.py b/headphones/importer.py index f8008b1a..4bb3f9a7 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -432,9 +432,16 @@ def addArtisttoDB(artistid, extrasonly=False, forcefull=False): tracks = myDB.action('SELECT * from alltracks WHERE ReleaseID=?', [releaseid]).fetchall() - # This is used to see how many tracks you have from an album - to mark it as downloaded. Default is 80%, can be set in config as ALBUM_COMPLETION_PCT + # This is used to see how many tracks you have from an album - to + # mark it as downloaded. Default is 80%, can be set in config as + # ALBUM_COMPLETION_PCT total_track_count = len(tracks) + if total_track_count == 0: + logger.warning("Total track count is zero for Release ID " + + "'%s', skipping.", releaseid) + continue + for track in tracks: controlValueDict = {"TrackID": track['TrackID'],