Skip to content

Commit 7f083e1

Browse files
committed
Added more verbose logging and tried to fix problem with emby no returning latest additions and deletions
1 parent cc90e42 commit 7f083e1

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

app.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Media Utilities
33
"""
44

5-
version = 'v2.2.1'
5+
version = 'v2.2.2'
66

77
import sys
88
import os

service/PlaylistSync.py

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
from logging import Logger
24
from apscheduler.schedulers.blocking import BlockingScheduler
35
from dataclasses import dataclass, field
@@ -14,6 +16,11 @@ class PlexCollectionConfig:
1416
collection_name: str
1517
target_servers: list[str] = field(default_factory=list)
1618

19+
@dataclass
20+
class AddDeleteInfo:
21+
added_items: int
22+
deleted_items: int
23+
1724
class PlaylistSync(ServiceBase):
1825
def __init__(self, ansi_code: str, plex_api: PlexAPI, emby_api: EmbyAPI, config, logger: Logger, scheduler: BlockingScheduler):
1926
super().__init__(ansi_code, self.__module__, config, logger, scheduler)
@@ -50,7 +57,7 @@ def __server_supported(self, server_type: str) -> str:
5057
return lower_name
5158
return ''
5259

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:
5461
# Check if any items were added to the playlist
5562
added_items: list[str] = []
5663
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
6774
for item in emby_playlist.items:
6875
if item.id not in emby_item_ids:
6976
deleted_playlist_items.append(item.playlist_item_id)
70-
77+
7178
if len(added_items) > 0 or len(deleted_playlist_items) > 0:
7279
if len(added_items) > 0:
7380
self.emby_api.add_playlist_items(emby_playlist.id, added_items)
7481

7582
if len(deleted_playlist_items) > 0:
7683
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)
7790

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):
7992
# Get the latest playlist
8093
emby_playlist:EmbyPlaylist = self.emby_api.get_playlist_items(playlist_id)
8194

@@ -90,8 +103,6 @@ def __emby_check_playlist_order_update(self, emby_item_ids: list[str], playlist_
90103
playlist_index += 1
91104

92105
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-
95106
# The order changed now iterate through the correct item order and find the playlist id to use in moving items
96107
current_index = 0
97108
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_
100111
self.emby_api.set_move_playlist_item_to_index(emby_playlist.id, current_playlist_item.playlist_item_id, current_index)
101112
current_index += 1
102113
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)))
103117
else:
104118
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)))
105119

@@ -119,8 +133,8 @@ def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollect
119133
else:
120134
emby_playlist:EmbyPlaylist = self.emby_api.get_playlist_items(emby_playlist_id)
121135
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)
124138

125139
def __sync_plex_collection(self, collection_config: PlexCollectionConfig):
126140
collection: PlexCollection = self.plex_api.get_collection(collection_config.library_name, collection_config.collection_name)

0 commit comments

Comments
 (0)