diff --git a/.env.sample b/.env.sample index 2f231ea..af6e63e 100644 --- a/.env.sample +++ b/.env.sample @@ -18,6 +18,9 @@ SLEEP_DURATION = "3600" ## Log file where all output will be written to LOGFILE = "log.log" +## Timeout for requests for jellyfin +REQUEST_TIMEOUT = 300 + ## Map usernames between servers in the event that they are different, order does not matter ## Comma separated for multiple options #USER_MAPPING = { "testuser2": "testuser3", "testuser1":"testuser4" } diff --git a/README.md b/README.md index 35e1c76..899425d 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,9 @@ SLEEP_DURATION = "3600" ## Log file where all output will be written to LOGFILE = "log.log" +## Timeout for requests for jellyfin +REQUEST_TIMEOUT = 300 + ## Map usernames between servers in the event that they are different, order does not matter ## Comma separated for multiple options USER_MAPPING = { "testuser2": "testuser3", "testuser1":"testuser4" } diff --git a/src/jellyfin.py b/src/jellyfin.py index e4bd4b5..2dbd7f3 100644 --- a/src/jellyfin.py +++ b/src/jellyfin.py @@ -1,5 +1,6 @@ -import asyncio, aiohttp, traceback +import asyncio, aiohttp, traceback, os from math import floor +from dotenv import load_dotenv from src.functions import logger, search_mapping, contains_nested from src.library import ( @@ -10,6 +11,8 @@ combine_watched_dicts, ) +load_dotenv(override=True) + def get_movie_guids(movie): if "ProviderIds" in movie: @@ -70,6 +73,12 @@ class Jellyfin: def __init__(self, baseurl, token): self.baseurl = baseurl self.token = token + self.timeout = aiohttp.ClientTimeout( + total = int(os.getenv("REQUEST_TIMEOUT", 300)), + connect=None, + sock_connect=None, + sock_read=None, + ) if not self.baseurl: raise Exception("Jellyfin baseurl not set") @@ -130,7 +139,7 @@ async def get_users(self): users = {} query_string = "/Users" - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=self.timeout) as session: response = await self.query(query_string, "get", session) # If response is not empty @@ -156,7 +165,7 @@ async def get_user_library_watched( 0, ) - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=self.timeout) as session: # Movies if library_type == "Movie": user_watched[user_name][library_title] = [] @@ -404,7 +413,7 @@ async def get_users_watched( tasks_watched = [] tasks_libraries = [] - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=self.timeout) as session: libraries = await self.query(f"/Users/{user_id}/Views", "get", session) for library in libraries["Items"]: library_id = library["Id"] @@ -545,7 +554,7 @@ async def update_user_watched( f"Jellyfin: mark list\nShows: {videos_shows_ids}\nEpisodes: {videos_episodes_ids}\nMovies: {videos_movies_ids}", 1, ) - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=self.timeout) as session: if videos_movies_ids: jellyfin_search = await self.query( f"/Users/{user_id}/Items" @@ -829,7 +838,7 @@ async def update_watched( ): try: tasks = [] - async with aiohttp.ClientSession() as session: + async with aiohttp.ClientSession(timeout=self.timeout) as session: for user, libraries in watched_list.items(): logger(f"Jellyfin: Updating for entry {user}, {libraries}", 1) user_other = None