-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateSpotify.py
85 lines (70 loc) · 2.78 KB
/
updateSpotify.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
import threading
import time
import os
import subprocess
import playlistChecker
def createFolderIfNotExist(name):
if not os.path.exists(name):
os.makedirs(name)
print(f"Folder '{name}' created.")
return(True)
return(False)
def _runCommand( command, callback=print):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
for line in process.stdout:
callback(line.strip())
for line in process.stderr:
callback(line.strip())
class SpotifyDownloader:
def __init__(self, playlists):
self._urlPrefix = "https://open.spotify.com/playlist/"
self.playlists = self._preparePlaylists(playlists)
def _preparePlaylists(self, playlists):
playlistFolders = []
for playlist in playlists:
playlistID = playlist.removeprefix(self._urlPrefix).split("?si=")[0]
if createFolderIfNotExist(playlistID):
time.sleep(1)
print("Folder created")
self._createPlaylistConfig(playlistID) #< create config file for the playlist if it doesn't exist
playlistFolders.append(playlistID)
return(playlistFolders)
def _getListeners(self, listener=playlistChecker.PlaylistChangeListener):
listeners = {}
for id in self.playlists:
listeners[id] = listener(f"{self._urlPrefix}{id}")
return(listeners)
def listenForChange(self, interval=2):
listeners = self._getListeners()
while self.run:
try:
for id, listener in listeners.items():
if listener.isChange():
self.syncPlaylist(id)
time.sleep(interval)
except Exception as e:
print(f"Got error {e}, restarting in a few seconds")
time.sleep(5)
print("App stopped...")
def _createPlaylistConfig(self, id, callback=print):
originalDir = os.getcwd()
os.chdir(id)
_runCommand(f"python -m spotdl sync {self._urlPrefix}{id} --save-file conf.spotdl", callback=callback)
os.chdir(originalDir)
def syncPlaylist(self, id, callback=print):
originalDir = os.getcwd()
callback(f"updating playlist {id}")
os.chdir(id)
_runCommand("python -m spotdl sync conf.spotdl", callback=callback)
os.chdir(originalDir)
def syncPlaylists(self, callback=print):
for playlist in self.playlists:
self.syncPlaylist(playlist, callback=callback)
def start(self):
self.run = True
self.listenForChange()
self.startChangeListener()
def startChangeListener(self):
threading.Thread(target=self.listenForChange).start()
def stop(self):
self.run = False