Last active
November 22, 2023 09:27
-
-
Save nichtich/140ac06a83c6cfbbbb46107c2d4d4a79 to your computer and use it in GitHub Desktop.
Query Wikidata via SPARQL from command line
This file contains 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
#!/bin/bash | |
# | |
# Run a Wikidata query from command line | |
# | |
# Usage examples: | |
# | |
# wdquery "DESCRIBE wd:Q42" | |
# wdquery - xml < query.sparql | |
# wdquery query.sparql json | |
# | |
QUERY=${1--} # SPARQL query or file | |
FORMAT=${2-json} # output format | |
ENDPOINT=https://query.wikidata.org/sparql | |
# make sure query is written to a file | |
if ! [ -f "$QUERY" ] | |
then | |
TMPFILE="$(mktemp)" | |
if [ "$QUERY" == "-" ] | |
then | |
# read query from STDIN | |
cat > "$TMPFILE" | |
else | |
# write query to file | |
echo "$QUERY" > "$TMPFILE" | |
fi | |
QUERY="$TMPFILE" | |
fi | |
curl -s -X GET -F "query=@$QUERY" "$ENDPOINT?format=$FORMAT" | |
[ -z "$TMPFILE" ] || rm "$TMPFILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment