@@ -50,7 +50,7 @@ def __server_supported(self, server_type: str) -> str:
50
50
return lower_name
51
51
return ''
52
52
53
- def __sync_emby_playlist (self , emby_item_ids : list [str ], emby_playlist : EmbyPlaylist ):
53
+ def __emby_add_remove_items_to_playlist (self , emby_item_ids : list [str ], emby_playlist : EmbyPlaylist ):
54
54
# Check if any items were added to the playlist
55
55
added_items : list [str ] = []
56
56
for emby_item_id in emby_item_ids :
@@ -63,36 +63,45 @@ def __sync_emby_playlist(self, emby_item_ids: list[str], emby_playlist: EmbyPlay
63
63
added_items .append (emby_item_id )
64
64
65
65
# Check if any items were deleted out of the playlist
66
- deleted_items : list [str ] = []
66
+ deleted_playlist_items : list [str ] = []
67
67
for item in emby_playlist .items :
68
68
if item .id not in emby_item_ids :
69
- deleted_items .append (item .id )
69
+ deleted_playlist_items .append (item .playlist_item_id )
70
70
71
- # Check if the order of the playlist items changed
72
- playlist_changed = False
73
- if len (emby_item_ids ) == len (emby_playlist .items ):
71
+ if len (added_items ) > 0 or len (deleted_playlist_items ) > 0 :
72
+ if len (added_items ) > 0 :
73
+ self .emby_api .add_playlist_items (emby_playlist .id , added_items )
74
+
75
+ if len (deleted_playlist_items ) > 0 :
76
+ self .emby_api .remove_playlist_items (emby_playlist .id , deleted_playlist_items )
77
+
78
+ def __emby_check_playlist_order_update (self , emby_item_ids : list [str ], playlist_id : str ):
79
+ # Get the latest playlist
80
+ emby_playlist :EmbyPlaylist = self .emby_api .get_playlist_items (playlist_id )
81
+
82
+ # Should be the correct length before this call but make sure
83
+ if len (emby_playlist .items ) == len (emby_item_ids ):
84
+ playlist_changed = False
74
85
playlist_index = 0
75
86
for item_id in emby_item_ids :
76
87
if item_id != emby_playlist .items [playlist_index ].id :
77
88
playlist_changed = True
78
89
break
79
90
playlist_index += 1
91
+
92
+ if playlist_changed is True :
93
+ self .log_info ('Syncing {} {} to {}' .format (utils .get_formatted_plex (), utils .get_tag ('collection' , emby_playlist .name ), utils .get_formatted_emby ()))
94
+
95
+ # The order changed now iterate through the correct item order and find the playlist id to use in moving items
96
+ current_index = 0
97
+ for correct_item_id in emby_item_ids :
98
+ for current_playlist_item in emby_playlist .items :
99
+ if correct_item_id == current_playlist_item .id :
100
+ self .emby_api .set_move_playlist_item_to_index (emby_playlist .id , current_playlist_item .playlist_item_id , current_index )
101
+ current_index += 1
102
+ break
80
103
else :
81
- playlist_changed = True
82
-
83
- if playlist_changed is True or len (added_items ) > 0 or len (deleted_items ) > 0 :
84
- self .log_info ('Syncing {} {} to {}' .format (utils .get_formatted_plex (), utils .get_tag ('collection' , emby_playlist .name ), utils .get_formatted_emby ()))
85
-
86
- if len (added_items ) > 0 :
87
- self .emby_api .add_playlist_items (emby_playlist .id , added_items )
88
-
89
- if len (deleted_items ) > 0 :
90
- self .emby_api .remove_playlist_items (emby_playlist .id , deleted_items )
91
-
92
- current_index = 1
93
- for item_id in emby_item_ids :
94
- self .emby_api .set_move_playlist_item_to_index (emby_playlist .id , item_id , current_index )
95
- current_index += 1
104
+ self .log_warning ('{} sync {} {} playlist update failed. Playlist length incorrect!' .format (utils .get_emby_ansi_code (), utils .get_plex_ansi_code (), utils .get_tag ('collection' , emby_playlist .name )))
96
105
97
106
def __sync_emby_playlist_with_plex_collection (self , plex_collection : PlexCollection ):
98
107
emby_item_ids : list [str ] = []
@@ -110,7 +119,8 @@ def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollect
110
119
else :
111
120
emby_playlist :EmbyPlaylist = self .emby_api .get_playlist_items (emby_playlist_id )
112
121
if emby_playlist is not None :
113
- self .__sync_emby_playlist (emby_item_ids , emby_playlist )
122
+ self .__emby_add_remove_items_to_playlist (emby_item_ids , emby_playlist )
123
+ self .__emby_check_playlist_order_update (emby_item_ids , emby_playlist_id )
114
124
115
125
def __sync_plex_collection (self , collection_config : PlexCollectionConfig ):
116
126
collection : PlexCollection = self .plex_api .get_collection (collection_config .library_name , collection_config .collection_name )
@@ -133,6 +143,7 @@ def __sync_playlists(self):
133
143
self .log_warning (self .emby_api .get_connection_error_log ())
134
144
135
145
def init_scheduler_jobs (self ):
146
+ self .__sync_playlists ()
136
147
if self .cron is not None :
137
148
self .log_service_enabled ()
138
149
self .scheduler .add_job (self .__sync_playlists , trigger = 'cron' , hour = self .cron .hours , minute = self .cron .minutes )
0 commit comments