mirror of
https://github.com/Xevion/indexer-analyze.git
synced 2025-12-06 01:15:20 -06:00
Process each series episode in task group, add semaphore to limit request concurrency
This commit is contained in:
52
sonarr.py
52
sonarr.py
@@ -2,43 +2,53 @@ import httpx
|
||||
from config import SONARR_URL, SONARR_API_KEY, logger
|
||||
|
||||
|
||||
# Add a global semaphore for concurrency limiting
|
||||
semaphore = None
|
||||
|
||||
|
||||
async def with_limit(coro):
|
||||
global semaphore
|
||||
if semaphore is None:
|
||||
raise RuntimeError(
|
||||
"Semaphore not initialized. Call set_concurrency_limit first."
|
||||
)
|
||||
async with semaphore:
|
||||
return await coro
|
||||
|
||||
|
||||
def set_concurrency_limit(limit: int):
|
||||
global semaphore
|
||||
import anyio
|
||||
|
||||
semaphore = anyio.Semaphore(limit)
|
||||
|
||||
|
||||
async def get_all_series(client: httpx.AsyncClient):
|
||||
"""
|
||||
Fetch all series from Sonarr.
|
||||
"""
|
||||
url = f"{SONARR_URL}/api/v3/series"
|
||||
resp = await client.get(url)
|
||||
resp = await with_limit(client.get(f"{SONARR_URL}/api/v3/series"))
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def get_series(client: httpx.AsyncClient, series_id: int):
|
||||
"""
|
||||
Fetch a single series by ID from Sonarr.
|
||||
"""
|
||||
url = f"{SONARR_URL}/api/v3/series/{series_id}"
|
||||
resp = await client.get(url)
|
||||
resp = await with_limit(client.get(f"{SONARR_URL}/api/v3/series/{series_id}"))
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def get_episodes_for_series(client: httpx.AsyncClient, series_id: int):
|
||||
"""
|
||||
Fetch all episodes for a given series from Sonarr.
|
||||
"""
|
||||
url = f"{SONARR_URL}/api/v3/episode?seriesId={series_id}"
|
||||
resp = await client.get(url)
|
||||
resp = await with_limit(
|
||||
client.get(f"{SONARR_URL}/api/v3/episode?seriesId={series_id}")
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
async def get_history_for_episode(client: httpx.AsyncClient, episode_id: int):
|
||||
"""
|
||||
Fetch history for a given episode from Sonarr.
|
||||
"""
|
||||
resp = await client.get(
|
||||
SONARR_URL + "/api/v3/history",
|
||||
params={"episodeId": episode_id, "pageSize": 100},
|
||||
resp = await with_limit(
|
||||
client.get(
|
||||
SONARR_URL + "/api/v3/history",
|
||||
params={"episodeId": episode_id, "pageSize": 100},
|
||||
)
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
Reference in New Issue
Block a user