Skip to content

Commit 1717bf9

Browse files
committed
Fixed playlist sync calls to emby
1 parent 27d3a64 commit 1717bf9

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

api/emby.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class EmbyPlaylistItem:
1010
name: str
1111
id: str
12+
playlist_item_id: str
1213

1314
@dataclass
1415
class EmbyPlaylist:
@@ -206,10 +207,7 @@ def get_playlist_id(self, playlist_name: str) -> str:
206207
return self.get_invalid_item_id()
207208

208209
def __get_comma_separated_list(self, list_to_separate: list[str]) -> str:
209-
comma_separated_ids = ''
210-
for list_item in list_to_separate:
211-
comma_separated_ids += list_item + ','
212-
return comma_separated_ids.rstrip(',')
210+
return ','.join(list_to_separate)
213211

214212
def create_playlist(self, playlist_name: str, ids: list[str]) -> str:
215213
try:
@@ -230,15 +228,12 @@ def create_playlist(self, playlist_name: str, ids: list[str]) -> str:
230228
def get_playlist_items(self, playlist_id: str) -> EmbyPlaylist:
231229
try:
232230
playlist = self.search_item(playlist_id)
233-
234-
payload = {
235-
'api_key': self.api_key}
236-
r = requests.get(self.__get_api_url() + '/Playlists/' + playlist_id + '/Items', params=payload)
231+
r = requests.get(self.__get_api_url() + '/Playlists/' + playlist_id + '/Items?api_key=' + self.api_key)
237232
response = r.json()
238233

239234
emby_playlist = EmbyPlaylist(playlist['Name'], playlist_id)
240235
for item in response['Items']:
241-
emby_playlist.items.append(EmbyPlaylistItem(item['Name'], item['Id']))
236+
emby_playlist.items.append(EmbyPlaylistItem(item['Name'], item['Id'], item['PlaylistItemId']))
242237

243238
return emby_playlist
244239
except Exception as e:
@@ -249,30 +244,23 @@ def get_playlist_items(self, playlist_id: str) -> EmbyPlaylist:
249244
def add_playlist_items(self, playlist_id: str, item_ids: list[str]):
250245
try:
251246
headers = {'accept': 'application/json'}
252-
payload = {
253-
'Ids ': self.__get_comma_separated_list(item_ids)}
254-
embyUrl = self.__get_api_url()+ '/Playlists/' + playlist_id + '/Items' + '?api_key=' + self.api_key
255-
r = requests.post(embyUrl, headers=headers, data=payload)
256-
pass
247+
embyUrl = self.__get_api_url()+ '/Playlists/' + playlist_id + '/Items' + '?Ids=' + self.__get_comma_separated_list(item_ids) + '&api_key=' + self.api_key
248+
r = requests.post(embyUrl, headers=headers)
257249
except Exception as e:
258250
self.logger.error("{} add_playlist_items {} {} error {}".format(self.log_header, utils.get_tag('playlist_id', playlist_id), utils.get_tag('item_ids', item_ids), utils.get_tag('error', e)))
259251

260-
def remove_playlist_items(self, playlist_id: str, item_ids: list[str]):
252+
def remove_playlist_items(self, playlist_id: str, playlist_item_ids: list[str]):
261253
try:
262254
headers = {'accept': 'application/json'}
263-
payload = {
264-
'EntryIds': self.__get_comma_separated_list(item_ids)}
265-
embyUrl = self.__get_api_url()+ '/Playlists/' + playlist_id + '/Items/Delete' + '?api_key=' + self.api_key
266-
r = requests.post(embyUrl, headers=headers, data=payload)
267-
pass
255+
embyUrl = self.__get_api_url()+ '/Playlists/' + playlist_id + '/Items/Delete' + '?EntryIds=' + self.__get_comma_separated_list(playlist_item_ids) + '&api_key=' + self.api_key
256+
r = requests.post(embyUrl, headers=headers)
268257
except Exception as e:
269-
self.logger.error("{} remove_playlist_item {} {} error {}".format(self.log_header, utils.get_tag('playlist_id', playlist_id), utils.get_tag('item_ids', item_ids), utils.get_tag('error', e)))
258+
self.logger.error("{} remove_playlist_item {} {} error {}".format(self.log_header, utils.get_tag('playlist_id', playlist_id), utils.get_tag('playlist_item_ids', playlist_item_ids), utils.get_tag('error', e)))
270259

271-
def set_move_playlist_item_to_index(self, playlist_id: str, item_id: str, index: int):
260+
def set_move_playlist_item_to_index(self, playlist_id: str, playlist_item_id: str, index: int):
272261
try:
273262
headers = {'accept': 'application/json'}
274-
embyUrl = self.__get_api_url()+ '/Playlists/' + playlist_id + '/Items/' + item_id + '/Move/' + str(index) + '?api_key=' + self.api_key
263+
embyUrl = self.__get_api_url()+ '/Playlists/' + playlist_id + '/Items/' + playlist_item_id + '/Move/' + str(index) + '?api_key=' + self.api_key
275264
r = requests.post(embyUrl, headers=headers)
276-
pass
277265
except Exception as e:
278-
self.logger.error("{} set_move_playlist_item_to_index {} {} {} error {}".format(self.log_header, utils.get_tag('playlist_id', playlist_id), utils.get_tag('item_id', item_id), utils.get_tag('move_index', index), utils.get_tag('error', e)))
266+
self.logger.error("{} set_move_playlist_item_to_index {} {} {} error {}".format(self.log_header, utils.get_tag('playlist_id', playlist_id), utils.get_tag('playlist_item_id', playlist_item_id), utils.get_tag('move_index', index), utils.get_tag('error', e)))

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.0'
5+
version = 'v2.2.1'
66

77
import sys
88
import os

service/PlaylistSync.py

+33-22
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __server_supported(self, server_type: str) -> str:
5050
return lower_name
5151
return ''
5252

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):
5454
# Check if any items were added to the playlist
5555
added_items: list[str] = []
5656
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
6363
added_items.append(emby_item_id)
6464

6565
# Check if any items were deleted out of the playlist
66-
deleted_items: list[str] = []
66+
deleted_playlist_items: list[str] = []
6767
for item in emby_playlist.items:
6868
if item.id not in emby_item_ids:
69-
deleted_items.append(item.id)
69+
deleted_playlist_items.append(item.playlist_item_id)
7070

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
7485
playlist_index = 0
7586
for item_id in emby_item_ids:
7687
if item_id != emby_playlist.items[playlist_index].id:
7788
playlist_changed = True
7889
break
7990
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
80103
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)))
96105

97106
def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollection):
98107
emby_item_ids: list[str] = []
@@ -110,7 +119,8 @@ def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollect
110119
else:
111120
emby_playlist:EmbyPlaylist = self.emby_api.get_playlist_items(emby_playlist_id)
112121
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)
114124

115125
def __sync_plex_collection(self, collection_config: PlexCollectionConfig):
116126
collection: PlexCollection = self.plex_api.get_collection(collection_config.library_name, collection_config.collection_name)
@@ -133,6 +143,7 @@ def __sync_playlists(self):
133143
self.log_warning(self.emby_api.get_connection_error_log())
134144

135145
def init_scheduler_jobs(self):
146+
self.__sync_playlists()
136147
if self.cron is not None:
137148
self.log_service_enabled()
138149
self.scheduler.add_job(self.__sync_playlists, trigger='cron', hour=self.cron.hours, minute=self.cron.minutes)

0 commit comments

Comments
 (0)