images: portraits and logos from Wikimedia Commons with author and licence
New adapter wikimedia.images: Wikidata P18 for people without an official photo, P154 logos for parties and organisations matched by Wikidata id, Firmenbuch (P5285) or ZVR number (P6279). Licence data from the Commons file page goes into image_credit (keyed by image URL); official Parliament/EP photos are never replaced. Web: logos on party cards, party, organisation and organisation-list pages, credit lines under portraits and logos, image proxy allows upload.wikimedia.org. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5640ed653b
commit
b761517386
22 files changed
+484
-17
No files matched your search
@@ -25,6 +25,7 @@ from politrack.core.records import (
|
||||
Document,
|
||||
Event,
|
||||
Evidence,
|
||||
Image,
|
||||
Legal,
|
||||
Money,
|
||||
OrgRecord,
|
||||
@@ -82,6 +83,8 @@ class Loader:
|
||||
return self.post_removed(record)
|
||||
if isinstance(record, ArchiveCopy):
|
||||
return self.archive_copy(record)
|
||||
if isinstance(record, Image):
|
||||
return self.image(record)
|
||||
raise TypeError(f"unknown record type {type(record).__name__}")
|
||||
|
||||
# -- documents --------------------------------------------------------------------
|
||||
@@ -244,6 +247,33 @@ class Loader:
|
||||
self.ctx.stats["social_accounts"] += 1
|
||||
return row["id"]
|
||||
|
||||
def image(self, rec: Image) -> int | None:
|
||||
"""Portrait or logo from Commons: only where no official image exists (or a Commons one is replaced)."""
|
||||
third_party = "(col IS NULL OR col LIKE 'https://upload.wikimedia.org/%%')"
|
||||
if rec.role == "photo":
|
||||
entity_id = self.resolver.person(rec.person)
|
||||
table, col = "person", "photo_url"
|
||||
else:
|
||||
entity_id = self.resolver.org(rec.org, create=False)
|
||||
table, col = "organization", "logo_url"
|
||||
if entity_id is None:
|
||||
self.ctx.stats["images_unresolved"] += 1
|
||||
return None
|
||||
self.conn.execute(
|
||||
"""INSERT INTO image_credit (url, page_url, credit, license, license_url, source_slug)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)
|
||||
ON CONFLICT (url) DO UPDATE SET page_url = EXCLUDED.page_url, credit = EXCLUDED.credit,
|
||||
license = EXCLUDED.license, license_url = EXCLUDED.license_url, updated_at = now()""",
|
||||
(rec.url, rec.page_url, rec.credit, rec.license, rec.license_url, self.ctx.spec.slug),
|
||||
)
|
||||
updated = self.conn.execute(
|
||||
f"UPDATE {table} SET {col} = %s WHERE id = %s AND {third_party.replace('col', col)} "
|
||||
f"AND {col} IS DISTINCT FROM %s",
|
||||
(rec.url, entity_id, rec.url),
|
||||
).rowcount
|
||||
self.ctx.stats["images_set" if updated else "images_kept"] += 1
|
||||
return entity_id
|
||||
|
||||
# -- standalone facets ------------------------------------------------------------
|
||||
|
||||
def vote_event(self, rec: VoteEvent) -> int:
|
||||
|
||||
@@ -121,6 +121,20 @@ class SocialAccount:
|
||||
verified: bool = False # True only for official sources (parliament profile, official website)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Image:
|
||||
"""A portrait (person) or logo (organization) from a freely licensed source, with its attribution. Fills the
|
||||
entity's photo_url / logo_url only if it has no official image."""
|
||||
url: str # the image (thumbnail) shown on the site
|
||||
role: Literal["photo", "logo"]
|
||||
page_url: str | None = None # description page with the full licence information
|
||||
credit: str | None = None
|
||||
license: str | None = None
|
||||
license_url: str | None = None
|
||||
person: PersonRef | None = None
|
||||
org: OrgRef | None = None
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------------------------
|
||||
# Facets (structured payload of a claim)
|
||||
# --------------------------------------------------------------------------------------------
|
||||
@@ -273,7 +287,8 @@ class ArchiveCopy:
|
||||
archive_url: str
|
||||
|
||||
|
||||
Record = Union[PersonRecord, OrgRecord, SocialAccount, VoteEvent, Event, Claim, Document, PostRemoved, ArchiveCopy]
|
||||
Record = Union[PersonRecord, OrgRecord, SocialAccount, VoteEvent, Event, Claim, Document, PostRemoved, ArchiveCopy,
|
||||
Image]
|
||||
|
||||
|
||||
def to_jsonable(obj: Any) -> Any:
|
||||
|
||||
Reference in new issue
Block a user