Last active
August 6, 2021 14:34
-
-
Save thisismattmiller/dc785cd975931480ace2da41e61a80af to your computer and use it in GitHub Desktop.
Example using python to interact with Carnegie Hall's SPARQL endpoint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import json | |
| url = "http://data.carnegiehall.org/sparql/" | |
| sparql = """ | |
| #Find works by string in the title (case-insensitive) | |
| PREFIX dcterms: <http://purl.org/dc/terms/> | |
| PREFIX foaf: <http://xmlns.com/foaf/0.1/> | |
| PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | |
| select distinct ?instance ?composer ?title where { | |
| ?instance a <http://schema.org/MusicComposition> ; | |
| dcterms:creator ?creator ; | |
| rdfs:label ?title . | |
| ?creator foaf:name ?composer | |
| filter regex(?title, "Piano Sonata", "i") | |
| } | |
| limit 50 | |
| """ | |
| headers = { | |
| 'Accept' : 'application/json', | |
| 'User-Agent': 'Matt Miller - Test Script ' | |
| } | |
| data = { | |
| 'query': sparql | |
| } | |
| r = requests.post(url, headers=headers, data=data) | |
| data = json.loads(r.text) | |
| for result in data['results']['bindings']: | |
| if 'composer' in result and 'instance' in result and 'title' in result: | |
| print(result['composer']['value'], result['title']['value'], result['instance']['value']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment