Skip to content

Commit

Permalink
Timeout issues (#103)
Browse files Browse the repository at this point in the history
* Add timeout support for jellyfin requests

Signed-off-by: Luigi311 <[email protected]>
  • Loading branch information
luigi311 authored Sep 25, 2023
1 parent 237e82e commit ac7f389
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
21 changes: 15 additions & 6 deletions src/jellyfin.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -10,6 +11,8 @@
combine_watched_dicts,
)

load_dotenv(override=True)


def get_movie_guids(movie):
if "ProviderIds" in movie:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand All @@ -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] = []
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac7f389

Please sign in to comment.