-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathget_playlists.py
78 lines (70 loc) · 2.38 KB
/
get_playlists.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
import argparse
import os
from time import sleep
import pandas as pd
import spotipy
from tqdm import tqdm
from utils import get_access_token, paginate
if __name__ == "__main__":
"""
Entry point for the get_playlists script.
Parses command-line arguments and retrieves playlists for a list of Spotify users.
Args:
--limit (int): Limit number of playlists to retrieve. Default is None (retrieve all playlists).
--playlists_file (str): Path to the output CSV file where playlists will be saved. Default is "data/playlists.csv".
--start_from (int): Index of the first user from which to retrieve playlists for. Default is 0.
--users_file (str): Path to the user IDs CSV file to retrieve playlists for. Default is "data/users.csv".
Returns:
None
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--limit", type=int, default=None, help="Limit number of playlists"
)
parser.add_argument(
"--playlists_file",
type=str,
default="data/playlists.csv",
help="Output playlists CSV file",
)
parser.add_argument(
"--start_from",
type=int,
default=0,
help="Start from user index",
)
parser.add_argument(
"--users_file",
type=str,
default="data/users.csv",
help="Input users CSV file",
)
args = parser.parse_args()
users = pd.read_csv(args.users_file, header=None, index_col=None)[0]
playlists = (
set(pd.read_csv(args.playlists_file, header=None, index_col=None)[0])
if os.path.exists(args.playlists_file)
else set()
)
for user in tqdm(
users[args.start_from :],
initial=args.start_from,
total=len(users),
desc="Getting playlists",
):
try:
access_token = get_access_token()
sp = spotipy.Spotify(access_token)
playlists |= set(
playlist["id"] for playlist in paginate(sp.user_playlists, user=user)
)
except KeyboardInterrupt:
break
except Exception:
print(f"Skipping {user}")
sleep(1)
if args.limit is not None and len(playlists) >= args.limit:
break
print(len(playlists))
print(f"Writing {args.playlists_file}")
pd.DataFrame(playlists).to_csv(args.playlists_file, index=False, header=False)