-
-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathaudioscrobbler_legacy.py
More file actions
127 lines (109 loc) · 3.52 KB
/
Copy pathaudioscrobbler_legacy.py
File metadata and controls
127 lines (109 loc) · 3.52 KB
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
from ._base import APIHandler
from ._exceptions import *
from .. import database
from ._apikeys import apikeystore
from bottle import request
class AudioscrobblerLegacy(APIHandler):
__apiname__ = "Legacy Audioscrobbler"
__doclink__ = "https://web.archive.org/web/20190531021725/https://www.last.fm/api/submissions"
__aliases__ = [
"audioscrobbler_legacy",
"audioscrobbler/1.2"
]
def init(self):
# no need to save these on disk, clients can always request a new session
self.mobile_sessions = {}
self.methods = {
"handshake":self.handshake,
"nowplaying":self.now_playing,
"scrobble":self.submit_scrobble
}
self.errors = {
BadAuthException: (403, "BADAUTH\n"),
InvalidAuthException: (403, "BADAUTH\n"),
InvalidMethodException: (400, "FAILED\n"),
InvalidSessionKey: (403, "BADSESSION\n"),
Exception: (500, "FAILED\n")
}
def get_method(self,pathnodes,keys):
if keys.get("hs") == 'true': return 'handshake'
else: return pathnodes[0]
def handshake(self,pathnodes,keys):
auth = keys.get("a")
timestamp = keys.get("t")
apikey = keys.get("api_key")
host = keys.get("Host")
protocol = 'http' if (keys.get("u") == 'nossl') else request.urlparts.scheme
if auth is not None:
for client in apikeystore:
key = apikeystore[client]
if self.check_token(auth,key,timestamp):
sessionkey = self.generate_key(client)
if ':' not in host:
port = 80 if protocol == 'http' else 443
host = f"{host}:{port}"
return 200, (
"OK\n"
f"{sessionkey}\n"
f"{protocol}://{host}/apis/audioscrobbler_legacy/nowplaying\n"
f"{protocol}://{host}/apis/audioscrobbler_legacy/scrobble\n"
)
else:
raise InvalidAuthException()
else:
raise BadAuthException()
def now_playing(self,pathnodes,keys):
# I see no implementation in the other compatible APIs, so I have just
# created a route that always says it was successful except if the
# session is invalid
if keys.get("s") is None or keys.get("s") not in self.mobile_sessions:
raise InvalidSessionKey()
else:
return 200,"OK\n"
def submit_scrobble(self,pathnodes,keys):
key = keys.get("s")
if key is None or key not in self.mobile_sessions:
raise InvalidSessionKey()
client = self.mobile_sessions.get(key)
for count in range(50):
artist_key = f"a[{count}]"
album_key = f"b[{count}]"
length_key = f"l[{count}]"
track_key = f"t[{count}]"
time_key = f"i[{count}]"
if artist_key not in keys or track_key not in keys:
return 200,"OK\n"
artiststr,titlestr = keys[artist_key], keys[track_key]
try:
timestamp = int(keys[time_key])
except Exception:
timestamp = None
scrobble = {
'track_artists':[artiststr],
'track_title':titlestr,
'scrobble_time':timestamp,
}
if album_key in keys:
scrobble['album_name'] = keys[album_key]
if length_key in keys:
scrobble['track_length'] = keys[length_key]
#database.createScrobble(artists,title,timestamp)
self.scrobble(scrobble, client=client)
return 200,"OK\n"
def check_token(self, received_token, expected_key, ts):
expected_token = md5(md5(expected_key) + ts)
return received_token == expected_token
def generate_key(self,client):
key = "".join(
str(
random.choice(
list(range(10)) + list("abcdefghijklmnopqrstuvwxyz") +
list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) for _ in range(32))
self.mobile_sessions[key] = client
return key
import hashlib
import random
def md5(input):
m = hashlib.md5()
m.update(bytes(input,encoding="utf-8"))
return m.hexdigest()