-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_playlists.py
73 lines (62 loc) · 2 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
from os import mkdir
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from spotipy.oauth2 import SpotifyClientCredentials
from pathlib import Path
from config import *
import shutil
import os
import os.path
import re
SCOPE= 'playlist-read-collaborative'
REMOVE = False
EXISTING_PLAYLISTS = os.listdir(SYNC_FOLDER)
def authenticate():
'''
Authenticate Spotify api
'''
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
scope=SCOPE))
return sp
def get_playlists(sp, user=USER):
'''
fetch playlists names from Spotify
'''
playlists = {}
results = sp.user_playlists(user=user)
for entry in results["items"]:
playlist_name = re.sub(r"[^A-Za-z0-9]", "", entry["name"]) #remove wihtespaces and stuff
playlists[playlist_name] = entry["external_urls"]["spotify"]
return playlists
def sync_single_playlist(url, name):
'''
Use spodl to sync a single playlist
'''
command = f"spotdl sync {url} --save-file {name}.sync.spotdl"
print("run: ", command)
os.system(command)
def sync_playlists(playlists, sync_folder=SYNC_FOLDER):
'''
create playlist classes for each playlist
'''
for name, url in playlists.items():
os.chdir(SYNC_FOLDER)
Path(name).mkdir(parents=True, exist_ok=True)
os.chdir(os.path.join(SYNC_FOLDER, name))
sync_single_playlist(url, name)
#TODO: "Implement removing not wanted playlists"
def fetch_playlists_to_remove(playlists, existing_playlists=set(EXISTING_PLAYLISTS)):
playlist_names = set(playlists.keys())
playlists_to_remove = existing_playlists.difference(playlist_names)
return playlists_to_remove
def remove_deleted_playlists(playlists_to_remove):
for name in playlists_to_remove:
f = os.path.join(SYNC_FOLDER, name)
if os.path.isdir(f):
if input("Remove the playlist: ", name):
shutil.rmtree(f)
return