Skip to content

Commit

Permalink
Keep trying when creating session
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair committed Oct 20, 2010
1 parent 31a5a1b commit 8296dd4
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lastfm.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ def _do_raw_lastfm_query(url):
try:
f = urllib2.urlopen(f)
except urllib2.URLError, e:
print e
raise

tree = xml.etree.ElementTree.ElementTree(file=f)
result=_etree_to_dict(tree.getroot())
print result
return result

def _do_lastfm_post(url, data):
Expand All @@ -64,7 +62,6 @@ def _do_lastfm_post(url, data):
try:
f = urllib2.urlopen(f, data)
except urllib2.URLError, e:
print e
raise


Expand All @@ -77,7 +74,6 @@ def _do_lastfm_query(type, method,**kwargs):
args[k] = v.encode("utf8")
s = ""
for k in sorted(args.keys()):
print k,args[k]
s+=k+args[k]
s+=secret
if "sk" in args.keys() or "token" in args.keys():
Expand All @@ -90,7 +86,6 @@ def _do_lastfm_query(type, method,**kwargs):
'',
urllib.urlencode(args),
''))
print url
return _do_raw_lastfm_query(url)
elif type == "POST":
url=urlparse.urlunparse(('http',
Expand All @@ -108,6 +103,7 @@ def _make_session(token):
fp = open(os.path.expanduser("~/.lastfmsess"), "w")
fp.write(key)
fp.close()
print "session successfully created. thank you."

def scrobble(artist, track):
# OK, not the real start time. but that'll do
Expand All @@ -118,9 +114,15 @@ def scrobble(artist, track):
if len(sys.argv) > 1:
if sys.argv[1] == "auth":
t = _get_auth_token()
print "http:https://www.last.fm/api/auth/?api_key=%s&token=%s" % (key, t)
time.sleep(10)
_make_session(t)
print "Please open this url then come back to this window: http:https://www.last.fm/api/auth/?api_key=%s&token=%s" % (key, t)
i = 0
while i < 5:
try:
_make_session(t)
break
except urllib2.HTTPError ,e:
print e.code
time.sleep(10)
else:
scrobble("The New Pornographers", "Failsafe")

0 comments on commit 8296dd4

Please sign in to comment.