ocarina/trunk/src/GuiObjects/scrobbler.py

54 lines
1.2 KiB
Python

import urllib2
import pygtk
pygtk.require('2.0')
import gtk
import webbrowser
import thread
import xml.dom
from xml.dom import minidom
class Scrobbler(gtk.VBox):
def __init__(self,data):
gtk.VBox.__init__(self,False,0)
self.data = data
self.url = "http://ws.audioscrobbler.com/2.0/"
self.key = "api_key=2c76f85a6704efd74b5a358821284ef9"
self.token = "token="
# Run these in new thread so we can keep loading other things while waiting
thread.start_new_thread(self.setup,("setup",None))
#self.fetchToken()
#self.authorize()
#self.makeRequest()
self.show()
def setup(self,widgit,data):
self.fetchToken()
self.authorize()
def addMethod(self,method):
return self.url+"?method="+method
def authorize(self):
url = "http://www.last.fm/api/auth/?"
url+=self.key+"&"+self.token
webbrowser.open(url)
def fetchToken(self):
url = self.addMethod("auth.gettoken")+"&"+self.key
req = urllib2.Request(url)
req.add_header('User-Agent','Ocarina')
status = minidom.parse(urllib2.urlopen(req)).documentElement
attr = status.getAttributeNode("status")
if attr.value != "ok":
return
self.token += status.firstChild.nextSibling.firstChild.data
print self.token