Skip to content

Commit ed3c9f1

Browse files
authored
Merge pull request #23 from Kyrluckechuck/main
Improve Large Playlist Handling, Handle Missing Songs
2 parents ffc868e + 5d75d14 commit ed3c9f1

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

spotiplex/main.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,25 @@ def __init__(self):
6363
if self.lidarr_sync == "true":
6464
self.sync_lists = self.lidarr_api.get_lidarr_playlists()
6565
else:
66+
# This should be an array of arrays to be run by multiple 'threads':
67+
# For example: [["playlist1"],["playlist2"],["playlist3","playlist4"]]
6668
self.sync_lists = self.config.get("manual_playlists")
67-
currentuser = self.plex_service.plex.myPlexAccount().username.lower()
68-
if currentuser in self.user_list:
69-
self.user_list.remove(currentuser)
69+
print(f"Attempting to run for {self.sync_lists}")
70+
self.default_user = self.plex_service.plex.myPlexAccount().username
71+
72+
# If the the user list provided is empty, add the default user from the token
73+
if not self.user_list or len(self.user_list) == 0:
74+
self.user_list.append(self.default_user)
7075

7176
def process_for_user(self, user):
72-
if user:
73-
self.plex_service.plex = self.plex_service.plex.switchUser(user)
77+
print(f"processing for user {user}")
78+
if user == self.default_user:
79+
self.plex_service.plex = self.plex_service.plex
7480
print(f"Processing playlists for user: {user}")
81+
print("User matches credentials provided, defaulting.")
82+
else:
83+
print(f"Attempting to switch to user {user}")
84+
self.plex_service.plex = self.plex_service.plex.switchUser(user)
7585

7686
with ThreadPoolExecutor(max_workers=self.worker_count) as executor:
7787
futures = [

spotiplex/plex.py

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import requests
22
import urllib3
33
from plexapi.server import PlexServer
4+
from plexapi.exceptions import BadRequest, NotFound
45
from confighandler import read_config
6+
import copy
57

68

79
class PlexService:
@@ -19,25 +21,43 @@ def connect_plex(self):
1921
return PlexServer(self.server_url, self.server_token, session=session)
2022

2123
def check_tracks_in_plex(self, spotify_tracks):
24+
print("Checking tracks in plex")
2225
music_lib = self.plex.library.section("Music")
2326
plex_tracks = []
2427
orig_tracks = []
2528

2629
for track_name, artist_name in spotify_tracks:
2730
artist_tracks_in_plex = music_lib.search(title=artist_name)
2831
if artist_tracks_in_plex:
29-
try:
30-
for track in artist_tracks_in_plex:
32+
for track in artist_tracks_in_plex:
33+
try:
3134
plex_track = track.track(title=track_name)
35+
36+
# Once we find a matching track, we want to break out of this iteration to get to the next song
3237
if plex_track:
33-
plex_tracks.append(plex_track)
34-
else:
35-
orig_tracks.append([track_name, "Song Not in Plex"])
36-
except Exception as plex_search_exception:
37-
print(plex_search_exception)
38+
break
39+
except NotFound:
40+
# This is not really an exception, just continue
41+
continue
42+
except (Exception, BadRequest) as plex_search_exception:
43+
print(f"Exception trying to search for title={artist_name}, title={track_name}")
44+
print(plex_search_exception)
45+
# While this is a fatal exception to the specific search, continue on since a subsequent match may succeed
46+
continue
47+
48+
if plex_track:
49+
plex_tracks.append(plex_track)
50+
else:
51+
print("Song not in plex!")
52+
print(f"Found artists for '{artist_name}' ({len(artist_tracks_in_plex)})")
53+
print(f"Attempted to match song '{track_name}', but could not!")
54+
orig_tracks.append([track_name, "Song Not in Plex"])
3855
else:
56+
print(f"No results found for artist: {artist_name}")
3957
continue
4058

59+
print(f"Found {len(plex_tracks)} of possible {len(spotify_tracks)} (Failed to find {len(orig_tracks)})")
60+
4161
return plex_tracks
4262

4363
def create_or_update_playlist(
@@ -61,9 +81,18 @@ def find_playlist_by_name(self, playlist_name):
6181
return playlist
6282
return None
6383

84+
# Playlist is created in intervals of 300 since Plex's API will return a 414 URL TOO LONG inconsistently past that
6485
def create_playlist(self, playlist_name, playlist_id, tracks):
86+
tracks_to_add = copy.deepcopy(tracks)
6587
try:
66-
new_playlist = self.plex.createPlaylist(playlist_name, items=tracks)
88+
iteration_tracks = tracks_to_add[:300]
89+
del tracks_to_add[:300]
90+
new_playlist = self.plex.createPlaylist(playlist_name, items=iteration_tracks)
91+
92+
while len(tracks_to_add) > 0:
93+
iteration_tracks = tracks_to_add[:300]
94+
del tracks_to_add[:300]
95+
new_playlist.addItems(iteration_tracks)
6796
return new_playlist
6897
except Exception as e:
6998
print(f"Error creating playlist {playlist_name}: {e}")

0 commit comments

Comments
 (0)