Update scan method

This commit is contained in:
Zoe Roux 2024-04-09 00:07:31 +02:00
parent a18fa7ebad
commit 52380bcb29
No known key found for this signature in database
3 changed files with 41 additions and 30 deletions

View File

@ -14,5 +14,5 @@ async def main():
path = os.environ.get("SCANNER_LIBRARY_ROOT", "/video")
await asyncio.gather(
monitor(path, publisher),
scan(path),
scan(path, publisher, client),
)

View File

@ -1,35 +1,35 @@
import os
import re
import asyncio
from logging import getLogger
from pathlib import Path
from monitor.publisher import Publisher
from providers.kyoo_client import KyooClient
logger = getLogger(__name__)
async def scan(path: str):
async def scan(path: str, publisher: Publisher, client: KyooClient):
logger.info("Starting the scan. It can take some times...")
registered = await _get_registered_paths()
self.issues = await self.get_issues()
ignore_pattern = None
try:
ignore_pattern = re.compile(os.environ.get("LIBRARY_IGNORE_PATTERN", ""))
except Exception as e:
ignore_pattern = re.compile("")
logger.error(f"Invalid ignore pattern. Ignoring. Error: {e}")
registered = await client.get_registered_paths()
videos = [str(p) for p in Path(path).rglob("*") if p.is_file()]
deleted = [x for x in self.registered if x not in videos]
# if path in self.registered or self._ignore_pattern.match(path):
# return
#
to_register = [
p for p in videos if p not in registered and not ignore_pattern.match(p)
]
deleted = [x for x in registered if x not in videos]
# try:
# self._ignore_pattern = re.compile(
# os.environ.get("LIBRARY_IGNORE_PATTERN", "")
# )
# except Exception as e:
# self._ignore_pattern = re.compile("")
# logging.error(f"Invalid ignore pattern. Ignoring. Error: {e}")
if len(deleted) != len(self.registered):
for x in deleted:
await self.delete(x)
for x in self.issues:
if x not in videos:
await self.delete(x, "issue")
if len(deleted) != len(registered):
await asyncio.gather(*map(publisher.delete, deleted))
elif len(deleted) > 0:
logging.warning("All video files are unavailable. Check your disks.")
logger.warning("All video files are unavailable. Check your disks.")
# We batch videos by 20 because too mutch at once kinda DDOS everything.
for group in batch(iter(videos), 20):
await asyncio.gather(*map(self.identify, group))
logging.info("Scan finished.")
await asyncio.gather(*map(publisher.add, to_register))
logger.info("Scan finished.")

View File

@ -30,15 +30,26 @@ class KyooClient:
async def __aexit__(self, exc_type, exc_value, exc_tb):
await self.client.close()
async def get_issues(self) -> List[str]:
async def get_registered_paths(self) -> List[str]:
paths = None
async with self.client.get(
f"{self._url}/issues",
f"{self._url}/episodes",
params={"limit": 0},
headers={"X-API-Key": self._api_key},
) as r:
r.raise_for_status()
ret = await r.json()
return [x["cause"] for x in ret if x["domain"] == "scanner"]
paths = list(x["path"] for x in ret["items"])
async with self.client.get(
f"{self._url}/movies",
params={"limit": 0},
headers={"X-API-Key": self._api_key},
) as r:
r.raise_for_status()
ret = await r.json()
paths += list(x["path"] for x in ret["items"])
return paths
async def create_issue(self, path: str, issue: str, extra: dict | None = None):
await self.client.post(