Making the scanner send data to the back

This commit is contained in:
Zoe Roux 2023-03-20 19:01:38 +09:00
parent 8b2dd048d3
commit 541a7c9e2b
6 changed files with 56 additions and 17 deletions

View File

@ -106,6 +106,8 @@ namespace Kyoo.Core.Controllers
/// <inheritdoc /> /// <inheritdoc />
protected override async Task Validate(Show resource) protected override async Task Validate(Show resource)
{ {
resource.Slug ??= Utility.ToSlug(resource.Title);
await base.Validate(resource); await base.Validate(resource);
if (resource.Studio != null) if (resource.Studio != null)
{ {

View File

@ -42,9 +42,10 @@ class TheMovieDatabase(Provider):
status=Status.UNKNOWN, status=Status.UNKNOWN,
studio=None, studio=None,
genres=[], genres=[],
poster=[], posters=[],
thumbnails=[], thumbnails=[],
logo=[], logos=[],
trailers=[],
) )
translation = MovieTranslation( translation = MovieTranslation(
name=movie["title"], name=movie["title"],

View File

@ -22,9 +22,10 @@ class Movie:
studio: Optional[int | str] = None studio: Optional[int | str] = None
genres: list[Genre] = field(default_factory=list) genres: list[Genre] = field(default_factory=list)
poster: list[str] = field(default_factory=list) posters: list[str] = field(default_factory=list)
thumbnails: list[str] = field(default_factory=list) thumbnails: list[str] = field(default_factory=list)
logo: list[str] = field(default_factory=list) logos: list[str] = field(default_factory=list)
trailers: list[str] = field(default_factory=list)
path: Optional[str] = None path: Optional[str] = None
# TODO: handle staff # TODO: handle staff
@ -35,4 +36,14 @@ class Movie:
def to_kyoo(self): def to_kyoo(self):
# For now, the API of kyoo only support one language so we remove the others. # For now, the API of kyoo only support one language so we remove the others.
default_language = os.environ["LIBRARY_LANGUAGES"].split(",")[0] default_language = os.environ["LIBRARY_LANGUAGES"].split(",")[0]
return {**asdict(self), **asdict(self.translations[default_language])} return {
**asdict(self),
**asdict(self.translations[default_language]),
"poster": next(iter(self.posters), None),
"thumbnail": next(iter(self.thumbnails), None),
"logo": next(iter(self.logos), None),
"trailer": next(iter(self.trailers), None),
"start_air": self.release_date,
"title": self.translations[default_language].name,
"isMovie": True,
}

View File

@ -15,7 +15,20 @@ async def main():
if not languages: if not languages:
print("Missing environment variable 'LIBRARY_LANGUAGES'.") print("Missing environment variable 'LIBRARY_LANGUAGES'.")
exit(2) exit(2)
api_key = os.environ.get("KYOO_APIKEY")
if not api_key:
api_key = os.environ.get("KYOO_APIKEYS")
if not api_key:
print("Missing environment variable 'KYOO_APIKEY'.")
exit(2)
api_key = api_key.split(",")[0]
if len(sys.argv) > 1 and sys.argv[1] == "-v": if len(sys.argv) > 1 and sys.argv[1] == "-v":
logging.basicConfig(level=logging.INFO)
if len(sys.argv) > 1 and sys.argv[1] == "-vv":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
async with ClientSession() as client: async with ClientSession() as client:
await Scanner(client, languages.split(",")).scan(path) await Scanner(client, languages=languages.split(","), api_key=api_key).scan(
path
)

View File

@ -1,4 +1,5 @@
from functools import wraps from functools import wraps
import json
import os import os
import asyncio import asyncio
import logging import logging
@ -7,6 +8,7 @@ from pathlib import Path
from guessit import guessit from guessit import guessit
from providers.provider import Provider from providers.provider import Provider
def log_errors(f): def log_errors(f):
@wraps(f) @wraps(f)
async def internal(*args, **kwargs): async def internal(*args, **kwargs):
@ -14,12 +16,16 @@ def log_errors(f):
await f(*args, **kwargs) await f(*args, **kwargs)
except Exception as e: except Exception as e:
logging.exception("Unhandled error", exc_info=e) logging.exception("Unhandled error", exc_info=e)
return internal return internal
class Scanner: class Scanner:
def __init__(self, client: ClientSession, languages: list[str]) -> None: def __init__(
self, client: ClientSession, *, languages: list[str], api_key: str
) -> None:
self._client = client self._client = client
self._api_key = api_key
self.provider = Provider.get_all(client)[0] self.provider = Provider.get_all(client)[0]
self.languages = languages self.languages = languages
@ -41,14 +47,18 @@ class Scanner:
) )
logging.debug("Got movie: %s", movie) logging.debug("Got movie: %s", movie)
await self.post("movies", data=movie.to_kyoo()) await self.post("movies", data=movie.to_kyoo())
movie.path = str(path)
elif raw["type"] == "episode": elif raw["type"] == "episode":
pass pass
else: else:
logging.warn("Unknown video file type: %s", raw["type"]) logging.warn("Unknown video file type: %s", raw["type"])
async def post(self, path: str, *, data: object): async def post(self, path: str, *, data: object):
url = os.environ.get('KYOO_URL', "http://back:5000") url = os.environ.get("KYOO_URL", "http://back:5000")
print(json.dumps(data, indent=4))
async with self._client.post( async with self._client.post(
f"{url}/{path}", json=data f"{url}/{path}", json=data, headers={"X-API-Key": self._api_key}
) as r: ) as r:
if not r.ok:
print(await r.text())
r.raise_for_status() r.raise_for_status()

View File

@ -16,13 +16,15 @@ in
]; ];
shellHook = '' shellHook = ''
# Install python modules # Install python modules
SOURCE_DATE_EPOCH=$(date +%s) SOURCE_DATE_EPOCH=$(date +%s)
if [ ! -d "${venvDir}" ]; then if [ ! -d "${venvDir}" ]; then
${pkgs.python3}/bin/python3 -m venv "${venvDir}" ${pkgs.python3}/bin/python3 -m venv ${venvDir}
fi source ${venvDir}/bin/activate
source "${venvDir}/bin/activate" export PIP_DISABLE_PIP_VERSION_CHECK=1
export PIP_DISABLE_PIP_VERSION_CHECK=1 pip install -r ${pythonPkgs} >&2
pip install -r ${pythonPkgs} else
source ${venvDir}/bin/activate
fi
''; '';
} }