Skip to content

Commit 5dd1fba

Browse files
authored
Merge pull request #22 from Gyarbij/dev Refactored code and new python base
1. Updated it to use env python3. 2. Refactored the code into separate functions for better readability and maintainability. 3. Used f-strings for better readability in logging messages. 4. Added exception handling to the Plex and Spotify initialization functions to provide more informative error messages. 5. Upgraded python base to 3.12-rc-slim 6. Updated dockerfile. 7. Updated README.
2 parents 3868d9f + 2946bcd commit 5dd1fba

File tree

4 files changed

+71
-59
lines changed

4 files changed

+71
-59
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Base image: https://hub.docker.com/_/python
2-
FROM python:3.11.2-slim
2+
FROM python:3.12-rc-slim
33

44
# Prevents Python from generating .pyc files in the container
55
ENV PYTHONDONTWRITEBYTECODE=1

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ docker run -d \
8181

8282
### Docker Compose
8383

84-
docker-compose.yml should be configured per the below.
84+
docker-compose.yml should be configured per the below, if you don't user Spotify you can remove the Spotify variables and vice versa for Deezer.
8585

8686
A template is Here: [docker-compose.yml](https://github.com/gyarbij/plexist/blob/main/assets/docker-compose.yml)
8787

plexist/plexist.py

+67-55
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env python3
22

33
import logging
44
import os
@@ -13,75 +13,87 @@
1313
from modules.helperClasses import UserInputs
1414
from modules.spotify import spotify_playlist_sync
1515

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:
3536
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
4041
else:
4142
logging.error("Missing Plex Authorization Variables")
42-
break
43-
44-
########## SPOTIFY SYNC ##########
43+
return None
4544

46-
logging.info("Starting Spotify playlist sync")
47-
48-
SP_AUTHSUCCESS = False
4945

46+
def initialize_spotify_client(user_inputs):
5047
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
5451
):
5552
try:
56-
sp = spotipy.Spotify(
53+
return spotipy.Spotify(
5754
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,
6057
)
6158
)
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
6662
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")
7176

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")
7485

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")
7691

77-
########## DEEZER SYNC ##########
92+
logging.info("All playlist(s) sync complete")
93+
logging.info(f"Sleeping for {user_inputs.wait_seconds} seconds")
7894

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)
8396

84-
logging.info("All playlist(s) sync complete")
85-
logging.info("sleeping for %s seconds" % userInputs.wait_seconds)
8697

87-
time.sleep(userInputs.wait_seconds)
98+
if __name__ == "__main__":
99+
main()

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
spotipy>=2.22
2-
plexapi>=4.13.1
1+
spotipy>=2.22.1
2+
plexapi>=4.13.4
33
deezer-python>=5.8.1

0 commit comments

Comments
 (0)