forked from php1/lastfmproxy-2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudioscrobbler.py
134 lines (94 loc) · 3.74 KB
/
audioscrobbler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import httpclient
import time
import string
import md5
class audioscrobbler:
def __init__(self):
self.clientid = "lfp"
self.clientversion = "0.1"
self.handshakeurl = "post.audioscrobbler.com"
# Saved here in case we need to re-handshake later
self.username = None
self.password = None
self.version = None
self.session = None
self.nowplayingurl = None
self.submiturl = None
self.queue = []
self.debug = 0
def hexify(self, s):
result = ""
for c in s:
result = result + ("%02x" % ord(c))
return result
def urlencode(self, s):
result = ""
for c in s:
o = ord(c)
if (o >= ord('a') and o <= ord('z')) or (o >= ord('A') and o <= ord('Z')) or (o >= ord('0') and o <= ord('1')):
result = result + c
else:
result = result + ("%%%02x" % o)
return result
def handshake(self, username, password, version):
self.username = username
self.password = password
timestamp = str(int(time.time()))
auth = self.hexify(md5.md5(password).digest())
auth = self.hexify(md5.md5(auth + timestamp).digest())
req = "/?hs=true&p=1.2&c=" + self.clientid + "&v=" + self.clientversion + "&u=" + username + "&t=" + timestamp + "&a=" + auth
s = httpclient.httpclient(self.handshakeurl)
s.req(req)
reslines = string.split(s.response, "\n")
if self.debug:
print "audioscrobbler: handshake " + reslines[0]
if reslines[0] != "OK":
print "audioscrobbler: Handshake error:"
print repr(s.response)
return True
self.session = reslines[1]
self.nowplayingurl = reslines[2]
self.submiturl = reslines[3]
return False
def nowplaying(self, artist, track, album, length):
if not self.session:
return True
if self.debug:
print "audioscrobbler: now playing " + artist + " - " + track
artist = self.urlencode(artist)
track = self.urlencode(track)
album = self.urlencode(album)
req = "s=" + self.session + "&a=" + artist + "&t=" + track + "&b=" + album + "&l=" + str(length) + "&m="
s = httpclient.httpclient(self.nowplayingurl)
s.post(req)
reslines = string.split(s.response, "\n")
if reslines[0] != "OK":
print "audioscrobbler: Now playing error:"
print repr(s.response)
if reslines[0] == "BADSESSION":
self.session = None
return True
return False
def submit(self, artist, track, album, starttime, rating, length, key):
if not self.session:
return True
if self.debug:
print "audioscrobbler: submitting " + artist + " - " + track
artist = self.urlencode(artist)
track = self.urlencode(track)
album = self.urlencode(album)
req = "s=" + self.session + "&a[0]=" + artist + "&t[0]=" + track + "&b[0]=" + album + "&r[0]=" + rating + "&i[0]=" + str(starttime) + "&l[0]=" + str(length) + "&o[0]=L" + key + "&n[0]=&m[0]="
s = httpclient.httpclient(self.submiturl)
s.post(req)
reslines = string.split(s.response, "\n")
if reslines[0] != "OK":
print "audioscrobbler: Submission error:"
print repr(s.response)
if reslines[0] == "BADSESSION":
self.session = None
self.queue.append( (artist,track,album,starttime,rating,length,key) )
f = open("scrobblerqueue.txt", "w")
f.write(repr(self.queue))
f.close()
return True
return False