[feat] wikidata: add mastodon, peertube and Lemmy accounts to infobox

This commit is contained in:
Popolon 2025-01-17 22:35:10 +01:00 committed by Bnyro
parent e4f8f0483f
commit 60c5fc67bf

View file

@ -60,6 +60,9 @@ WIKIDATA_PROPERTIES = {
'P2002': 'Twitter',
'P2013': 'Facebook',
'P2003': 'Instagram',
'P4033': 'Mastodon',
'P11947': 'Lemmy',
'P12622': 'PeerTube',
}
# SERVICE wikibase:mwapi : https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual/MWAPI
@ -363,8 +366,8 @@ def get_attributes(language):
def add_label(name):
attributes.append(WDLabelAttribute(name))
def add_url(name, url_id=None, **kwargs):
attributes.append(WDURLAttribute(name, url_id, kwargs))
def add_url(name, url_id=None, url_path_prefix=None, **kwargs):
attributes.append(WDURLAttribute(name, url_id, url_path_prefix, kwargs))
def add_image(name, url_id=None, priority=1):
attributes.append(WDImageAttribute(name, url_id, priority))
@ -476,6 +479,11 @@ def get_attributes(language):
add_url('P2013', url_id='facebook_profile')
add_url('P2003', url_id='instagram_profile')
# Fediverse
add_url('P4033', url_path_prefix='/@') # Mastodon user
add_url('P11947', url_path_prefix='/c/') # Lemmy community
add_url('P12622', url_path_prefix='/c/') # PeerTube channel
# Map
attributes.append(WDGeoAttribute('P625'))
@ -589,25 +597,40 @@ class WDLabelAttribute(WDAttribute):
class WDURLAttribute(WDAttribute):
"""
:argument url_id: ID matching one key in 'external_urls.json' for converting
IDs to full URLs
:argument url_path_prefix: Path prefix if the values are of format id@domain.
If provided, value are rewritten to 'https://<domain><url_path_prefix><id>'.
"""
HTTP_WIKIMEDIA_IMAGE = 'http://commons.wikimedia.org/wiki/Special:FilePath/'
__slots__ = 'url_id', 'kwargs'
__slots__ = 'url_id', 'url_path_prefix', 'kwargs'
def __init__(self, name, url_id=None, kwargs=None):
def __init__(self, name, url_id=None, url_path_prefix=None, kwargs=None):
super().__init__(name)
self.url_id = url_id
self.url_path_prefix = url_path_prefix
self.kwargs = kwargs
def get_str(self, result, language):
value = result.get(self.name + 's')
if self.url_id and value is not None and value != '':
if not value:
return None
value = value.split(',')[0]
if self.url_id:
url_id = self.url_id
if value.startswith(WDURLAttribute.HTTP_WIKIMEDIA_IMAGE):
value = value[len(WDURLAttribute.HTTP_WIKIMEDIA_IMAGE) :]
url_id = 'wikimedia_image'
return get_external_url(url_id, value)
if self.url_path_prefix:
[accountId, domain] = value.split('@')
return f"https://{domain}{self.url_path_prefix}{accountId}"
return value