Skip to content

Instantly share code, notes, and snippets.

@CarlQLange
Created September 27, 2017 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlQLange/2733a8de1d57b3e0712e03532451082d to your computer and use it in GitHub Desktop.
Save CarlQLange/2733a8de1d57b3e0712e03532451082d to your computer and use it in GitHub Desktop.
WikiDataQuery = Function[query,
ImportString[
URLRead[
HTTPRequest[<|
"Scheme" -> "https",
"Domain" -> "query.wikidata.org",
"Path" -> "bigdata/namespace/wdq/sparql",
"Query" -> URLQueryEncode[<|"query" -> query|>]
|>, <|
"Headers" -> <|"Accept" -> "text/csv"|>
|>],
"Body"],
"Dataset", HeaderLines -> 1]
];
(* eg
recentcardinals =
WikiDataQuery[
"SELECT ?link ?linkLabel ?ageAtDeath ?dateOfBirth ?dateOfDeath \
WHERE {
?link wdt:P31 wd:Q5.
?link p:P39 _:b2.
_:b2 ps:P39 wd:Q45722.
OPTIONAL { ?link wdt:P569 ?dateOfBirth. }
OPTIONAL { ?link wdt:P570 ?dateOfDeath. }
BIND((YEAR(?dateOfDeath)) - (YEAR(?dateOfBirth)) AS ?ageAtDeath) \
.
FILTER (?dateOfBirth >= \"1900-01-01T00:00:00Z\"^^xsd:dateTime) .
SERVICE wikibase:label { bd:serviceParam wikibase:language \
\"en\". }
}"]
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment