Proper rendering computation (god that was a pain)

This commit is contained in:
Zoe Roux 2025-05-06 02:27:31 +02:00
parent 968dc8eab9
commit fff0c0ea91
No known key found for this signature in database
3 changed files with 11 additions and 73 deletions

View File

@ -1,13 +1,6 @@
#!/usr/bin/env python3 from typing import Any, List, cast
if __name__ == "__main__":
import sys
from pathlib import Path
sys.path.append(str(Path(f"{__file__}/../../..").resolve()))
from guessit.api import default_api from guessit.api import default_api
from typing import cast, List, Any
from rebulk import Rebulk from rebulk import Rebulk
from rebulk.match import Match from rebulk.match import Match
@ -27,8 +20,7 @@ def guessit(
expected_titles: List[str] = [], expected_titles: List[str] = [],
extra_flags: dict[str, Any] = {}, extra_flags: dict[str, Any] = {},
) -> dict[str, list[Match]]: ) -> dict[str, list[Match]]:
rendering = [] return default_api.guessit(
ret = default_api.guessit(
name, name,
{ {
"episode_prefer_number": True, "episode_prefer_number": True,
@ -36,34 +28,6 @@ def guessit(
"expected_title": expected_titles, "expected_title": expected_titles,
"enforce_list": True, "enforce_list": True,
"advanced": True, "advanced": True,
"rendering": rendering,
} }
| extra_flags, | extra_flags,
) )
print(rendering)
return ret
# Only used to test locally
if __name__ == "__main__":
import sys
import json
# from providers.implementations.thexem import TheXemClient
from guessit.jsonutils import GuessitEncoder
from aiohttp import ClientSession
import asyncio
async def main():
async with ClientSession() as client:
# xem = TheXemClient(client)
advanced = any(x == "-a" for x in sys.argv)
ret = guessit(
sys.argv[1],
expected_titles=[],
extra_flags={"advanced": advanced},
)
print(json.dumps(ret, cls=GuessitEncoder, indent=4))
asyncio.run(main())

View File

@ -1,6 +1,5 @@
# Read that for examples/rules: https://github.com/pymedusa/Medusa/blob/master/medusa/name_parser/rules/rules.py # Read that for examples/rules: https://github.com/pymedusa/Medusa/blob/master/medusa/name_parser/rules/rules.py
import hashlib
from copy import copy from copy import copy
from logging import getLogger from logging import getLogger
from typing import Any, List, Optional, cast from typing import Any, List, Optional, cast
@ -187,29 +186,3 @@ class SeasonYearDedup(Rule):
year: List[Match] = matches.named("year") # type: ignore year: List[Match] = matches.named("year") # type: ignore
if len(season) == 1 and len(year) == 1 and season[0].value == year[0].value: if len(season) == 1 and len(year) == 1 and season[0].value == year[0].value:
return season return season
# class RenderingDedup(Rule):
# """Compute rendering (sha of path - version/part)
#
# Example: "One Piece (1999) v2 152 part2.mkv"
# Computes: sha("One Piece (1999) 152.mkv")
# ```
# """
#
# priority = POST_PROCESS + 100000
# consequence = AppendMatch
#
# def when(self, matches: Matches, context: dict[str, list[str]]) -> Any:
# ret = hashlib.new("sha256")
#
# value: list[Match] = sorted(
# list(matches) + matches.holes(), # type: ignore
# key=lambda m: m.start,
# )
# for m in value:
# if m.name == "part" or m.name == "version":
# continue
# ret.update(cast(str, m.raw).encode("utf-8"))
# context["rendering"] = [ret.hexdigest()]
# return [Match(start=0, end=1, value=ret.hexdigest(), raw="", name="rendering")]

View File

@ -16,11 +16,7 @@ pipeline: list[Callable[[str, Guess], Awaitable[Guess]]] = [
async def identify(path: str) -> Video: async def identify(path: str) -> Video:
raw = guessit( raw = guessit(path, expected_titles=[])
path,
expected_titles=[],
extra_flags={"advanced": True},
)
# guessit should only return one (according to the doc) # guessit should only return one (according to the doc)
title = raw.get("title", [])[0] title = raw.get("title", [])[0]
@ -34,9 +30,14 @@ async def identify(path: str) -> Video:
seasons = raw.get("season", []) seasons = raw.get("season", [])
episodes = raw.get("episode", []) episodes = raw.get("episode", [])
rendering = path[:version.start] + path[version.end:] # just strip the version & part number from the path
rendering_path = "".join(
c
for i, c in enumerate(path)
if not (version and version.start <= i < version.end)
and not (part and part.start <= i < part.end)
)
print(raw)
guess = Guess( guess = Guess(
title=cast(str, title.value), title=cast(str, title.value),
kind=cast(Literal["episode"] | Literal["movie"], kind.value), kind=cast(Literal["episode"] | Literal["movie"], kind.value),
@ -63,7 +64,7 @@ async def identify(path: str) -> Video:
return Video( return Video(
path=path, path=path,
rendering=sha256(path.encode()).hexdigest(), rendering=sha256(rendering_path.encode()).hexdigest(),
part=cast(int, part.value) if part else None, part=cast(int, part.value) if part else None,
version=cast(int, version.value) if version else 1, version=cast(int, version.value) if version else 1,
guess=guess, guess=guess,