Sending items to the kyoo api

This commit is contained in:
Zoe Roux 2023-03-18 21:52:01 +09:00
parent 55da64a702
commit 89fbb3a3cf
2 changed files with 17 additions and 3 deletions

View File

@ -1,6 +1,8 @@
from dataclasses import dataclass, field
import os
from dataclasses import asdict, dataclass, field
from datetime import datetime
from typing import Optional
from .genre import Genre
from .status import Status
@ -28,3 +30,9 @@ class Movie:
translations: dict[str, MovieTranslation] = field(default_factory=dict)
def to_kyoo(self):
# For now, the API of kyoo only support one language so we remove the others.
default_language = os.environ["LIBRARY_LANGUAGES"].split(',')[0]
return { **asdict(self), **asdict(self.translations[default_language]) }

View File

@ -2,17 +2,18 @@ import asyncio
import logging
from pathlib import Path
from guessit import guessit
from themoviedb.routes.base import ClientSession
from themoviedb.routes.base import ClientSession, os
from providers.provider import Provider
class Scanner:
def __init__(self, client: ClientSession, languages: list[str]) -> None:
self._client = client
self.provider = Provider.get_all(client)[0]
self.languages = languages
async def scan(self, path: str):
videos = filter(lambda p: p.is_file(), Path(path).rglob("*"))
await asyncio.gather(*map(self.identify, videos))
await asyncio.gather(*map(self.identify, videos), return_exceptions=True)
async def identify(self, path: Path):
raw = guessit(path)
@ -24,7 +25,12 @@ class Scanner:
if raw["type"] == "movie":
movie = await self.provider.identify_movie(raw["title"], raw.get("year"), language=self.languages)
logging.debug("Got movie: %s", movie)
await self.post("movies", data=movie.to_kyoo())
elif raw["type"] == "episode":
pass
else:
logging.warn("Unknown video file type: %s", raw["type"])
async def post(self, path: str, *, data: object):
async with self._client.post(f"{os.environ['KYOO_URL']}/{path}", json=data) as r:
r.raise_for_status()