Skip to content

Commit

Permalink
Rework the scanner register check
Browse files Browse the repository at this point in the history
Store all paths already registered in memory instead of asking kyoo for
each path individually. This should help reduce high response time of
kyoo on startup.
  • Loading branch information
zoriya committed Jun 20, 2023
1 parent 08bbfd9 commit 4bffd35
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions scanner/scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aiohttp import ClientSession
from pathlib import Path
from guessit import guessit
from typing import List
from providers.provider import Provider
from providers.types.episode import Episode, PartialShow
from providers.types.season import Season, SeasonTranslation
Expand All @@ -24,34 +25,32 @@ def __init__(

async def scan(self, path: str):
logging.info("Starting the scan. It can take some times...")
self.registered = await self.get_registered_paths()
videos = filter(lambda p: p.is_file(), Path(path).rglob("*"))
# We batch videos by 20 because too mutch at once kinda DDOS everything.
for group in batch(videos, 20):
logging.info("Batch finished. Starting a new one")
await asyncio.gather(*map(self.identify, group))

async def is_registered(self, path: Path) -> bool:
async def get_registered_paths(self) -> List[Path]:
# TODO: Once movies are separated from the api, a new endpoint should be created to check for paths.
async with self._client.get(
f"{self._url}/episodes/count",
params={"path": f"eq:{path}"},
f"{self._url}/episodes",
params={"limit": 0},
headers={"X-API-Key": self._api_key},
) as r:
r.raise_for_status()
ret = await r.text()
if ret != "0":
return True
return False
ret = await r.json()
return list(x["path"] for x in ret["items"])

@log_errors
async def identify(self, path: Path):
if path in self.registered:
return

raw = guessit(path, "--episode-prefer-number")

if (
not "mimetype" in raw
or not raw["mimetype"].startswith("video")
or await self.is_registered(path)
):
if not "mimetype" in raw or not raw["mimetype"].startswith("video"):
return

logging.info("Identied %s: %s", path, raw)
Expand Down

0 comments on commit 4bffd35

Please sign in to comment.