Skip to content

Commit 1fcac70

Browse files
committed
Fixes to None stat checking
1 parent 6878df0 commit 1fcac70

File tree

5 files changed

+90
-75
lines changed

5 files changed

+90
-75
lines changed

api/emby.py

+16-13
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ def search_item(self, id: str) -> Any:
127127
self.logger.warning(
128128
f"{self.log_header} search_item returned no results {utils.get_tag("item", id)}"
129129
)
130-
return None
131130
except Exception as e:
132131
self.logger.error(
133132
f"{self.log_header} search_item {utils.get_tag("item", id)} {utils.get_tag("error", e)}"
134133
)
134+
135+
return None
135136

136137
def get_item_id_from_path(self, path) -> str:
137138
try:
@@ -298,21 +299,23 @@ def create_playlist(
298299
def get_playlist_items(self, playlist_id: str) -> EmbyPlaylist:
299300
try:
300301
playlist = self.search_item(playlist_id)
301-
r = requests.get(
302-
self.__get_api_url() + "/Playlists/" + playlist_id + "/Items?api_key=" + self.api_key)
303-
response = r.json()
302+
if playlist is not None:
303+
r = requests.get(
304+
self.__get_api_url() + "/Playlists/" + playlist_id + "/Items?api_key=" + self.api_key)
305+
response = r.json()
304306

305-
emby_playlist = EmbyPlaylist(playlist["Name"], playlist_id)
306-
for item in response["Items"]:
307-
emby_playlist.items.append(
308-
EmbyPlaylistItem(
309-
item["Name"],
310-
item["Id"],
311-
item["PlaylistItemId"]
307+
emby_playlist = EmbyPlaylist(playlist["Name"], playlist_id)
308+
for item in response["Items"]:
309+
emby_playlist.items.append(
310+
EmbyPlaylistItem(
311+
item["Name"],
312+
item["Id"],
313+
item["PlaylistItemId"]
314+
)
312315
)
313-
)
314316

315-
return emby_playlist
317+
return emby_playlist
318+
316319
except Exception as e:
317320
self.logger.error(
318321
f"{self.log_header} get_playlist_items {utils.get_tag("playlist_id", playlist_id)} {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.3.1"
5+
version = "v2.3.2"
66

77
import sys
88
import os

service/DeleteWatched.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ def __find_emby_watched_media(self, lib: LibraryInfo, user_list: List[UserInfo])
211211
item_id = item["EpisodeId"]
212212
else:
213213
item_id = item["NowPlayingItemId"]
214-
if self.emby_api.get_watched_status(user.emby_user_id, item_id):
214+
215+
emby_watched_status = self.emby_api.get_watched_status(user.emby_user_id, item_id)
216+
if emby_watched_status is not None and emby_watched_status:
215217
item_hours_since_play = self.hours_since_play(
216218
True,
217219
datetime.fromisoformat(item["ActivityDateInserted"])

service/PlaylistSync.py

+48-47
Original file line numberDiff line numberDiff line change
@@ -163,51 +163,52 @@ def __emby_update_playlist(
163163
# Get the latest playlist
164164
edited_emby_playlist:EmbyPlaylist = self.emby_api.get_playlist_items(original_emby_playlist.id)
165165

166-
# Should be the correct length before this call but make sure
167-
if len(edited_emby_playlist.items) == len(emby_item_ids):
168-
playlist_changed = False
169-
playlist_index = 0
170-
for item_id in emby_item_ids:
171-
if item_id != edited_emby_playlist.items[playlist_index].id:
172-
playlist_changed = True
173-
break
174-
playlist_index += 1
175-
176-
if playlist_changed:
177-
# The order changed now iterate through the correct item order and find the playlist id to use in moving items
178-
current_index = 0
179-
for correct_item_id in emby_item_ids:
180-
for current_playlist_item in edited_emby_playlist.items:
181-
if correct_item_id == current_playlist_item.id:
182-
if not self.emby_api.set_move_playlist_item_to_index(
183-
edited_emby_playlist.id,
184-
current_playlist_item.playlist_item_id,
185-
current_index
186-
):
187-
playlist_tag = utils.get_tag("playlist", original_emby_playlist.name)
188-
item_tag = utils.get_tag("item", current_playlist_item.playlist_item_id)
189-
index_tag = utils.get_tag("index", current_index)
190-
self.log_warning(
191-
f"{utils.get_formatted_emby()} failed {playlist_tag} moving {item_tag} to {index_tag}"
192-
)
193-
current_index += 1
194-
break
195-
196-
if playlist_changed or add_delete_info.added_items > 0 or add_delete_info.deleted_items > 0:
197-
collection_tag = utils.get_tag("collection", original_emby_playlist.name)
198-
added_tag = utils.get_tag("added", add_delete_info.added_items)
199-
deleted_tag = utils.get_tag("deleted", add_delete_info.deleted_items)
200-
reordered_tag = utils.get_tag("reordered", playlist_changed)
201-
self.log_info(
202-
f"Syncing {utils.get_formatted_plex()} {collection_tag} to {utils.get_formatted_emby()} {added_tag} {deleted_tag} {reordered_tag}"
203-
)
204-
else:
205-
collection_tag = utils.get_tag("collection", original_emby_playlist.name)
206-
length_tag = utils.get_tag("length", len(emby_item_ids))
207-
reported_length_tag = utils.get_tag("reported_length", len(edited_emby_playlist.items))
208-
self.log_warning(
209-
f"{utils.get_emby_ansi_code()} sync {utils.get_plex_ansi_code()} {collection_tag} playlist update failed. Playlist length should be {length_tag} {reported_length_tag}!"
210-
)
166+
if edited_emby_playlist is not None:
167+
# Should be the correct length before this call but make sure
168+
if len(edited_emby_playlist.items) == len(emby_item_ids):
169+
playlist_changed = False
170+
playlist_index = 0
171+
for item_id in emby_item_ids:
172+
if item_id != edited_emby_playlist.items[playlist_index].id:
173+
playlist_changed = True
174+
break
175+
playlist_index += 1
176+
177+
if playlist_changed:
178+
# The order changed now iterate through the correct item order and find the playlist id to use in moving items
179+
current_index = 0
180+
for correct_item_id in emby_item_ids:
181+
for current_playlist_item in edited_emby_playlist.items:
182+
if correct_item_id == current_playlist_item.id:
183+
if not self.emby_api.set_move_playlist_item_to_index(
184+
edited_emby_playlist.id,
185+
current_playlist_item.playlist_item_id,
186+
current_index
187+
):
188+
playlist_tag = utils.get_tag("playlist", original_emby_playlist.name)
189+
item_tag = utils.get_tag("item", current_playlist_item.playlist_item_id)
190+
index_tag = utils.get_tag("index", current_index)
191+
self.log_warning(
192+
f"{utils.get_formatted_emby()} failed {playlist_tag} moving {item_tag} to {index_tag}"
193+
)
194+
current_index += 1
195+
break
196+
197+
if playlist_changed or add_delete_info.added_items > 0 or add_delete_info.deleted_items > 0:
198+
collection_tag = utils.get_tag("collection", original_emby_playlist.name)
199+
added_tag = utils.get_tag("added", add_delete_info.added_items)
200+
deleted_tag = utils.get_tag("deleted", add_delete_info.deleted_items)
201+
reordered_tag = utils.get_tag("reordered", playlist_changed)
202+
self.log_info(
203+
f"Syncing {utils.get_formatted_plex()} {collection_tag} to {utils.get_formatted_emby()} {added_tag} {deleted_tag} {reordered_tag}"
204+
)
205+
else:
206+
collection_tag = utils.get_tag("collection", original_emby_playlist.name)
207+
length_tag = utils.get_tag("length", len(emby_item_ids))
208+
reported_length_tag = utils.get_tag("reported_length", len(edited_emby_playlist.items))
209+
self.log_warning(
210+
f"{utils.get_emby_ansi_code()} sync {utils.get_plex_ansi_code()} {collection_tag} playlist update failed. Playlist length should be {length_tag} {reported_length_tag}!"
211+
)
211212

212213
def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollection):
213214
emby_item_ids: list[str] = []
@@ -232,7 +233,7 @@ def __sync_emby_playlist_with_plex_collection(self, plex_collection: PlexCollect
232233
)
233234
else:
234235
emby_playlist:EmbyPlaylist = self.emby_api.get_playlist_items(emby_playlist_id)
235-
if emby_playlist:
236+
if emby_playlist is not None:
236237
self.__emby_update_playlist(emby_item_ids, emby_playlist)
237238

238239
# Give emby time to process
@@ -263,7 +264,7 @@ def __sync_playlists(self):
263264
self.log_warning(self.emby_api.get_connection_error_log())
264265

265266
def init_scheduler_jobs(self):
266-
if self.cron:
267+
if self.cron is not None:
267268
self.log_service_enabled()
268269
self.scheduler.add_job(
269270
self.__sync_playlists,

service/SyncWatched.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def __get_user_data(self) -> List[UserInfo]:
141141
plex_user_id = plex_user_info["user_id"]
142142
if (
143143
"friendly_name" in plex_user_info
144-
and plex_user_info["friendly_name"]
144+
and plex_user_info["friendly_name"] is not None
145145
and plex_user_info["friendly_name"] != ""
146146
):
147147
plex_friendly_name = plex_user_info["friendly_name"]
@@ -294,7 +294,11 @@ def __set_plex_show_watched(
294294
plex_episode_location = self.__get_plex_path(
295295
emby_episode_item["Path"]
296296
)
297-
if episode and not episode.isWatched and episode.locations[0] == plex_episode_location:
297+
if (
298+
episode is not None
299+
and not episode.isWatched
300+
and episode.locations[0] == plex_episode_location
301+
):
298302
episode.markWatched()
299303

300304
show_title = episode.grandparentTitle + " - " + episode.title
@@ -344,11 +348,14 @@ def __sync_plex_with_emby_watch_status(
344348
)
345349
)
346350
if hours_since_play < 24:
347-
if (jellystat_item["SeriesName"]
348-
and self.emby_api.get_watched_status(
349-
user.emby_user_id,
350-
jellystat_item["EpisodeId"]
351-
)
351+
emby_watched_status = self.emby_api.get_watched_status(
352+
user.emby_user_id,
353+
jellystat_item["EpisodeId"]
354+
)
355+
if (
356+
jellystat_item["SeriesName"] is not None
357+
and emby_watched_status is not None
358+
and emby_watched_status
352359
):
353360
emby_series_item = self.emby_api.search_item(
354361
jellystat_item["NowPlayingItemId"]
@@ -357,24 +364,26 @@ def __sync_plex_with_emby_watch_status(
357364
jellystat_item["EpisodeId"]
358365
)
359366

360-
if emby_series_item and emby_episode_item:
367+
if emby_series_item is not None and emby_episode_item is not None:
361368
self.__set_plex_show_watched(
362369
emby_series_item["Path"],
363370
emby_episode_item,
364371
user
365372
)
366373
else:
367-
# Check that the item has been marked as watched by emby
368-
if self.emby_api.get_watched_status(
374+
emby_watched_status = self.emby_api.get_watched_status(
369375
user.emby_user_id,
370376
jellystat_item["NowPlayingItemId"]
371-
):
377+
)
378+
379+
# Check that the item has been marked as watched by emby
380+
if emby_watched_status is not None and emby_watched_status:
372381
emby_item = self.emby_api.search_item(
373382
jellystat_item["NowPlayingItemId"]
374383
)
375384

376385
if (
377-
emby_item
386+
emby_item is not None
378387
and emby_item["Type"] == self.emby_api.get_media_type_movie_name()
379388
):
380389
self.__set_plex_movie_watched(emby_item, user)
@@ -416,7 +425,7 @@ def __sync_watch_status(self):
416425

417426
def init_scheduler_jobs(self):
418427
if len(self.config_user_list) > 0:
419-
if self.cron:
428+
if self.cron is not None:
420429
self.log_service_enabled()
421430

422431
self.scheduler.add_job(

0 commit comments

Comments
 (0)