Added timeouts to last.fm functions (urllib->urllib2), added some error catching when parsing data

This commit is contained in:
rembo10
2012-08-06 16:12:30 +05:30
parent 07a9ffe403
commit ed5d8b7459
+16 -6
View File
@@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with Headphones. If not, see <http://www.gnu.org/licenses/>. # along with Headphones. If not, see <http://www.gnu.org/licenses/>.
import urllib import urllib, urllib2
from xml.dom import minidom from xml.dom import minidom
from collections import defaultdict from collections import defaultdict
import random import random
@@ -37,7 +37,7 @@ def getSimilar():
url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&mbid=%s&api_key=%s' % (result['ArtistID'], api_key) url = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&mbid=%s&api_key=%s' % (result['ArtistID'], api_key)
try: try:
data = urllib.urlopen(url).read() data = urllib2.urlopen(url, timeout=20).read()
except: except:
time.sleep(1) time.sleep(1)
continue continue
@@ -45,7 +45,11 @@ def getSimilar():
if len(data) < 200: if len(data) < 200:
continue continue
d = minidom.parseString(data) try:
d = minidom.parseString(data)
except:
logger.debug("Could not parse similar artist data from last.fm")
node = d.documentElement node = d.documentElement
artists = d.getElementsByTagName("artist") artists = d.getElementsByTagName("artist")
@@ -93,8 +97,14 @@ def getArtists():
username = headphones.LASTFM_USERNAME username = headphones.LASTFM_USERNAME
url = 'http://ws.audioscrobbler.com/2.0/?method=library.getartists&limit=10000&api_key=%s&user=%s' % (api_key, username) url = 'http://ws.audioscrobbler.com/2.0/?method=library.getartists&limit=10000&api_key=%s&user=%s' % (api_key, username)
data = urllib.urlopen(url).read() data = urllib2.urlopen(url, timeout=20).read()
d = minidom.parseString(data)
try:
d = minidom.parseString(data)
except:
logger.error("Could not parse artist list from last.fm data")
return
artists = d.getElementsByTagName("artist") artists = d.getElementsByTagName("artist")
artistlist = [] artistlist = []
@@ -131,7 +141,7 @@ def getAlbumDescription(rgid, artist, album):
} }
searchURL = 'http://ws.audioscrobbler.com/2.0/?' + urllib.urlencode(params) searchURL = 'http://ws.audioscrobbler.com/2.0/?' + urllib.urlencode(params)
data = urllib.urlopen(searchURL).read() data = urllib2.urlopen(searchURL, timeout=20).read()
if data == '<?xml version="1.0" encoding="utf-8"?><lfm status="failed"><error code="6">Album not found</error></lfm>': if data == '<?xml version="1.0" encoding="utf-8"?><lfm status="failed"><error code="6">Album not found</error></lfm>':
return return