-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.py
More file actions
41 lines (29 loc) · 1.3 KB
/
timeline.py
File metadata and controls
41 lines (29 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# pip install sparqlwrapper
# https://rdflib.github.io/sparqlwrapper/
import sys
from SPARQLWrapper import SPARQLWrapper, JSON
endpoint_url = "https://query.wikidata.org/sparql"
query = """#title: Visual Timeline of exhibitions at the Sprengel Museum
#defaultView:Timeline
SELECT ?exhibition ?exhibitionLabel ?date ?image WHERE {
?exhibition wdt:P31/wdt:P279* wd:Q464980; # Instance of or subclass of exhibition
wdt:P276 wd:Q510144. # Location: Sprengel Museum
# Check for either inception or start time
OPTIONAL { ?exhibition wdt:P571 ?date. }
OPTIONAL { ?exhibition wdt:P580 ?date. }
# Fetch the image if it exists
OPTIONAL { ?exhibition wdt:P18 ?image. }
FILTER(BOUND(?date)) # Ensures a date exists for the timeline
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,de". }
}
ORDER BY ?date"""
def get_results(endpoint_url, query):
user_agent = "WDQS-example Python/%s.%s" % (sys.version_info[0], sys.version_info[1])
# TODO adjust user agent; see https://w.wiki/CX6
sparql = SPARQLWrapper(endpoint_url, agent=user_agent)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query().convert()
results = get_results(endpoint_url, query)
for result in results["results"]["bindings"]:
print(result)