Last active
May 30, 2022 08:18
-
-
Save knbknb/e0072fe562d598cf9580ee00ad368e24 to your computer and use it in GitHub Desktop.
wikidata-query-from-commandline.sh : codesnippets / examples for bash
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
#!/usr/bin/env bash | |
##### Query Wikidata Entities from bash - alternative | |
# returns only very basic graphs NOT via the SPARQL endpoint | |
# knb 2020 | |
WDURL='https://www.wikidata.org/w/api.php' | |
WDQS='action=wbgetentities&format=json&sites=enwiki' | |
WGE="$WDURL?$WDQS" | |
echo $WGE >&2 | |
#curl -s: means --silent | |
WDITEM=Solar_System | |
# returns "Solar System" | |
curl -sL "$WGE&titles=$WDITEM" | jq -r '.entities[].labels.en.value' >&2 | |
# Solar System: get the wikidata identifier ("wd"- prefix) result value: "Q544" | |
SOLSYS_ID=$(curl -sL "$WGE&titles=Solar_System&sites=enwiki" | jq -r '.entities[].id') | |
# perform query | |
curl -s "$WGE&ids=$SOLSYS_ID" | jq -r ".entities.$SOLSYS_ID" | |
# returns | |
#{ | |
# "pageid": 804, | |
# "ns": 0, | |
# "title": "Q544", | |
# "lastrevid": 1320739025, | |
# "modified": "2020-12-10T09:06:57Z", | |
# "type": "item", | |
# "id": "Q544", | |
# "labels": { | |
# "en": { | |
# "language": "en", | |
# "value": "Solar System" | |
# }, | |
# "pl": { | |
# "language": "pl", | |
# "value": "Układ Słoneczny" | |
# }, ... | |
# } | |
#} |
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
#!/usr/bin/env bash | |
# submit a complex SPARQL query to Wikidata, | |
# a CONSTRUCT query that returns a graph. | |
# Read SPARQL queryfile, POST it as "query", text/turtle results to stdout | |
curl -sL --data-urlencode "query@rockAndRollBandData.rq" \ | |
-H "Accept: text/turtle" \ | |
https://query.wikidata.org/bigdata/namespace/wdq/sparql | |
# return JSON results to file rockAndRollBands.json | |
curl -sL --data-urlencode "query@rockAndRollBandData.rq" \ | |
-H "Accept: application/json" \ | |
-o rockAndRollBands.json \ | |
https://query.wikidata.org/bigdata/namespace/wdq/sparql | |
# read in JSON results from rockAndRollBands.json, convert them to CSV on the fly. | |
< rockAndRollBands.json jq -r \ | |
'.results.bindings | map({"p":.predicate.value, "s":.subject.value, "o":.object.value})' \ | |
| jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | |
## ? | |
## Groups.csv tail -n +2 | cut -dQ -f 2 | xargs -i echo Q{} | xargs -i echo 'curl -sL "$WGE&ids={}" | jq -r ".entities.{}.labels.en.value"' | |
curl --data-urlencode "query@wikipedia-german-compsci-alive.rq" \ | |
-H "Accept: application/json" \ | |
https://query.wikidata.org/bigdata/namespace/wdq/sparql | |
< german-compsci.json jq -r \ | |
'.results.bindings | map({"p":.person.value, "n":.personLabel.value, "dob":.dateOfBirth.value, "dod":.dateOfDeath.value})' \ | |
| jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | |
# https://opendata.stackexchange.com/a/7153/5763 | |
# Querying Wikidata : WDQ vs. WDQS / SPARQL | |
# Perl commandline tool for querying Wikidata | |
# -t: add label/description, or both | |
wdq -tplanet '?planet wdt:P31 wd:Q44559; wdt:P397 ?sun' > sun-and-stars-and-exoplanets--3890.json | |
### count results | |
< sun-and-stars-and-exoplanets.json jq -r '.[]' | jq -c | wc -l | |
# convert to csv | |
< sun-and-stars-and-exoplanets--3890.json jq -r \ | |
'.results.bindings | map({"p":.planet, "pl":.planetLabel.value, "pd":.planetDescription.value, "sun":.sun.value})' \ | |
| jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' | |
< Groups.csv tail -n +2 | cut -dQ -f 2 | xargs -i echo Q{} | while read q; do wdq -tperson "$q"; done > people.json |
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
PREFIX schema: <http://schema.org/> | |
PREFIX wd: <http://www.wikidata.org/entity/> | |
PREFIX wdt: <http://www.wikidata.org/prop/direct/> | |
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | |
prefix owl: <http://www.w3.org/2002/07/owl#> | |
CONSTRUCT { | |
?band a schema:MusicGroup ; | |
schema:name ?bandName ; | |
schema:musicGroupMember ?member ; | |
schema:albums ?album . | |
?album a schema:MusicAlbum ; | |
schema:name ?albumTitle ; | |
schema:datePublished ?releaseDate . | |
?member schema:name ?memberName ; | |
schema:roleName ?roleName . | |
} | |
WHERE { | |
?band wdt:P136 wd:Q11399 ; # band has genre of rock and roll | |
rdfs:label ?bandName ; | |
wdt:P527 ?member . # band has-part ?member | |
FILTER ( lang(?bandName) = "en" ) | |
?member rdfs:label ?memberName . | |
FILTER ( lang(?memberName) = "en" ) | |
OPTIONAL { # Member's role. | |
?member p:P361 ?roleStatement . # part-of role statement. | |
?roleStatement rdf:type wikibase:BestRank ; # The best role statement! | |
pq:P2868 ?role . # subject-has-role ?role. | |
?role rdfs:label ?roleName . | |
FILTER ( lang(?roleName) = "en" ) | |
} | |
{ ?album wdt:P31 wd:Q482994 . } # instance of album (wd:Q482994) | |
UNION | |
{ ?album wdt:P31 ?albumSubclass . # or a subclass of that such as | |
?albumSubclass p:P279 wd:Q482994 . # live or compilation album | |
} | |
UNION | |
{ ?album wdt:P31 ?albumSubclass . | |
?albumSubclass p:P279 ?albumClassStatement . # subclass of | |
?albumClassStatement ps:P279 wd:Q482994 ; | |
rdf:type wikibase:BestRank . | |
} | |
?album wdt:P175 ?band ; # has performer | |
rdfs:label ?albumTitle ; | |
p:P577 ?releaseDateStatement . # publication date | |
FILTER ( lang(?albumTitle) = "en" ) | |
?releaseDateStatement ps:P577 ?releaseDate ; # release date as ISO 8601 | |
rdf:type wikibase:BestRank . # Only the best! | |
} |
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
@prefix schema: <http://schema.org/> . | |
@prefix wd: <http://www.wikidata.org/entity/> . | |
@prefix wdt: <http://www.wikidata.org/prop/direct/> . | |
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | |
@prefix sesame: <http://www.openrdf.org/schema/sesame#> . | |
@prefix owl: <http://www.w3.org/2002/07/owl#> . | |
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | |
@prefix fn: <http://www.w3.org/2005/xpath-functions#> . | |
@prefix foaf: <http://xmlns.com/foaf/0.1/> . | |
@prefix dc: <http://purl.org/dc/elements/1.1/> . | |
@prefix hint: <http://www.bigdata.com/queryHints#> . | |
@prefix bd: <http://www.bigdata.com/rdf#> . | |
@prefix bds: <http://www.bigdata.com/rdf/search#> . | |
@prefix psn: <http://www.wikidata.org/prop/statement/value-normalized/> . | |
@prefix pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/> . | |
@prefix prn: <http://www.wikidata.org/prop/reference/value-normalized/> . | |
@prefix mwapi: <https://www.mediawiki.org/ontology#API/> . | |
@prefix gas: <http://www.bigdata.com/rdf/gas#> . | |
@prefix wdtn: <http://www.wikidata.org/prop/direct-normalized/> . | |
@prefix psv: <http://www.wikidata.org/prop/statement/value/> . | |
@prefix ps: <http://www.wikidata.org/prop/statement/> . | |
@prefix pqv: <http://www.wikidata.org/prop/qualifier/value/> . | |
@prefix pq: <http://www.wikidata.org/prop/qualifier/> . | |
@prefix prv: <http://www.wikidata.org/prop/reference/value/> . | |
@prefix pr: <http://www.wikidata.org/prop/reference/> . | |
@prefix wdno: <http://www.wikidata.org/prop/novalue/> . | |
@prefix p: <http://www.wikidata.org/prop/> . | |
@prefix wikibase: <http://wikiba.se/ontology#> . | |
@prefix wdata: <http://www.wikidata.org/wiki/Special:EntityData/> . | |
@prefix wds: <http://www.wikidata.org/entity/statement/> . | |
@prefix wdv: <http://www.wikidata.org/value/> . | |
@prefix wdref: <http://www.wikidata.org/reference/> . | |
@prefix prov: <http://www.w3.org/ns/prov#> . | |
@prefix skos: <http://www.w3.org/2004/02/skos/core#> . | |
@prefix geo: <http://www.opengis.net/ont/geosparql#> . | |
@prefix geof: <http://www.opengis.net/def/geosparql/function/> . | |
@prefix mediawiki: <https://www.mediawiki.org/ontology#> . | |
@prefix ontolex: <http://www.w3.org/ns/lemon/ontolex#> . | |
@prefix dct: <http://purl.org/dc/terms/> . | |
wd:Q1814025 a schema:MusicGroup ; | |
schema:name "True Symphonic Rockestra"@en ; | |
schema:musicGroupMember wd:Q296039 ; | |
schema:albums wd:Q5158585 . | |
wd:Q5158585 a schema:MusicAlbum ; | |
schema:name "Concerto in True Minor"@en ; | |
schema:datePublished "2008-01-01T00:00:00Z"^^xsd:dateTime . | |
wd:Q296039 schema:name "James LaBrie"@en ; | |
schema:roleName "front person"@en . | |
wd:Q11036 a schema:MusicGroup ; | |
schema:name "The Rolling Stones"@en ; | |
schema:musicGroupMember wd:Q216051 ; | |
schema:albums wd:Q1139077 . | |
wd:Q1139077 a schema:MusicAlbum ; | |
schema:name "Undercover"@en ; | |
schema:datePublished "1983-11-07T00:00:00Z"^^xsd:dateTime . | |
wd:Q216051 schema:name "Charlie Watts"@en ; | |
schema:roleName "drummer"@en . | |
wd:Q11036 schema:albums wd:Q31665 . | |
wd:Q31665 a schema:MusicAlbum ; | |
schema:name "GRRR!"@en ; | |
schema:datePublished "2012-11-12T00:00:00Z"^^xsd:dateTime . | |
wd:Q11036 schema:albums wd:Q12632908 . | |
wd:Q12632908 a schema:MusicAlbum ; | |
schema:name "In Concert"@en ; | |
schema:datePublished "1982-07-21T00:00:00Z"^^xsd:dateTime . | |
wd:Q11036 schema:albums wd:Q62607106 . | |
wd:Q62607106 a schema:MusicAlbum ; | |
schema:name "Hot Rocks 1964-1971"@en ; | |
schema:datePublished "1971-12-20T00:00:00Z"^^xsd:dateTime . | |
wd:Q11036 schema:albums wd:Q1137354 . | |
wd:Q1137354 a schema:MusicAlbum ; | |
schema:name "Steel Wheels"@en ; | |
schema:datePublished "1989-08-29T00:00:00Z"^^xsd:dateTime . | |
wd:Q11036 schema:albums wd:Q62602690 . | |
wd:Q62602690 a schema:MusicAlbum ; | |
schema:name "Through the Past, Darkly (Big Hits Vol. 2)"@en ; | |
schema:datePublished "1969-11-01T00:00:00Z"^^xsd:dateTime . | |
wd:Q11036 schema:albums wd:Q63107624 . | |
wd:Q63107624 a schema:MusicAlbum ; | |
schema:name "Aftermath"@en ; | |
schema:datePublished "1966-04-15T00:00:00Z"^^xsd:dateTime . |
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
#!/usr/bin/env bash | |
# knb 202012 | |
# https://opendata.stackexchange.com/questions/5248/how-to-get-the-name-of-a-wikidata-item | |
OPTIND=1 | |
declare VERBOSE="--silent" | |
declare ID=Q19675 # Q19675 -> Louvre | |
declare LANG=es | |
declare HEADER1="User-Agent: VSCodeSparqlExecutor/1.2.1+; (github.com/danwhitfield/sparql-executor-vscode-extension; <Driller001> knb.findme@gmail.com)" | |
while getopts 'd:i:l:h:' opt | |
do | |
case "$opt" in | |
d) VERBOSE="" | |
;; | |
i) ID="${OPTARG}" # entity code we want to translate | |
;; | |
l) LANG="${OPTARG}" # natural language to translate to | |
;; | |
h) HEADER1="${OPTARG}" # user-agent header. wikidata recommends | |
;; | |
*) echo "Usage: $0 [-v] (more verbose output)" && exit 0 ;; | |
esac | |
done | |
URL="https://www.wikidata.org/w/api.php?action=wbgetentities&props=labels&languages=$LANG&format=json&ids=$ID" | |
< <(curl -sL -H "$HEADER1" "$URL") jq -r .entities.$ID.labels.$LANG.value | |
#echo "curl -sL -H '$HEADER1' '$URL' 2>/dev/null" | |
#echo "curl -sL '$URL' 2>/dev/null" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment