1
- #! /bin/sh
1
+ #!/usr/ bin/env python3
2
2
3
3
import logging
4
4
import os
13
13
from modules .helperClasses import UserInputs
14
14
from modules .spotify import spotify_playlist_sync
15
15
16
- # Read ENV variables
17
- userInputs = UserInputs(
18
- plex_url=os.getenv(" PLEX_URL" ),
19
- plex_token=os.getenv(" PLEX_TOKEN" ),
20
- write_missing_as_csv=os.getenv(" WRITE_MISSING_AS_CSV" , " 0" ) == " 1" ,
21
- add_playlist_poster=os.getenv(" ADD_PLAYLIST_POSTER" , " 1" ) == " 1" ,
22
- add_playlist_description=os.getenv(" ADD_PLAYLIST_DESCRIPTION" , " 1" ) == " 1" ,
23
- append_instead_of_sync=os.getenv(" APPEND_INSTEAD_OF_SYNC" , False) == " 1" ,
24
- wait_seconds=int(os.getenv(" SECONDS_TO_WAIT" , 86400)),
25
- spotipy_client_id=os.getenv(" SPOTIFY_CLIENT_ID" ),
26
- spotipy_client_secret=os.getenv(" SPOTIFY_CLIENT_SECRET" ),
27
- spotify_user_id=os.getenv(" SPOTIFY_USER_ID" ),
28
- deezer_user_id=os.getenv(" DEEZER_USER_ID" ),
29
- deezer_playlist_ids=os.getenv(" DEEZER_PLAYLIST_ID" ),
30
- )
31
- while True:
32
- logging.info(" Starting playlist sync" )
33
-
34
- if userInputs.plex_url and userInputs.plex_token:
16
+
17
+ def read_environment_variables ():
18
+ return UserInputs (
19
+ plex_url = os .getenv ("PLEX_URL" ),
20
+ plex_token = os .getenv ("PLEX_TOKEN" ),
21
+ write_missing_as_csv = os .getenv ("WRITE_MISSING_AS_CSV" , "0" ) == "1" ,
22
+ add_playlist_poster = os .getenv ("ADD_PLAYLIST_POSTER" , "1" ) == "1" ,
23
+ add_playlist_description = os .getenv ("ADD_PLAYLIST_DESCRIPTION" , "1" ) == "1" ,
24
+ append_instead_of_sync = os .getenv ("APPEND_INSTEAD_OF_SYNC" , "False" ) == "1" ,
25
+ wait_seconds = int (os .getenv ("SECONDS_TO_WAIT" , 86400 )),
26
+ spotipy_client_id = os .getenv ("SPOTIFY_CLIENT_ID" ),
27
+ spotipy_client_secret = os .getenv ("SPOTIFY_CLIENT_SECRET" ),
28
+ spotify_user_id = os .getenv ("SPOTIFY_USER_ID" ),
29
+ deezer_user_id = os .getenv ("DEEZER_USER_ID" ),
30
+ deezer_playlist_ids = os .getenv ("DEEZER_PLAYLIST_ID" ),
31
+ )
32
+
33
+
34
+ def initialize_plex_server (user_inputs ):
35
+ if user_inputs .plex_url and user_inputs .plex_token :
35
36
try :
36
- plex = PlexServer(userInputs .plex_url, userInputs .plex_token)
37
- except:
38
- logging.error(" Plex Authorization error" )
39
- break
37
+ return PlexServer (user_inputs .plex_url , user_inputs .plex_token )
38
+ except Exception as e :
39
+ logging .error (f "Plex Authorization error: { e } " )
40
+ return None
40
41
else :
41
42
logging .error ("Missing Plex Authorization Variables" )
42
- break
43
-
44
- # ######### SPOTIFY SYNC ##########
43
+ return None
45
44
46
- logging.info(" Starting Spotify playlist sync" )
47
-
48
- SP_AUTHSUCCESS = False
49
45
46
+ def initialize_spotify_client (user_inputs ):
50
47
if (
51
- userInputs .spotipy_client_id
52
- and userInputs .spotipy_client_secret
53
- and userInputs .spotify_user_id
48
+ user_inputs .spotipy_client_id
49
+ and user_inputs .spotipy_client_secret
50
+ and user_inputs .spotify_user_id
54
51
):
55
52
try :
56
- sp = spotipy.Spotify(
53
+ return spotipy .Spotify (
57
54
auth_manager = SpotifyClientCredentials (
58
- userInputs .spotipy_client_id,
59
- userInputs .spotipy_client_secret,
55
+ user_inputs .spotipy_client_id ,
56
+ user_inputs .spotipy_client_secret ,
60
57
)
61
58
)
62
- SP_AUTHSUCCESS = True
63
- except:
64
- logging.info(" Spotify Authorization error, skipping spotify sync" )
65
-
59
+ except Exception as e :
60
+ logging .error (f"Spotify Authorization error: { e } " )
61
+ return None
66
62
else :
67
- logging.info(
68
- " Missing one or more Spotify Authorization Variables, skipping"
69
- " spotify sync"
70
- )
63
+ logging .error ("Missing one or more Spotify Authorization Variables" )
64
+ return None
65
+
66
+
67
+ def main ():
68
+ user_inputs = read_environment_variables ()
69
+ plex = initialize_plex_server (user_inputs )
70
+
71
+ if plex is None :
72
+ return
73
+
74
+ while True :
75
+ logging .info ("Starting playlist sync" )
71
76
72
- if SP_AUTHSUCCESS:
73
- spotify_playlist_sync(sp, plex, userInputs)
77
+ # Spotify sync
78
+ logging .info ("Starting Spotify playlist sync" )
79
+ sp = initialize_spotify_client (user_inputs )
80
+ if sp is not None :
81
+ spotify_playlist_sync (sp , plex , user_inputs )
82
+ logging .info ("Spotify playlist sync complete" )
83
+ else :
84
+ logging .error ("Spotify sync skipped due to authorization error" )
74
85
75
- logging.info(" Spotify playlist sync complete" )
86
+ # Deezer sync
87
+ logging .info ("Starting Deezer playlist sync" )
88
+ dz = deezer .Client ()
89
+ deezer_playlist_sync (dz , plex , user_inputs )
90
+ logging .info ("Deezer playlist sync complete" )
76
91
77
- # ######### DEEZER SYNC ##########
92
+ logging .info ("All playlist(s) sync complete" )
93
+ logging .info (f"Sleeping for { user_inputs .wait_seconds } seconds" )
78
94
79
- logging.info(" Starting Deezer playlist sync" )
80
- dz = deezer.Client ()
81
- deezer_playlist_sync(dz, plex, userInputs)
82
- logging.info(" Deezer playlist sync complete" )
95
+ time .sleep (user_inputs .wait_seconds )
83
96
84
- logging.info(" All playlist(s) sync complete" )
85
- logging.info(" sleeping for %s seconds" % userInputs.wait_seconds)
86
97
87
- time.sleep(userInputs.wait_seconds)
98
+ if __name__ == "__main__" :
99
+ main ()
0 commit comments