Plex: Do not error if guids can not be gathered. Parallelize show processing for get watched.
This commit is contained in:
89
src/plex.py
89
src/plex.py
@@ -1,4 +1,4 @@
|
|||||||
import re, requests
|
import re, requests, os
|
||||||
from urllib3.poolmanager import PoolManager
|
from urllib3.poolmanager import PoolManager
|
||||||
|
|
||||||
from plexapi.server import PlexServer
|
from plexapi.server import PlexServer
|
||||||
@@ -24,6 +24,52 @@ class HostNameIgnoringAdapter(requests.adapters.HTTPAdapter):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_library_watched_show(show):
|
||||||
|
try:
|
||||||
|
show_guids = {}
|
||||||
|
for show_guid in show.guids:
|
||||||
|
# Extract source and id from guid.id
|
||||||
|
m = re.match(r"(.*)://(.*)", show_guid.id)
|
||||||
|
show_guid_source, show_guid_id = m.group(1).lower(), m.group(2)
|
||||||
|
show_guids[show_guid_source] = show_guid_id
|
||||||
|
|
||||||
|
show_guids["title"] = show.title
|
||||||
|
show_guids["locations"] = tuple([x.split("/")[-1] for x in show.locations])
|
||||||
|
show_guids = frozenset(show_guids.items())
|
||||||
|
|
||||||
|
# Get all watched episodes for show
|
||||||
|
episode_guids = {}
|
||||||
|
watched_episodes = show.watched()
|
||||||
|
for episode in watched_episodes:
|
||||||
|
episode_guids_temp = {}
|
||||||
|
try:
|
||||||
|
if len(episode.guids) > 0:
|
||||||
|
for guid in episode.guids:
|
||||||
|
# Extract after :// from guid.id
|
||||||
|
m = re.match(r"(.*)://(.*)", guid.id)
|
||||||
|
guid_source, guid_id = m.group(1).lower(), m.group(2)
|
||||||
|
episode_guids_temp[guid_source] = guid_id
|
||||||
|
except:
|
||||||
|
logger(
|
||||||
|
f"Plex: Failed to get guids for {episode.title} in {show.title}, Using location only",
|
||||||
|
4,
|
||||||
|
)
|
||||||
|
|
||||||
|
episode_guids_temp["locations"] = tuple(
|
||||||
|
[x.split("/")[-1] for x in episode.locations]
|
||||||
|
)
|
||||||
|
|
||||||
|
if episode.parentTitle not in episode_guids:
|
||||||
|
episode_guids[episode.parentTitle] = []
|
||||||
|
|
||||||
|
episode_guids[episode.parentTitle].append(episode_guids_temp)
|
||||||
|
|
||||||
|
return show_guids, episode_guids
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return {}, {}
|
||||||
|
|
||||||
|
|
||||||
def get_user_library_watched(user, user_plex, library):
|
def get_user_library_watched(user, user_plex, library):
|
||||||
try:
|
try:
|
||||||
user_name = user.title.lower()
|
user_name = user.title.lower()
|
||||||
@@ -61,40 +107,17 @@ def get_user_library_watched(user, user_plex, library):
|
|||||||
|
|
||||||
elif library.type == "show":
|
elif library.type == "show":
|
||||||
user_watched[user_name][library.title] = {}
|
user_watched[user_name][library.title] = {}
|
||||||
|
shows = library_videos.search(unwatched=False)
|
||||||
|
|
||||||
for show in library_videos.search(unwatched=False):
|
# Parallelize show processing
|
||||||
logger(f"Plex: Adding {show.title} to {user_name} watched list", 3)
|
args = []
|
||||||
show_guids = {}
|
for show in shows:
|
||||||
for show_guid in show.guids:
|
args.append([get_user_library_watched_show, show])
|
||||||
# Extract source and id from guid.id
|
|
||||||
m = re.match(r"(.*)://(.*)", show_guid.id)
|
|
||||||
show_guid_source, show_guid_id = m.group(1).lower(), m.group(2)
|
|
||||||
show_guids[show_guid_source] = show_guid_id
|
|
||||||
|
|
||||||
show_guids["title"] = show.title
|
for show_guids, episode_guids in future_thread_executor(
|
||||||
show_guids["locations"] = tuple(
|
args, workers=min(os.cpu_count(), 4)
|
||||||
[x.split("/")[-1] for x in show.locations]
|
):
|
||||||
)
|
if show_guids and episode_guids:
|
||||||
show_guids = frozenset(show_guids.items())
|
|
||||||
|
|
||||||
# Get all watched episodes for show
|
|
||||||
episode_guids = {}
|
|
||||||
for episode in show.watched():
|
|
||||||
episode_guids_temp = {}
|
|
||||||
for guid in episode.guids:
|
|
||||||
# Extract after :// from guid.id
|
|
||||||
m = re.match(r"(.*)://(.*)", guid.id)
|
|
||||||
guid_source, guid_id = m.group(1).lower(), m.group(2)
|
|
||||||
episode_guids_temp[guid_source] = guid_id
|
|
||||||
|
|
||||||
episode_guids_temp["locations"] = tuple(
|
|
||||||
[x.split("/")[-1] for x in episode.locations]
|
|
||||||
)
|
|
||||||
if episode.parentTitle not in episode_guids:
|
|
||||||
episode_guids[episode.parentTitle] = []
|
|
||||||
episode_guids[episode.parentTitle].append(episode_guids_temp)
|
|
||||||
|
|
||||||
if episode_guids:
|
|
||||||
# append show, season, episode
|
# append show, season, episode
|
||||||
if show_guids not in user_watched[user_name][library.title]:
|
if show_guids not in user_watched[user_name][library.title]:
|
||||||
user_watched[user_name][library.title][show_guids] = {}
|
user_watched[user_name][library.title][show_guids] = {}
|
||||||
|
|||||||
Reference in New Issue
Block a user