-
Notifications
You must be signed in to change notification settings - Fork 3
/
spet.py
67 lines (53 loc) · 1.72 KB
/
spet.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
import sys
import os
import argparse
import spotipy
import spotipy.util as util
from parser import parse
scope = "playlist-read-collaborative"
def login(username) -> str:
return util.prompt_for_user_token(
username,
scope,
client_id= os.getenv("SPOTIPY_CLIENT_ID"),
client_secret= os.getenv("SPOTIPY_CLIENT_SECRET"),
redirect_uri= os.getenv("SPOTIPY_REDIRECT_URI"),
)
def get_commands_playlists(token, username, playlist_id):
commands = []
sp = spotipy.Spotify(auth=token)
playlists = sp.user_playlists(username)
playlist = next((p for p in playlists["items"] if p["id"] == playlist_id), None)
if playlist:
result_list = sp.user_playlist(
username, playlist["id"], fields="tracks,next"
)
for i, item in enumerate(result_list["tracks"]["items"]):
track = item["track"]
commands.append(
{
"name": track["name"],
"artist": track["artists"][0]["name"],
"album": track["album"]["name"],
}
)
parse(commands)
else:
print("Playlist not found")
def get_playlists(token, username):
sp = spotipy.Spotify(auth=token)
playlists = sp.user_playlists(username)
for playlist in playlists["items"]:
print(f"id: {playlist.get('id')} name: {playlist.get('name')}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("username", help="spotify username")
parser.add_argument("--display-playlists", help="display playlist", action="store_true")
parser.add_argument("-p", "--playlist-id", help="playlist ID", type=str)
args = parser.parse_args()
if args.display_playlists:
get_playlists(login(args.username), args.username)
sys.exit()
if args.playlist_id:
get_commands_playlists(login(args.username), args.username, args.playlist_id)
sys.exit()