#!/usr/bin/env python # person2metadata.py - given the Wikidata QNumber of a person, output metadata in the form of json about them # Eric Lease Morgan # (c) University of Notre Dame; distributed under a GNU Public License # January 4, 2025 - first cut; after learning of Mark's death # April 6, 2025 - added a great deal of extra content; "Thank you, Susan Hoover!" # configure ENDPOINT = 'https://query.wikidata.org/sparql' TEMPLATE = """ SELECT DISTINCT ?label ?description ?dateBirth ?dateDeath ?country ?gender ?imageURL (GROUP_CONCAT(DISTINCT ?occupationLabel; separator=";") AS ?occupations) WHERE { wd:##QNUMBER## rdfs:label ?label . wd:##QNUMBER## schema:description ?description . OPTIONAL {wd:##QNUMBER## wdt:P18 ?imageURL .} OPTIONAL {wd:##QNUMBER## wdt:P21 ?gender .} OPTIONAL {wd:##QNUMBER## wdt:P27 ?country .} OPTIONAL {wd:##QNUMBER## wdt:P106 ?occupation .} OPTIONAL {wd:##QNUMBER## wdt:P569 ?dateBirth .} OPTIONAL {wd:##QNUMBER## wdt:P570 ?dateDeath .} FILTER (langMatches(lang(?label), 'en')) FILTER (langMatches(lang(?description), 'en')) SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' . ?occupation rdfs:label ?occupationLabel . } } GROUP BY ?label ?description ?dateBirth ?dateDeath ?country ?gender ?imageURL LIMIT 1 """ # require from requests import get from json import dumps from sys import argv, exit # get input if len( argv ) != 2 : exit( "Usage: " + argv[ 0 ] + " " ) qnumber = argv[ 1 ] # create a query and submit it query = TEMPLATE.replace( '##QNUMBER##', qnumber ) response = get( ENDPOINT, params={ 'format':'json', 'query':query } ) # output and done print( dumps( response.json() ) ) exit()