Command-line tools to make working with wikidata zuper easy.
For most, those tools are just wikidata-sdk functions wrapped for the command-line needs.
npm install -g wikidata-cliInstalling globally allows to make those commands accessible from your shell $PATH
Working with Wikidata, we often end up with obscure ids. We can always look-up those ids labels on the website but that means loading pages and pages, when a small API call and parsing could return just what we need: a label
qlabel Q1103345
# => The Cluetrain ManifestoBy default, the result uses your environment local language (process.env.LANG), but we can pass a 2-letters language code as second argument
qlabel Q1103345 de
# => Cluetrain-ManifestA quick way to access the claims of an entity
# all Q2001's claims
qclaims Q2001
# or just his place of birth
qclaims Q2001 P19
# or by specifying another language than your local language
qclaims Q2001 fr
qclaims Q2001 P19 frA quick way to access an entities data
qdata Q1496This simply outputs the result of https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=Q1496, parsed to keep only what is relevant to the requested entity (here Q1496).
The output is valid json, so it lets you the possibility to pipe it to a JSON parser such as jsondepth:
qdata Q1496 | jd labels.pt
# => { language: 'pt', value: 'Fernão de Magalhães' }This one is kind of the inverse of qlabel: pass it the title of a Wikipedia article and it will return the corresponding Wikidata id
wikiqid Cantabria
# => Q3946
wikiqid New Delhi
# => Q987By default, it will look at the Wikipedia corresponding to your environment local language (process.env.LANG), but you can specify another language by passing a 2-letters language code as last argument
wikiqid science politique fr
# => Q36442You can also pass it full Wikipedia urls
wikiqid https://en.wikipedia.org/wiki/Friedrich_Nietzsche
# => Q9358A command to access the list of all Wikidata properties in a given language (by default the environment local language)
- Get the list of all Wikidata properties in your environment local language:
wdpropsOutput an JSON object of the kind:
[...]
"P2897": "identifiant Eldoblaje Movie",
"P2898": null,
"P2899": "âge minimal",
[...]
NB: properties without a label in the requested language are set to null, as you can see above for P2898 in French
- Get the list of all Wikidata properties in another language
# here swedish
wdprops svThis command first tries to find the list in the props folder (created at wikidata-cli root), and request them to query.wikidata.org if missing.
This means that after a while, your local version will miss new and updated properties: this can be solved by running wdprops reset
A command to run a SPARQL query and get its JSON output
From this SPARQL query file: ./path/to/query.rq
SELECT ?work WHERE {
?work wdt:P50 wd:Q42 .
}get its output from your terminal like so:
wdsparql ./path/to/query.rq > ./results.jsonOptions
-s, --simplify: output the results simplifed by wikidata-sdksimplifySparqlResultsfunction
wdsparql -s ./path/to/query.rq > ./simplified_results.jsonA command to apply wikidata-sdk simplifySparqlResults function function to a query results file. Kind of the isolated second step of the wdsparql --simplify command:
wdsparql ./path/to/query.rq > ./results.json
wdsparqlsimplify ./results.json > ./simplified_results.jsonCommands that got their own modules:
a command-line tool to filter a Wikidata dump by claim
a small server to edit Wikidata from the terminal:
curl -X POST http://localhost:4115/claim -d 'entity=Q4115189&property=P2002&value=Zorg'