120 lines
3.0 KiB
Python
120 lines
3.0 KiB
Python
import urllib2
|
|
import pygtk
|
|
pygtk.require('2.0')
|
|
import gtk
|
|
import webbrowser
|
|
import thread
|
|
import hashlib
|
|
|
|
from button import Button
|
|
|
|
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 = "2c76f85a6704efd74b5a358821284ef9"
|
|
self.secret = "3a6012bfb627b60a884cf33fc044885c"
|
|
self.token = ""
|
|
|
|
# Run these in new thread so we can keep loading other things while waiting
|
|
self.makegui()
|
|
#thread.start_new_thread(self.setup,("setup",None))
|
|
|
|
self.show()
|
|
|
|
|
|
def makegui(self):
|
|
if self.data.lfm == "":
|
|
self.nosession = Button("lfm",None,"Click here to configure Ocarina for submitting songs to last.fm",self.setup)
|
|
self.nosession.set_size_request(100,100)
|
|
self.pack_start(self.nosession,False,False,0)
|
|
|
|
|
|
# One time setup (in theory)
|
|
def setup(self,widgit,data):
|
|
self.nosession.hide()
|
|
self.remove(self.nosession)
|
|
self.contBtn = Button("lfm",None,"Click to continue",self.fetchSession)
|
|
self.pack_start(self.contBtn,False,False,0)
|
|
self.fetchToken()
|
|
self.authorize()
|
|
|
|
|
|
# Fetch the authentication token
|
|
def fetchToken(self):
|
|
(url,list) = self.addMethod("auth.gettoken")
|
|
(url,list) = self.addParam(url,list,"api_key",self.key)
|
|
status = self.placeRequest(url)
|
|
attr = status.getAttributeNode("status")
|
|
if attr.value != "ok":
|
|
return
|
|
self.token += hashlib.md5(status.firstChild.nextSibling.firstChild.data).hexdigest()
|
|
|
|
|
|
# Open to authorization page
|
|
def authorize(self):
|
|
url = "http://www.last.fm/api/auth/?"
|
|
(url,list) = self.addParam(url,[],"api_key",self.key)
|
|
(url,list) = self.addParam(url,[],"token",self.token)
|
|
#url+="&"+self.token
|
|
webbrowser.open(url)
|
|
|
|
|
|
def fetchSession(self,widgit,data):
|
|
(url,list) = self.addMethod("auth.getSession")
|
|
(url,list) = self.addParam(url,list,"api_key",self.key)
|
|
(url,list) = self.addParam(url,list,"token",self.token)
|
|
sig = self.getSig(list)
|
|
(url,list) = self.addParam(url,None,"api_sig",sig)
|
|
print url
|
|
#status = self.placeRequest(url)
|
|
return
|
|
attr = status.getAttributeNode("status")
|
|
if attr.value != "ok":
|
|
return
|
|
node = status.firstChild.firstChild
|
|
user = node.data
|
|
key = node.nextSibling.data
|
|
subscriber = node.nextSibling.nextSibling.data
|
|
#print user,key,subscriber
|
|
|
|
|
|
def addMethod(self,method):
|
|
return (self.url+"?method="+method,[("method",method)])
|
|
|
|
|
|
def addParam(self,url,list,param,val):
|
|
url+="&"+param+"="+val
|
|
if list:
|
|
list += [(param,val)]
|
|
return url,list
|
|
#return (url+"&"+param+"="+val,list+[(param,val)])
|
|
|
|
|
|
def getSig(self,list):
|
|
list.sort()
|
|
string = ""
|
|
for entry in list:
|
|
string+=entry[0]+entry[1]
|
|
string = string + self.secret
|
|
m = hashlib.md5(string)
|
|
return m.hexdigest()
|
|
|
|
|
|
def sendReq(self,req):
|
|
req+=self.secret
|
|
#hash = md5.new(req).digest()
|
|
#return self.placeRequest(self.url+"?"+hash)
|
|
|
|
|
|
def placeRequest(self,url):
|
|
print url
|
|
req = urllib2.Request(url)
|
|
req.add_header('User-Agent','Ocarina')
|
|
return minidom.parse(urllib2.urlopen(req)).documentElement
|