Skip to content

Commit 5929adb

Browse files
committed
Merge pull request #22 from cmathews393/21-periodic-sync-for-docker
21 periodic sync for docker
1 parent 4f1c93f commit 5929adb

File tree

3 files changed

+95
-28
lines changed

3 files changed

+95
-28
lines changed

.github/workflows/docker-image.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Docker Push On Commit
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
- test
9+
10+
jobs:
11+
build-and-push:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
17+
- name: Log in to DockerHub
18+
uses: docker/login-action@v1
19+
with:
20+
username: ${{ secrets.DOCKER_HUB_USERNAME }}
21+
password: ${{ secrets.DOCKER_HUB_APP_PASS }}
22+
23+
- name: Set up Docker Buildx
24+
uses: docker/setup-buildx-action@v1
25+
26+
- name: Build and push
27+
uses: docker/build-push-action@v2
28+
with:
29+
context: .
30+
file: ./Dockerfile
31+
push: true
32+
tags: |
33+
0xchloe/spotiplex:${{ github.ref_name == 'main' && 'latest' || github.ref_name }}
34+

spotiplex/main.py

+48-25
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ def __init__(self):
2121
"plex_users": os.environ.get("USERS", ""),
2222
"worker_count": int(os.environ.get("WORKERS", 1)),
2323
"seconds_interval": int(os.environ.get("INTERVAL", 86400)),
24-
"manual_playlists": os.environ.get(
25-
"SPOTIPLEX_MANUAL_PLAYLISTS", "False"
26-
),
24+
"manual_sync": os.environ.get("SPOTIPLEX_MANUAL_PLAYLISTS", "False"),
2725
}
2826
write_config("spotiplex", spotiplex_config)
2927

@@ -63,10 +61,20 @@ def __init__(self):
6361
if self.lidarr_sync == "true":
6462
self.sync_lists = self.lidarr_api.get_lidarr_playlists()
6563
else:
66-
self.sync_lists = self.config.get("manual_playlists")
64+
self.sync_lists = []
65+
manual_playlists = self.config.get("manual_playlists", "")
66+
if manual_playlists:
67+
# If manual_playlists is a string of items separated by commas, split it into a list
68+
manual_playlists_list = manual_playlists.split(",")
69+
# Then extend self.sync_lists with this list
70+
self.sync_lists.extend(manual_playlists_list)
71+
print(manual_playlists)
72+
self.sync_my_user = False
6773
currentuser = self.plex_service.plex.myPlexAccount().username.lower()
74+
6875
if currentuser in self.user_list:
6976
self.user_list.remove(currentuser)
77+
self.sync_my_user = True
7078

7179
def process_for_user(self, user):
7280
if user:
@@ -92,9 +100,24 @@ def process_for_user(self, user):
92100
print(f"Thread resulted in an error: {e}")
93101

94102
def is_running_in_docker():
95-
return os.path.exists("/.dockerenv")
103+
# Check for the .dockerenv file
104+
if os.path.exists("/.dockerenv"):
105+
return True
106+
107+
# Check for container-related entries in /proc/1/cgroup
108+
try:
109+
with open("/proc/1/cgroup", "rt") as f:
110+
if any("docker" in line or "containerd" in line for line in f):
111+
return True
112+
except Exception:
113+
pass
114+
115+
return False
96116

97117
def run(self):
118+
self.plex_service = PlexService()
119+
if self.sync_my_user:
120+
self.process_for_user(None)
98121
for user in self.user_list:
99122
self.process_for_user(user)
100123
if self.seconds_interval > 0:
@@ -103,32 +126,32 @@ def run(self):
103126
schedule.run_pending()
104127
time.sleep(1)
105128

106-
def extract_playlist_id(playlist_url): # parse playlist ID from URL if applicable
129+
def extract_playlist_id(playlist_url):
130+
# Check and extract the part after "?si="
107131
if "?si=" in playlist_url:
108132
playlist_url = playlist_url.split("?si=")[0]
109133

110-
return (
111-
playlist_url.split("playlist/")[1]
112-
if "playlist/" in playlist_url
113-
else playlist_url
114-
)
134+
# Check and extract the part after "playlist/"
135+
if "playlist/" in playlist_url:
136+
playlist_url = playlist_url.split("playlist/")[1]
137+
138+
return playlist_url
115139

116140
def process_playlist(
117-
self, playlists, plex_service, spotify_service, replace_existing
141+
self, playlist, plex_service, spotify_service, replace_existing
118142
):
119-
for playlist in playlists:
120-
try:
121-
playlist_id = Spotiplex.extract_playlist_id(playlist)
122-
print(playlist_id)
123-
playlist_name = spotify_service.get_playlist_name(playlist_id)
124-
spotify_tracks = spotify_service.get_playlist_tracks(playlist_id)
125-
plex_tracks = plex_service.check_tracks_in_plex(spotify_tracks)
126-
plex_service.create_or_update_playlist(
127-
playlist_name, playlist_id, plex_tracks
128-
)
129-
print(f"Processed playlist '{playlist_name}'.")
130-
except Exception as e:
131-
print(f"Error processing playlist '{playlist}':", e)
143+
try:
144+
playlist_id = Spotiplex.extract_playlist_id(playlist)
145+
print(playlist_id)
146+
playlist_name = spotify_service.get_playlist_name(playlist_id)
147+
spotify_tracks = spotify_service.get_playlist_tracks(playlist_id)
148+
plex_tracks = plex_service.check_tracks_in_plex(spotify_tracks)
149+
plex_service.create_or_update_playlist(
150+
playlist_name, playlist_id, plex_tracks
151+
)
152+
print(f"Processed playlist '{playlist_name}'.")
153+
except Exception as e:
154+
print(f"Error processing playlist '{playlist}':", e)
132155

133156
def configurator(self):
134157
# Config for Spotiplex

spotiplex/plex.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ def check_tracks_in_plex(self, spotify_tracks):
4040

4141
return plex_tracks
4242

43-
def create_or_update_playlist(
44-
self, playlist_name, playlist_id, tracks
45-
):
43+
def create_or_update_playlist(self, playlist_name, playlist_id, tracks):
4644
existing_playlist = self.find_playlist_by_name(playlist_name)
4745
if existing_playlist:
4846
if self.replace:
@@ -68,3 +66,15 @@ def create_playlist(self, playlist_name, playlist_id, tracks):
6866
except Exception as e:
6967
print(f"Error creating playlist {playlist_name}: {e}")
7068
return None
69+
70+
def list_active_streams(self):
71+
"""Lists all users currently playing media and the media they are playing."""
72+
sessions = self.plex.sessions()
73+
if sessions:
74+
for session in sessions:
75+
user = session.usernames[0] if session.usernames else "Unknown User"
76+
title = session.title
77+
# Depending on your requirement, you might also want to include session.type (movie, episode, etc.)
78+
print(f"User '{user}' is playing '{title}'")
79+
else:
80+
print("No active streams.")

0 commit comments

Comments
 (0)