Skip to content

Commit

Permalink
Merge pull request #22 from Gyarbij/dev Refactored code and new pytho…
Browse files Browse the repository at this point in the history
…n 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.
  • Loading branch information
Gyarbij authored Mar 18, 2023
2 parents 3868d9f + 2946bcd commit 5dd1fba
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Base image: https://hub.docker.com/_/python
FROM python:3.11.2-slim
FROM python:3.12-rc-slim

# Prevents Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ docker run -d \

### Docker Compose

docker-compose.yml should be configured per the below.
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.

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

Expand Down
122 changes: 67 additions & 55 deletions plexist/plexist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/usr/bin/env python3

import logging
import os
Expand All @@ -13,75 +13,87 @@
from modules.helperClasses import UserInputs
from modules.spotify import spotify_playlist_sync

# Read ENV variables
userInputs = UserInputs(
plex_url=os.getenv("PLEX_URL"),
plex_token=os.getenv("PLEX_TOKEN"),
write_missing_as_csv=os.getenv("WRITE_MISSING_AS_CSV", "0") == "1",
add_playlist_poster=os.getenv("ADD_PLAYLIST_POSTER", "1") == "1",
add_playlist_description=os.getenv("ADD_PLAYLIST_DESCRIPTION", "1") == "1",
append_instead_of_sync=os.getenv("APPEND_INSTEAD_OF_SYNC", False) == "1",
wait_seconds=int(os.getenv("SECONDS_TO_WAIT", 86400)),
spotipy_client_id=os.getenv("SPOTIFY_CLIENT_ID"),
spotipy_client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"),
spotify_user_id=os.getenv("SPOTIFY_USER_ID"),
deezer_user_id=os.getenv("DEEZER_USER_ID"),
deezer_playlist_ids=os.getenv("DEEZER_PLAYLIST_ID"),
)
while True:
logging.info("Starting playlist sync")

if userInputs.plex_url and userInputs.plex_token:

def read_environment_variables():
return UserInputs(
plex_url=os.getenv("PLEX_URL"),
plex_token=os.getenv("PLEX_TOKEN"),
write_missing_as_csv=os.getenv("WRITE_MISSING_AS_CSV", "0") == "1",
add_playlist_poster=os.getenv("ADD_PLAYLIST_POSTER", "1") == "1",
add_playlist_description=os.getenv("ADD_PLAYLIST_DESCRIPTION", "1") == "1",
append_instead_of_sync=os.getenv("APPEND_INSTEAD_OF_SYNC", "False") == "1",
wait_seconds=int(os.getenv("SECONDS_TO_WAIT", 86400)),
spotipy_client_id=os.getenv("SPOTIFY_CLIENT_ID"),
spotipy_client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"),
spotify_user_id=os.getenv("SPOTIFY_USER_ID"),
deezer_user_id=os.getenv("DEEZER_USER_ID"),
deezer_playlist_ids=os.getenv("DEEZER_PLAYLIST_ID"),
)


def initialize_plex_server(user_inputs):
if user_inputs.plex_url and user_inputs.plex_token:
try:
plex = PlexServer(userInputs.plex_url, userInputs.plex_token)
except:
logging.error("Plex Authorization error")
break
return PlexServer(user_inputs.plex_url, user_inputs.plex_token)
except Exception as e:
logging.error(f"Plex Authorization error: {e}")
return None
else:
logging.error("Missing Plex Authorization Variables")
break

########## SPOTIFY SYNC ##########
return None

logging.info("Starting Spotify playlist sync")

SP_AUTHSUCCESS = False

def initialize_spotify_client(user_inputs):
if (
userInputs.spotipy_client_id
and userInputs.spotipy_client_secret
and userInputs.spotify_user_id
user_inputs.spotipy_client_id
and user_inputs.spotipy_client_secret
and user_inputs.spotify_user_id
):
try:
sp = spotipy.Spotify(
return spotipy.Spotify(
auth_manager=SpotifyClientCredentials(
userInputs.spotipy_client_id,
userInputs.spotipy_client_secret,
user_inputs.spotipy_client_id,
user_inputs.spotipy_client_secret,
)
)
SP_AUTHSUCCESS = True
except:
logging.info("Spotify Authorization error, skipping spotify sync")

except Exception as e:
logging.error(f"Spotify Authorization error: {e}")
return None
else:
logging.info(
"Missing one or more Spotify Authorization Variables, skipping"
" spotify sync"
)
logging.error("Missing one or more Spotify Authorization Variables")
return None


def main():
user_inputs = read_environment_variables()
plex = initialize_plex_server(user_inputs)

if plex is None:
return

while True:
logging.info("Starting playlist sync")

if SP_AUTHSUCCESS:
spotify_playlist_sync(sp, plex, userInputs)
# Spotify sync
logging.info("Starting Spotify playlist sync")
sp = initialize_spotify_client(user_inputs)
if sp is not None:
spotify_playlist_sync(sp, plex, user_inputs)
logging.info("Spotify playlist sync complete")
else:
logging.error("Spotify sync skipped due to authorization error")

logging.info("Spotify playlist sync complete")
# Deezer sync
logging.info("Starting Deezer playlist sync")
dz = deezer.Client()
deezer_playlist_sync(dz, plex, user_inputs)
logging.info("Deezer playlist sync complete")

########## DEEZER SYNC ##########
logging.info("All playlist(s) sync complete")
logging.info(f"Sleeping for {user_inputs.wait_seconds} seconds")

logging.info("Starting Deezer playlist sync")
dz = deezer.Client()
deezer_playlist_sync(dz, plex, userInputs)
logging.info("Deezer playlist sync complete")
time.sleep(user_inputs.wait_seconds)

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

time.sleep(userInputs.wait_seconds)
if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spotipy>=2.22
plexapi>=4.13.1
spotipy>=2.22.1
plexapi>=4.13.4
deezer-python>=5.8.1

0 comments on commit 5dd1fba

Please sign in to comment.