-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmusicAPI.py
51 lines (47 loc) · 1.7 KB
/
musicAPI.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
import random
import spotipy
from spotipy import oauth2
class spotify:
def __init__(self):
#Enter you delails
token = oauth2.SpotifyClientCredentials(client_id="Your client id",
client_secret="Your cliend secret")
cache_token = token.get_access_token(as_dict=False)
self.sp = spotipy.Spotify(cache_token)
def latest_albums(self):
"""Returns latest albums
Returns:
latest albums
"""
imp = [
'id',
'name',
'release_date',
'total_tracks',
'type',
'external_urls',
]
limit = 40
result = self.sp.new_releases('IN', limit)
albums = []
for i in result['albums']['items']:
albums.append({j: i[j] if j != 'external_urls' else i[j]['spotify'] for j in imp})
rand = random.randint(0, limit - 1)
return albums[rand]['external_urls']
def featured_playlists(self):
"""Return featured playlists
Returns:
featured playlists
"""
limit = 40
result = self.sp.featured_playlists(limit=limit)
playlists = result['playlists']
final_playlists = [] # Playlists with filtered data
for i in playlists['items']:
final_playlists.append({'desc': i['description'], 'external_urls': i['external_urls']['spotify']})
rand = random.randint(0, playlists['total'] - 1)
playlist = final_playlists[rand]
return f"{playlist['desc']}\n{playlist['external_urls']}"
if __name__ == "__main__":
sp = spotify()
print(sp.latest_albums())