1
+ import time
2
+
1
3
from logging import Logger
2
4
from apscheduler .schedulers .blocking import BlockingScheduler
3
5
from dataclasses import dataclass , field
@@ -14,6 +16,11 @@ class PlexCollectionConfig:
14
16
collection_name : str
15
17
target_servers : list [str ] = field (default_factory = list )
16
18
19
+ @dataclass
20
+ class AddDeleteInfo :
21
+ added_items : int
22
+ deleted_items : int
23
+
17
24
class PlaylistSync (ServiceBase ):
18
25
def __init__ (self , ansi_code : str , plex_api : PlexAPI , emby_api : EmbyAPI , config , logger : Logger , scheduler : BlockingScheduler ):
19
26
super ().__init__ (ansi_code , self .__module__ , config , logger , scheduler )
@@ -50,7 +57,7 @@ def __server_supported(self, server_type: str) -> str:
50
57
return lower_name
51
58
return ''
52
59
53
- def __emby_add_remove_items_to_playlist (self , emby_item_ids : list [str ], emby_playlist : EmbyPlaylist ):
60
+ def __emby_add_remove_items_to_playlist (self , emby_item_ids : list [str ], emby_playlist : EmbyPlaylist ) -> AddDeleteInfo :
54
61
# Check if any items were added to the playlist
55
62
added_items : list [str ] = []
56
63
for emby_item_id in emby_item_ids :
@@ -67,15 +74,21 @@ def __emby_add_remove_items_to_playlist(self, emby_item_ids: list[str], emby_pla
67
74
for item in emby_playlist .items :
68
75
if item .id not in emby_item_ids :
69
76
deleted_playlist_items .append (item .playlist_item_id )
70
-
77
+
71
78
if len (added_items ) > 0 or len (deleted_playlist_items ) > 0 :
72
79
if len (added_items ) > 0 :
73
80
self .emby_api .add_playlist_items (emby_playlist .id , added_items )
74
81
75
82
if len (deleted_playlist_items ) > 0 :
76
83
self .emby_api .remove_playlist_items (emby_playlist .id , deleted_playlist_items )
84
+
85
+ # Give emby time to update the playlist
86
+ time .sleep (1 )
87
+
88
+ return AddDeleteInfo (len (added_items ), len (deleted_playlist_items ))
89
+ return AddDeleteInfo (0 , 0 )
77
90
78
- def __emby_check_playlist_order_update (self , emby_item_ids : list [str ], playlist_id : str ):
91
+ def __emby_check_playlist_order_update (self , emby_item_ids : list [str ], playlist_id : str , add_delete_info : AddDeleteInfo ):
79
92
# Get the latest playlist
80
93
emby_playlist :EmbyPlaylist = self .emby_api .get_playlist_items (playlist_id )
81
94
@@ -90,8 +103,6 @@ def __emby_check_playlist_order_update(self, emby_item_ids: list[str], playlist_
90
103
playlist_index += 1
91
104
92
105
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
106
# The order changed now iterate through the correct item order and find the playlist id to use in moving items
96
107
current_index = 0
97
108
for correct_item_id in emby_item_ids :
@@ -100,6 +111,9 @@ def __emby_check_playlist_order_update(self, emby_item_ids: list[str], playlist_
100
111
self .emby_api .set_move_playlist_item_to_index (emby_playlist .id , current_playlist_item .playlist_item_id , current_index )
101
112
current_index += 1
102
113
break
114
+
115
+ if playlist_changed is True or add_delete_info .added_items > 0 or add_delete_info .deleted_items > 0 :
116
+ self .log_info ('Syncing {} {} to {} {} {} {}' .format (utils .get_formatted_plex (), utils .get_tag ('collection' , emby_playlist .name ), utils .get_formatted_emby (), utils .get_tag ('added' , add_delete_info .added_items ), utils .get_tag ('deleted' , add_delete_info .deleted_items ), utils .get_tag ('reordered' , playlist_changed )))
103
117
else :
104
118
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 )))
105
119
@@ -119,8 +133,8 @@ def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollect
119
133
else :
120
134
emby_playlist :EmbyPlaylist = self .emby_api .get_playlist_items (emby_playlist_id )
121
135
if emby_playlist is not None :
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 )
136
+ add_delete_info = self .__emby_add_remove_items_to_playlist (emby_item_ids , emby_playlist )
137
+ self .__emby_check_playlist_order_update (emby_item_ids , emby_playlist_id , add_delete_info )
124
138
125
139
def __sync_plex_collection (self , collection_config : PlexCollectionConfig ):
126
140
collection : PlexCollection = self .plex_api .get_collection (collection_config .library_name , collection_config .collection_name )
0 commit comments