Skip to content

Instantly share code, notes, and snippets.

@nichtich
Last active November 22, 2023 09:27
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 nichtich/140ac06a83c6cfbbbb46107c2d4d4a79 to your computer and use it in GitHub Desktop.
Save nichtich/140ac06a83c6cfbbbb46107c2d4d4a79 to your computer and use it in GitHub Desktop.
Query Wikidata via SPARQL from command line
#!/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