Cleanup length and functions instead of methods
This commit is contained in:
@@ -518,7 +518,8 @@ class Jellyfin:
|
|||||||
|
|
||||||
if episode_found:
|
if episode_found:
|
||||||
jellyfin_episode_id = jellyfin_episode["Id"]
|
jellyfin_episode_id = jellyfin_episode["Id"]
|
||||||
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['Name']} as watched for {user_name} in {library} for Jellyfin"
|
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} Episode {jellyfin_episode['Name']}"
|
||||||
|
+f" as watched for {user_name} in {library} for Jellyfin"
|
||||||
if not dryrun:
|
if not dryrun:
|
||||||
logger(f"Marked {msg}", 0)
|
logger(f"Marked {msg}", 0)
|
||||||
await self.query(
|
await self.query(
|
||||||
@@ -551,7 +552,7 @@ class Jellyfin:
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger(
|
logger(
|
||||||
f"Jellyfin: Error updating watched for {user_name} in library {library}",
|
f"Jellyfin: Error updating watched for {user_name} in library {library}, {e}",
|
||||||
2,
|
2,
|
||||||
)
|
)
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|||||||
410
src/plex.py
410
src/plex.py
@@ -12,65 +12,7 @@ from src.functions import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# class plex accept base url and token and username and password but default with none
|
def get_user_watched(user, user_plex, library):
|
||||||
class Plex:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
baseurl=None,
|
|
||||||
token=None,
|
|
||||||
username=None,
|
|
||||||
password=None,
|
|
||||||
servername=None,
|
|
||||||
ssl_bypass=False,
|
|
||||||
):
|
|
||||||
self.baseurl = baseurl
|
|
||||||
self.token = token
|
|
||||||
self.username = username
|
|
||||||
self.password = password
|
|
||||||
self.servername = servername
|
|
||||||
self.plex = self.login(ssl_bypass)
|
|
||||||
self.admin_user = self.plex.myPlexAccount()
|
|
||||||
self.users = self.get_users()
|
|
||||||
|
|
||||||
def login(self, ssl_bypass=False):
|
|
||||||
try:
|
|
||||||
if self.baseurl and self.token:
|
|
||||||
# Login via token
|
|
||||||
if ssl_bypass:
|
|
||||||
session = requests.Session()
|
|
||||||
session.verify = False
|
|
||||||
plex = PlexServer(self.baseurl, self.token, session=session)
|
|
||||||
else:
|
|
||||||
plex = PlexServer(self.baseurl, self.token)
|
|
||||||
elif self.username and self.password and self.servername:
|
|
||||||
# Login via plex account
|
|
||||||
account = MyPlexAccount(self.username, self.password)
|
|
||||||
plex = account.resource(self.servername).connect()
|
|
||||||
else:
|
|
||||||
raise Exception("No complete plex credentials provided")
|
|
||||||
|
|
||||||
return plex
|
|
||||||
except Exception as e:
|
|
||||||
if self.username or self.password:
|
|
||||||
msg = f"Failed to login via plex account {self.username}"
|
|
||||||
logger(f"Plex: Failed to login, {msg}, Error: {e}", 2)
|
|
||||||
else:
|
|
||||||
logger(f"Plex: Failed to login, Error: {e}", 2)
|
|
||||||
raise Exception(e)
|
|
||||||
|
|
||||||
def get_users(self):
|
|
||||||
try:
|
|
||||||
users = self.plex.myPlexAccount().users()
|
|
||||||
|
|
||||||
# append self to users
|
|
||||||
users.append(self.plex.myPlexAccount())
|
|
||||||
|
|
||||||
return users
|
|
||||||
except Exception as e:
|
|
||||||
logger(f"Plex: Failed to get users, Error: {e}", 2)
|
|
||||||
raise Exception(e)
|
|
||||||
|
|
||||||
def get_user_watched(self, user, user_plex, library):
|
|
||||||
try:
|
try:
|
||||||
user_name = user.title.lower()
|
user_name = user.title.lower()
|
||||||
user_watched = {}
|
user_watched = {}
|
||||||
@@ -143,9 +85,7 @@ class Plex:
|
|||||||
user_watched[user_name][library.title][show_guids] = {}
|
user_watched[user_name][library.title][show_guids] = {}
|
||||||
if (
|
if (
|
||||||
season.title
|
season.title
|
||||||
not in user_watched[user_name][library.title][
|
not in user_watched[user_name][library.title][show_guids]
|
||||||
show_guids
|
|
||||||
]
|
|
||||||
):
|
):
|
||||||
user_watched[user_name][library.title][show_guids][
|
user_watched[user_name][library.title][show_guids][
|
||||||
season.title
|
season.title
|
||||||
@@ -162,6 +102,198 @@ class Plex:
|
|||||||
)
|
)
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|
||||||
|
|
||||||
|
def update_user_watched(user, user_plex, library, videos, dryrun):
|
||||||
|
try:
|
||||||
|
logger(f"Plex: Updating watched for {user.title} in library {library}", 1)
|
||||||
|
(
|
||||||
|
videos_shows_ids,
|
||||||
|
videos_episodes_ids,
|
||||||
|
videos_movies_ids,
|
||||||
|
) = generate_library_guids_dict(videos)
|
||||||
|
logger(
|
||||||
|
f"Plex: mark list\nShows: {videos_shows_ids}\nEpisodes: {videos_episodes_ids}\nMovies: {videos_movies_ids}",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
library_videos = user_plex.library.section(library)
|
||||||
|
if videos_movies_ids:
|
||||||
|
for movies_search in library_videos.search(unwatched=True):
|
||||||
|
movie_found = False
|
||||||
|
for movie_location in movies_search.locations:
|
||||||
|
if movie_location.split("/")[-1] in videos_movies_ids["locations"]:
|
||||||
|
movie_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not movie_found:
|
||||||
|
for movie_guid in movies_search.guids:
|
||||||
|
movie_guid_source = (
|
||||||
|
re.search(r"(.*)://", movie_guid.id).group(1).lower()
|
||||||
|
)
|
||||||
|
movie_guid_id = re.search(r"://(.*)", movie_guid.id).group(1)
|
||||||
|
|
||||||
|
# If movie provider source and movie provider id are in videos_movie_ids exactly, then the movie is in the list
|
||||||
|
if movie_guid_source in videos_movies_ids.keys():
|
||||||
|
if movie_guid_id in videos_movies_ids[movie_guid_source]:
|
||||||
|
movie_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if movie_found:
|
||||||
|
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
|
||||||
|
if not dryrun:
|
||||||
|
logger(f"Marked {msg}", 0)
|
||||||
|
movies_search.markWatched()
|
||||||
|
else:
|
||||||
|
logger(f"Dryrun {msg}", 0)
|
||||||
|
else:
|
||||||
|
logger(
|
||||||
|
f"Plex: Skipping movie {movies_search.title} as it is not in mark list for {user.title}",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
if videos_shows_ids and videos_episodes_ids:
|
||||||
|
for show_search in library_videos.search(unwatched=True):
|
||||||
|
show_found = False
|
||||||
|
for show_location in show_search.locations:
|
||||||
|
if show_location.split("/")[-1] in videos_shows_ids["locations"]:
|
||||||
|
show_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not show_found:
|
||||||
|
for show_guid in show_search.guids:
|
||||||
|
show_guid_source = (
|
||||||
|
re.search(r"(.*)://", show_guid.id).group(1).lower()
|
||||||
|
)
|
||||||
|
show_guid_id = re.search(r"://(.*)", show_guid.id).group(1)
|
||||||
|
|
||||||
|
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
|
||||||
|
if show_guid_source in videos_shows_ids.keys():
|
||||||
|
if show_guid_id in videos_shows_ids[show_guid_source]:
|
||||||
|
show_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if show_found:
|
||||||
|
for episode_search in show_search.episodes():
|
||||||
|
episode_found = False
|
||||||
|
|
||||||
|
for episode_location in episode_search.locations:
|
||||||
|
if (
|
||||||
|
episode_location.split("/")[-1]
|
||||||
|
in videos_episodes_ids["locations"]
|
||||||
|
):
|
||||||
|
episode_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not episode_found:
|
||||||
|
for episode_guid in episode_search.guids:
|
||||||
|
episode_guid_source = (
|
||||||
|
re.search(r"(.*)://", episode_guid.id)
|
||||||
|
.group(1)
|
||||||
|
.lower()
|
||||||
|
)
|
||||||
|
episode_guid_id = re.search(
|
||||||
|
r"://(.*)", episode_guid.id
|
||||||
|
).group(1)
|
||||||
|
|
||||||
|
# If episode provider source and episode provider id are in videos_episodes_ids exactly, then the episode is in the list
|
||||||
|
if episode_guid_source in videos_episodes_ids.keys():
|
||||||
|
if (
|
||||||
|
episode_guid_id
|
||||||
|
in videos_episodes_ids[episode_guid_source]
|
||||||
|
):
|
||||||
|
episode_found = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if episode_found:
|
||||||
|
msg = f"{show_search.title} {episode_search.title} as watched for {user.title} in {library} for Plex"
|
||||||
|
if not dryrun:
|
||||||
|
logger(f"Marked {msg}", 0)
|
||||||
|
episode_search.markWatched()
|
||||||
|
else:
|
||||||
|
logger(f"Dryrun {msg}", 0)
|
||||||
|
else:
|
||||||
|
logger(
|
||||||
|
f"Plex: Skipping episode {episode_search.title} as it is not in mark list for {user.title}",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger(
|
||||||
|
f"Plex: Skipping show {show_search.title} as it is not in mark list for {user.title}",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not videos_movies_ids and not videos_shows_ids and not videos_episodes_ids:
|
||||||
|
logger(
|
||||||
|
f"Jellyfin: No videos to mark as watched for {user.title} in library {library}",
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger(
|
||||||
|
f"Plex: Failed to update watched for {user.title} in library {library}, Error: {e}",
|
||||||
|
2,
|
||||||
|
)
|
||||||
|
raise Exception(e)
|
||||||
|
|
||||||
|
|
||||||
|
# class plex accept base url and token and username and password but default with none
|
||||||
|
class Plex:
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
baseurl=None,
|
||||||
|
token=None,
|
||||||
|
username=None,
|
||||||
|
password=None,
|
||||||
|
servername=None,
|
||||||
|
ssl_bypass=False,
|
||||||
|
):
|
||||||
|
self.baseurl = baseurl
|
||||||
|
self.token = token
|
||||||
|
self.username = username
|
||||||
|
self.password = password
|
||||||
|
self.servername = servername
|
||||||
|
self.plex = self.login(ssl_bypass)
|
||||||
|
self.admin_user = self.plex.myPlexAccount()
|
||||||
|
self.users = self.get_users()
|
||||||
|
|
||||||
|
def login(self, ssl_bypass=False):
|
||||||
|
try:
|
||||||
|
if self.baseurl and self.token:
|
||||||
|
# Login via token
|
||||||
|
if ssl_bypass:
|
||||||
|
session = requests.Session()
|
||||||
|
session.verify = False
|
||||||
|
plex = PlexServer(self.baseurl, self.token, session=session)
|
||||||
|
else:
|
||||||
|
plex = PlexServer(self.baseurl, self.token)
|
||||||
|
elif self.username and self.password and self.servername:
|
||||||
|
# Login via plex account
|
||||||
|
account = MyPlexAccount(self.username, self.password)
|
||||||
|
plex = account.resource(self.servername).connect()
|
||||||
|
else:
|
||||||
|
raise Exception("No complete plex credentials provided")
|
||||||
|
|
||||||
|
return plex
|
||||||
|
except Exception as e:
|
||||||
|
if self.username or self.password:
|
||||||
|
msg = f"Failed to login via plex account {self.username}"
|
||||||
|
logger(f"Plex: Failed to login, {msg}, Error: {e}", 2)
|
||||||
|
else:
|
||||||
|
logger(f"Plex: Failed to login, Error: {e}", 2)
|
||||||
|
raise Exception(e)
|
||||||
|
|
||||||
|
def get_users(self):
|
||||||
|
try:
|
||||||
|
users = self.plex.myPlexAccount().users()
|
||||||
|
|
||||||
|
# append self to users
|
||||||
|
users.append(self.plex.myPlexAccount())
|
||||||
|
|
||||||
|
return users
|
||||||
|
except Exception as e:
|
||||||
|
logger(f"Plex: Failed to get users, Error: {e}", 2)
|
||||||
|
raise Exception(e)
|
||||||
|
|
||||||
def get_watched(
|
def get_watched(
|
||||||
self,
|
self,
|
||||||
users,
|
users,
|
||||||
@@ -206,7 +338,7 @@ class Plex:
|
|||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
args.append([self.get_user_watched, user, user_plex, library])
|
args.append([get_user_watched, user, user_plex, library])
|
||||||
|
|
||||||
for user_watched in future_thread_executor(args):
|
for user_watched in future_thread_executor(args):
|
||||||
for user, user_watched_temp in user_watched.items():
|
for user, user_watched_temp in user_watched.items():
|
||||||
@@ -219,156 +351,6 @@ class Plex:
|
|||||||
logger(f"Plex: Failed to get watched, Error: {e}", 2)
|
logger(f"Plex: Failed to get watched, Error: {e}", 2)
|
||||||
raise Exception(e)
|
raise Exception(e)
|
||||||
|
|
||||||
def update_user_watched(self, user, user_plex, library, videos, dryrun):
|
|
||||||
try:
|
|
||||||
logger(f"Plex: Updating watched for {user.title} in library {library}", 1)
|
|
||||||
(
|
|
||||||
videos_shows_ids,
|
|
||||||
videos_episodes_ids,
|
|
||||||
videos_movies_ids,
|
|
||||||
) = generate_library_guids_dict(videos)
|
|
||||||
logger(
|
|
||||||
f"Plex: mark list\nShows: {videos_shows_ids}\nEpisodes: {videos_episodes_ids}\nMovies: {videos_movies_ids}",
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
|
|
||||||
library_videos = user_plex.library.section(library)
|
|
||||||
if videos_movies_ids:
|
|
||||||
for movies_search in library_videos.search(unwatched=True):
|
|
||||||
movie_found = False
|
|
||||||
for movie_location in movies_search.locations:
|
|
||||||
if (
|
|
||||||
movie_location.split("/")[-1]
|
|
||||||
in videos_movies_ids["locations"]
|
|
||||||
):
|
|
||||||
movie_found = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not movie_found:
|
|
||||||
for movie_guid in movies_search.guids:
|
|
||||||
movie_guid_source = (
|
|
||||||
re.search(r"(.*)://", movie_guid.id).group(1).lower()
|
|
||||||
)
|
|
||||||
movie_guid_id = re.search(r"://(.*)", movie_guid.id).group(
|
|
||||||
1
|
|
||||||
)
|
|
||||||
|
|
||||||
# If movie provider source and movie provider id are in videos_movie_ids exactly, then the movie is in the list
|
|
||||||
if movie_guid_source in videos_movies_ids.keys():
|
|
||||||
if (
|
|
||||||
movie_guid_id
|
|
||||||
in videos_movies_ids[movie_guid_source]
|
|
||||||
):
|
|
||||||
movie_found = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if movie_found:
|
|
||||||
msg = f"{movies_search.title} as watched for {user.title} in {library} for Plex"
|
|
||||||
if not dryrun:
|
|
||||||
logger(f"Marked {msg}", 0)
|
|
||||||
movies_search.markWatched()
|
|
||||||
else:
|
|
||||||
logger(f"Dryrun {msg}", 0)
|
|
||||||
else:
|
|
||||||
logger(
|
|
||||||
f"Plex: Skipping movie {movies_search.title} as it is not in mark list for {user.title}",
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
|
|
||||||
if videos_shows_ids and videos_episodes_ids:
|
|
||||||
for show_search in library_videos.search(unwatched=True):
|
|
||||||
show_found = False
|
|
||||||
for show_location in show_search.locations:
|
|
||||||
if (
|
|
||||||
show_location.split("/")[-1]
|
|
||||||
in videos_shows_ids["locations"]
|
|
||||||
):
|
|
||||||
show_found = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not show_found:
|
|
||||||
for show_guid in show_search.guids:
|
|
||||||
show_guid_source = (
|
|
||||||
re.search(r"(.*)://", show_guid.id).group(1).lower()
|
|
||||||
)
|
|
||||||
show_guid_id = re.search(r"://(.*)", show_guid.id).group(1)
|
|
||||||
|
|
||||||
# If show provider source and show provider id are in videos_shows_ids exactly, then the show is in the list
|
|
||||||
if show_guid_source in videos_shows_ids.keys():
|
|
||||||
if show_guid_id in videos_shows_ids[show_guid_source]:
|
|
||||||
show_found = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if show_found:
|
|
||||||
for episode_search in show_search.episodes():
|
|
||||||
episode_found = False
|
|
||||||
|
|
||||||
for episode_location in episode_search.locations:
|
|
||||||
if (
|
|
||||||
episode_location.split("/")[-1]
|
|
||||||
in videos_episodes_ids["locations"]
|
|
||||||
):
|
|
||||||
episode_found = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if not episode_found:
|
|
||||||
for episode_guid in episode_search.guids:
|
|
||||||
episode_guid_source = (
|
|
||||||
re.search(r"(.*)://", episode_guid.id)
|
|
||||||
.group(1)
|
|
||||||
.lower()
|
|
||||||
)
|
|
||||||
episode_guid_id = re.search(
|
|
||||||
r"://(.*)", episode_guid.id
|
|
||||||
).group(1)
|
|
||||||
|
|
||||||
# If episode provider source and episode provider id are in videos_episodes_ids exactly, then the episode is in the list
|
|
||||||
if (
|
|
||||||
episode_guid_source
|
|
||||||
in videos_episodes_ids.keys()
|
|
||||||
):
|
|
||||||
if (
|
|
||||||
episode_guid_id
|
|
||||||
in videos_episodes_ids[episode_guid_source]
|
|
||||||
):
|
|
||||||
episode_found = True
|
|
||||||
break
|
|
||||||
|
|
||||||
if episode_found:
|
|
||||||
msg = f"{show_search.title} {episode_search.title} as watched for {user.title} in {library} for Plex"
|
|
||||||
if not dryrun:
|
|
||||||
logger(f"Marked {msg}", 0)
|
|
||||||
episode_search.markWatched()
|
|
||||||
else:
|
|
||||||
logger(f"Dryrun {msg}", 0)
|
|
||||||
else:
|
|
||||||
logger(
|
|
||||||
f"Plex: Skipping episode {episode_search.title} as it is not in mark list for {user.title}",
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
logger(
|
|
||||||
f"Plex: Skipping show {show_search.title} as it is not in mark list for {user.title}",
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
|
|
||||||
if (
|
|
||||||
not videos_movies_ids
|
|
||||||
and not videos_shows_ids
|
|
||||||
and not videos_episodes_ids
|
|
||||||
):
|
|
||||||
logger(
|
|
||||||
f"Jellyfin: No videos to mark as watched for {user.title} in library {library}",
|
|
||||||
1,
|
|
||||||
)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger(
|
|
||||||
f"Plex: Failed to update watched for {user.title} in library {library}, Error: {e}",
|
|
||||||
2,
|
|
||||||
)
|
|
||||||
raise Exception(e)
|
|
||||||
|
|
||||||
def update_watched(
|
def update_watched(
|
||||||
self, watched_list, user_mapping=None, library_mapping=None, dryrun=False
|
self, watched_list, user_mapping=None, library_mapping=None, dryrun=False
|
||||||
):
|
):
|
||||||
@@ -433,7 +415,7 @@ class Plex:
|
|||||||
|
|
||||||
args.append(
|
args.append(
|
||||||
[
|
[
|
||||||
self.update_user_watched,
|
update_user_watched,
|
||||||
user,
|
user,
|
||||||
user_plex,
|
user_plex,
|
||||||
library,
|
library,
|
||||||
|
|||||||
Reference in New Issue
Block a user