1
1
import requests
2
2
import urllib3
3
3
from plexapi .server import PlexServer
4
+ from plexapi .exceptions import BadRequest , NotFound
4
5
from confighandler import read_config
6
+ import copy
5
7
6
8
7
9
class PlexService :
@@ -19,25 +21,43 @@ def connect_plex(self):
19
21
return PlexServer (self .server_url , self .server_token , session = session )
20
22
21
23
def check_tracks_in_plex (self , spotify_tracks ):
24
+ print ("Checking tracks in plex" )
22
25
music_lib = self .plex .library .section ("Music" )
23
26
plex_tracks = []
24
27
orig_tracks = []
25
28
26
29
for track_name , artist_name in spotify_tracks :
27
30
artist_tracks_in_plex = music_lib .search (title = artist_name )
28
31
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 :
31
34
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
32
37
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" ])
38
55
else :
56
+ print (f"No results found for artist: { artist_name } " )
39
57
continue
40
58
59
+ print (f"Found { len (plex_tracks )} of possible { len (spotify_tracks )} (Failed to find { len (orig_tracks )} )" )
60
+
41
61
return plex_tracks
42
62
43
63
def create_or_update_playlist (
@@ -61,9 +81,18 @@ def find_playlist_by_name(self, playlist_name):
61
81
return playlist
62
82
return None
63
83
84
+ # Playlist is created in intervals of 300 since Plex's API will return a 414 URL TOO LONG inconsistently past that
64
85
def create_playlist (self , playlist_name , playlist_id , tracks ):
86
+ tracks_to_add = copy .deepcopy (tracks )
65
87
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 )
67
96
return new_playlist
68
97
except Exception as e :
69
98
print (f"Error creating playlist { playlist_name } : { e } " )
0 commit comments