depicts/depicts/artwork.py
2023-11-09 07:39:39 +01:00

151 lines
4.3 KiB
Python

"""Class to represent artwork."""
from .mediawiki import get_entity, get_entity_with_cache
from .type import Claims, Entity, Sitelinks
class Artwork:
"""Artwork."""
entity: Entity
artist_entities: list[Entity]
def __init__(self, qid: str) -> None:
"""Init."""
entity = get_entity_with_cache(qid)
assert entity
self.entity = entity
self.item_id = int(qid[1:])
sites = ["commons", "enwiki"]
self.parent_categories = {site: {} for site in sites}
@property
def image_filename(self) -> str | None:
"""Image filename."""
if "P18" in self.entity["claims"]:
f: str = self.entity["claims"]["P18"][0]["mainsnak"]["datavalue"]["value"]
return f
else:
return None
@property
def display_title(self) -> str:
"""Display title."""
return (
f"{self.en_title} ({self.qid})"
if "en" in self.entity["labels"]
else self.qid
)
@property
def url(self) -> str:
"""Wikidata item URL."""
return "https://www.wikidata.org/wiki/" + self.qid
def get_artist_entities(self) -> None:
"""Get artist entities."""
self.artist_entities = []
for artist in self.artists_claim:
artist_entity = get_entity(artist["id"])
assert artist_entity
self.artist_entities.append(artist_entity)
def artist_labels(self) -> list[str]:
"""Get artist Wikidata item labels."""
if not hasattr(self, "artist_entities"):
self.get_artist_entities()
return [artist["labels"]["en"]["value"] for artist in self.artist_entities]
@property
def commons_cats(self) -> list[str]:
return [
i["mainsnak"]["datavalue"]["value"]
for i in self.entity["claims"].get("P373", [])
]
@property
def commons_sitelink(self) -> str | None:
"""Wikimedia sitelink."""
return (
self.sitelinks["commons"]["value"] if "commons" in self.sitelinks else None
)
@property
def en_title(self) -> str:
if "en" in self.entity["labels"]:
title: str = self.entity["labels"]["en"]["value"]
return title
else:
return self.qid
@property
def artists_claim(self) -> list[dict[str, str]]:
return [
image["mainsnak"]["datavalue"]["value"]
for image in self.entity["claims"].get("P170", [])
]
@property
def artists(self) -> list[dict[str, str]]:
if not hasattr(self, "artist_entities"):
self.get_artist_entities()
items = [
image["mainsnak"]["datavalue"]["value"]
for image in self.entity["claims"].get("P170", [])
]
lookup = {artist["id"]: artist["labels"] for artist in self.artist_entities}
for item in items:
item["labels"] = lookup[item["id"]]
return items
@property
def qid(self) -> str:
"""Wikidata item QID."""
return f"Q{self.item_id}"
@property
def commons_filenames(self) -> list[str]:
"""Filenames of images on Wikimedia Commons."""
return [
image["mainsnak"]["datavalue"]["value"]
for image in self.entity["claims"].get("P18", [])
]
def commons_cat_from_sitelink(self) -> str | None:
ns = "Category:"
if not self.commons_sitelink or not self.commons_sitelink.startswith(ns):
return None
return self.commons_sitelink[len(ns) :]
@property
def enwiki_url(self) -> str | None:
"""URL for English Wikipedia article about artwork."""
enwiki = self.enwiki
assert enwiki
return (
"https://en.wikipedia.org/wiki/" + enwiki.replace(" ", "_")
if not enwiki
else None
)
@property
def sitelinks(self) -> Sitelinks:
"""Item sitelinks."""
return self.entity["sitelinks"]
@property
def claims(self) -> Claims:
"""Item claims."""
return self.entity["claims"]
@property
def enwiki(self) -> str | None:
"""Article title on English Wikipedia."""
return self.sitelinks["enwiki"]["title"] if "enwiki" in self.sitelinks else None