urlinfo:imdb: add support for "CreativeWork" resources

This commit is contained in:
ducklet 2020-11-07 22:09:36 +01:00
parent d84f82768d
commit b7a683c11d

View file

@ -33,7 +33,7 @@ def thumbnail(url, width=182, height=268):
The default settings are what IMDb currently uses for desktop display. The default settings are what IMDb currently uses for desktop display.
""" """
resize = f"UY{height}" # there's also 'UX' to resize on width resize = f"UY{height}" # there's also 'UX' to resize on width
offset = "2,0" # by setting non-0 for the first value the image is fitted offset = "0,0"
crop = f"CR{offset},{width},{height}" crop = f"CR{offset},{width},{height}"
al = "AL" # not sure what this is, doesn't seem to do much but they use it. al = "AL" # not sure what this is, doesn't seem to do much but they use it.
@ -102,10 +102,10 @@ class Extracted:
genres: List[str] genres: List[str]
description: str description: str
published: date published: date
duration_s: int
rating_value: float
rating_count: int
creators: List[str] creators: List[str]
duration_s: Optional[int] = None
rating_value: Optional[float] = None
rating_count: Optional[int] = None
async def extractor(info): async def extractor(info):
@ -120,8 +120,10 @@ async def extractor(info):
return return
ld = json.loads(parser.value) ld = json.loads(parser.value)
assert ld["@context"] == "http://schema.org" and ld["@type"] == "Movie" assert ld["@context"] == "http://schema.org" and ld["@type"] in (
assert ld["aggregateRating"]["@type"] == "AggregateRating" "Movie",
"CreativeWork",
)
creators = [] creators = []
for k in "director", "creator": for k in "director", "creator":
@ -136,12 +138,17 @@ async def extractor(info):
genres=ld["genre"], genres=ld["genre"],
description=ld["description"], description=ld["description"],
published=date.fromisoformat(ld["datePublished"]), published=date.fromisoformat(ld["datePublished"]),
duration_s=parse_period(ld["duration"]),
rating_value=float(ld["aggregateRating"]["ratingValue"]),
rating_count=ld["aggregateRating"]["ratingCount"],
creators=creators, creators=creators,
) )
if "duration" in ld:
info.extracted.duration_s = parse_period(ld["duration"])
if "aggregateRating" in ld:
assert ld["aggregateRating"]["@type"] == "AggregateRating"
info.extracted.rating_value = float(ld["aggregateRating"]["ratingValue"])
info.extracted.rating_count = ld["aggregateRating"]["ratingCount"]
async def handle(message, url, info): async def handle(message, url, info):
ex = clone(info.extracted) ex = clone(info.extracted)
@ -165,10 +172,12 @@ async def handle(message, url, info):
details = [ details = [
f"🖋 {' '.join(ex.creators[:2])}", f"🖋 {' '.join(ex.creators[:2])}",
f"{pretty_duration(ex.duration_s)}",
f"⭐️ {ex.rating_value:_.01f} 10 (👤 {ex.rating_count})",
f"🏷 {' '.join(ex.genres)}",
] ]
if ex.duration_s:
details.append(f"{pretty_duration(ex.duration_s)}")
if ex.rating_count:
details.append(f"⭐️ {ex.rating_value:_.01f} 10 (👤 {ex.rating_count})")
details.append(f"🏷 {' '.join(ex.genres[:3])}")
lines = [ lines = [
f"<b>{ex.title}</b> (<b>{ex.published:%Y}</b>)", f"<b>{ex.title}</b> (<b>{ex.published:%Y}</b>)",
f"{', '.join(details)}", f"{', '.join(details)}",