Add logger, add looping

This commit is contained in:
Luigi311
2022-05-23 01:20:51 -06:00
parent 8b26e50346
commit 93da9eec34
4 changed files with 103 additions and 35 deletions

29
src/functions.py Normal file
View File

@@ -0,0 +1,29 @@
import os
from dotenv import load_dotenv
load_dotenv(override=True)
logfile = os.getenv("LOGFILE","log.log")
def logger(message, log_type=0):
output = str(message)
if log_type == 0:
pass
elif log_type == 1:
output = f"[INFO]: {output}"
elif log_type == 2:
output = f"[ERROR]: {output}"
else:
output = None
if output is not None:
print(output)
file = open(logfile, "a", encoding="utf-8")
file.write(output + "\n")
# Reimplementation of distutils.util.strtobool due to it being deprecated
# Source: https://github.com/PostHog/posthog/blob/01e184c29d2c10c43166f1d40a334abbc3f99d8a/posthog/utils.py#L668
def str_to_bool(value: any) -> bool:
if not value:
return False
return str(value).lower() in ("y", "yes", "t", "true", "on", "1")

View File

@@ -1,5 +1,6 @@
import requests, os
from dotenv import load_dotenv
from src.functions import logger
load_dotenv(override=True)
@@ -39,8 +40,8 @@ class Jellyfin():
return response.json()
except Exception as e:
print(e)
print(response)
logger(e, 2)
logger(response, 2)
def get_users(self):
users = {}
@@ -66,7 +67,7 @@ class Jellyfin():
for library in libraries:
library_title = library["Name"]
print(f"Jellyfin: Generating watched for {user_name} in library {library_title}")
logger(f"Jellyfin: Generating watched for {user_name} in library {library_title}", 0)
library_id = library["Id"]
# if whitelist is not empty and library is not in whitelist
@@ -128,7 +129,7 @@ class Jellyfin():
return users_watched
def update_watched(self, watched_list):
def update_watched(self, watched_list, dryrun=False):
for user, libraries in watched_list.items():
user_id = None
@@ -138,7 +139,7 @@ class Jellyfin():
break
if not user_id:
print(f"{user} not found in Jellyfin")
logger(f"{user} not found in Jellyfin", 2)
break
jellyfin_libraries = self.query(f"/Users/{user_id}/Views", "get")["Items"]
@@ -162,8 +163,12 @@ class Jellyfin():
for video in videos:
for key, value in jellyfin_video["ProviderIds"].items():
if key.lower() in video.keys() and value.lower() == video[key.lower()].lower():
print(f"Marking {jellyfin_video['Name']} as watched for {user}")
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_video_id}", "post")
msg = f"{jellyfin_video['Name']} as watched for {user}"
if not dryrun:
logger(f"Marking {msg}", 0)
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_video_id}", "post")
else:
logger(f"Dryrun {msg}", 0)
break
# TV Shows
@@ -183,7 +188,11 @@ class Jellyfin():
for episode in videos[show][season]:
for key, value in jellyfin_episode["ProviderIds"].items():
if key.lower() in episode.keys() and value.lower() == episode[key.lower()].lower():
print(f"Marked {jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} {jellyfin_episode['Name']} as watched for {user} in Jellyfin")
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_episode_id}", "post")
msg = f"{jellyfin_episode['SeriesName']} {jellyfin_episode['SeasonName']} {jellyfin_episode['Name']} as watched for {user} in Jellyfin"
if not dryrun:
logger(f"Marked {msg}", 0)
self.query(f"/Users/{user_id}/PlayedItems/{jellyfin_episode_id}", "post")
else:
logger(f"Dryrun {msg}", 0)
break

View File

@@ -1,5 +1,7 @@
import re, os
from dotenv import load_dotenv
from src.functions import logger
from plexapi.server import PlexServer
from plexapi.myplex import MyPlexAccount
@@ -23,14 +25,13 @@ class Plex:
def plex_login(self):
if self.baseurl:
# if self.username and self.password are not None or empty strings
if self.username and self.password:
if self.token:
# Login via token
plex = PlexServer(self.baseurl, self.token)
elif self.username and self.password:
# Login via plex account
account = MyPlexAccount(self.username, self.password)
plex = account.resource(self.baseurl).connect()
elif self.token:
# Login via token
plex = PlexServer(self.baseurl, self.token)
else:
raise Exception("No plex credentials provided")
else:
@@ -106,7 +107,7 @@ class Plex:
else:
if library_title.lower() not in [x.lower() for x in blacklist_library] and library.type not in [x.lower() for x in blacklist_library_type]:
for user in users:
print(f"Plex: Generating watched for {user.title} in library {library_title}")
logger(f"Plex: Generating watched for {user.title} in library {library_title}", 0)
user_name = user.title.lower()
watched = self.get_plex_user_watched(user, library)
if watched:
@@ -118,7 +119,7 @@ class Plex:
return users_watched
def update_watched(self, watched_list):
def update_watched(self, watched_list, dryrun=False):
for user, libraries in watched_list.items():
for index, value in enumerate(self.users):
if user.lower() == value.title.lower():
@@ -130,7 +131,7 @@ class Plex:
else:
user_plex = PlexServer(self.baseurl, user.get_token(self.plex.machineIdentifier))
print(f"Updating watched for {user.title}")
logger(f"Updating watched for {user.title}", 1)
for library, videos in libraries.items():
library_videos = user_plex.library.section(library)
@@ -143,8 +144,12 @@ class Plex:
for video_keys, video_id in video.items():
if video_keys == guid_source and video_id == guid_id:
if movies_search.viewCount == 0:
movies_search.markWatched()
print(f"Marked {movies_search.title} watched")
msg = f"{movies_search.title} watched"
if not dryrun:
logger(f"Marked {msg}", 0)
movies_search.markWatched()
else:
logger(f"Dyrun {msg}", 0)
break
elif library_videos.type == "show":
@@ -161,6 +166,10 @@ class Plex:
for episode_keys, episode_id in episode.items():
if episode_keys == guid_source and episode_id == guid_id:
if episode_search.viewCount == 0:
episode_search.markWatched()
print(f"Marked {show_search.title} {season_search.title} {episode_search.title} as watched for {user.title} in Plex")
msg = f"{show_search.title} {season_search.title} {episode_search.title} as watched for {user.title} in Plex"
if not dryrun:
logger(f"Marked {msg}", 0)
episode_search.markWatched()
else:
logger(f"Dryrun {msg}", 0)
break