-
Notifications
You must be signed in to change notification settings - Fork 239
/
lastfm.py
81 lines (56 loc) · 2.18 KB
/
lastfm.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
#!/usr/bin/python3
import requests
import random
class LastFM(object):
def __init__(
self,
api_key='a93da16045abb894cb7a4482255247bb',
api_secret='3d50bbcc22776479ca2f4bace507b21a'
):
self.__api_key = api_key
self.__api_secret = api_secret
def search(self, text):
_url = f'http://ws.audioscrobbler.com/2.0/?method=track.search&track={text}&api_key={self.__api_key}&format=json'
response = requests.post(_url).json()
data = response['results']['trackmatches']['track']
return data
def getInfo(self, data):
if len(data):
artist, track = data[0]['artist'], data[0]['name']
_url = f'http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={self.__api_key }&artist={artist}&track={track}&format=json'
response = requests.post(_url).json()
uri = random.randint(1000000000,10000000000)
uri = 's' + str(uri) + 't'
try:
info = {
'uri' : uri,
'name' : response['track']['name'],
'artist' : [response['track']['artist']['name']],
'album' : response['track']['album']['title'],
'image' : response['track']['album']['image'][-1]['#text'],
'duration_ms' : response['track']['duration']
}
return info
except:
try:
dur = 0
try: dur = data[0]['duration']
except:pass
info = {
'uri' : uri,
'name' : data[0]['name'],
'artist' : [data[0]['artist']],
'album' : data[0]['name'],
'image' : data[0]['image'][-1]['#text'],
'duration_ms' : dur
}
return info
except:
pass
return None
def get(self, text):
return self.getInfo(self.search(text))
if __name__ == '__main__':
last = LastFM()
data = last.get('TORVA - Soma')
print(data)