Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Human Alive-ness API

It works by querying Wikidata via SPARQL to search for birth and death dates of a person.

Requires an exact name match. It will throw if it can't find results. It will return true if every match has a birth date and not a death date. (It will return false if a match does not have a birth date, because we assume that to mean they are a historical figure whose birthdate we don't know.)

@stevekrouse.alive("Henry Kissinger") // true (for now)

You can view some example usages / tests here: https://www.val.town/v/stevekrouse.aliveTests

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { wikidata } from "https://esm.town/v/stevekrouse/wikidata";
export async function alive(name) {
let data = await wikidata(
`SELECT ?person ?personLabel ?birth_date ?death_date WHERE {
?person rdfs:label "${name}"@en;
wdt:P31 wd:Q5 . # Ensure the entity is a human
OPTIONAL {
?person wdt:P570 ?death_date . # Try to find a date of death
}
OPTIONAL {
?person wdt:P569 ?birth_date . # Date of birth
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}`,
);
if (!data.results.bindings.length)
throw "Could not find " + name;
return data.results.bindings.every((b) => b.birth_date && !b.death_date);
}
October 23, 2023