Last active
September 4, 2019 14:10
-
-
Save krmax44/f8f196dc35aede18ee3c2905cdc4afc7 to your computer and use it in GitHub Desktop.
Get a random German city.
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 node | |
const axios = require('axios'); | |
const sparqlQuery = `SELECT ?itemLabel | |
WHERE | |
{ | |
?item wdt:P31 wd:Q1549591. | |
?item wdt:P17 wd:Q183. | |
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | |
}`; | |
const url = 'https://query.wikidata.org/sparql?query=' + encodeURIComponent(sparqlQuery); | |
const headers = { 'Accept': 'application/sparql-results+json' }; | |
axios | |
.get(url, { headers }) | |
.then(({ data: { results: { bindings }} }) => console.log(`We're going to ${bindings[Math.floor(Math.random() * bindings.length)].itemLabel.value}!`)) | |
.catch(e => console.error(e)); |
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
{ | |
"name": "random-german-city", | |
"version": "1.0.0", | |
"license": "MIT", | |
"bin": "./index.js", | |
"dependencies": { | |
"axios": "^0.19.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
npx gist:f8f196dc35aede18ee3c2905cdc4afc7
Also available as a Runkit endpoint: https://676hqs3h5q82.runkit.sh