-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlastfmhandler.py
93 lines (79 loc) · 2.96 KB
/
lastfmhandler.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
import requests
import urllib.parse
import confighandler
apikey = confighandler.get_config()["apikey"]
def get_track_playcount(user: str, track: dict) -> int:
"""
Returns the playcount of a track
:param user: The user's last.fm username
:type user: str
:param track: The track to look track playcount for
:return: The playcount of the track
:rtype: int
"""
r = requests.get(
f"https://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={apikey}&artist={track['artist']['#text']}&track={urllib.parse.quote(track['name'].lower())}&format=json&username={user}").json()
return r['track']['userplaycount']
# from track
def get_album_playcount(user: str, track: dict) -> int:
"""
Returns the playcount of an album
:param user: The user's last.fm username
:type user: str
:param track: The track to look album playcount for
:type track: dict
:return: The playcount of the album
:rtype: int
"""
r = requests.get(
f"https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key={apikey}&artist={track['artist']['#text']}&album={track['album']['#text']}&format=json&username={user}").json()
return r['album']['userplaycount']
# from track
def get_artist_playcount(user: str, track: dict) -> int:
"""
Returns the playcount of an artist
:param user: The user's last.fm username
:type user: str
:param track: The track to look artist playcount for
:type track: dict
:return: The playcount of the artist
:rtype: int
"""
r = requests.get(
f"https://ws.audioscrobbler.com/2.0/?method=artist.getInfo&api_key={apikey}&artist={track['artist']['#text']}&format=json&username={user}").json()
return r['artist']['stats']['userplaycount']
def get_tracks_recent(user: str, count: int) -> dict:
"""
Returns the user's most recent tracks
:param user: The user's last.fm username
:type user: str
:param count: Numbre of tracks to fetch for
:type: count: int
:return: The user's most recent tracks
:rtype: dict
"""
return requests.get(
f"https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={user}&api_key={apikey}&format=json&limit={count}").json()
# from track
def get_album(track: dict) -> dict:
"""
Returns the album of a track
:param track: The track to look album for
:type track: dict
:return: The album of the track
:rtype: dict
"""
r = requests.get(
f"https://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key={apikey}&artist={track['artist']['#text']}&album={track['album']['#text']}&format=json").json()
return r['album']
def get_user_info(user: str) -> dict:
"""
Returns the user's info
:param user: The user's last.fm username
:type user: str
:return: The user's info
:rtype: dict
"""
r = requests.get(
f"https://ws.audioscrobbler.com/2.0/?method=user.getinfo&user={user}&api_key={apikey}&format=json").json()
return r