Skip to content

Instantly share code, notes, and snippets.

@krmax44
Last active September 4, 2019 14:10
Show Gist options
  • Save krmax44/f8f196dc35aede18ee3c2905cdc4afc7 to your computer and use it in GitHub Desktop.
Save krmax44/f8f196dc35aede18ee3c2905cdc4afc7 to your computer and use it in GitHub Desktop.
Get a random German city.
#!/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));
{
"name": "random-german-city",
"version": "1.0.0",
"license": "MIT",
"bin": "./index.js",
"dependencies": {
"axios": "^0.19.0"
}
}
@krmax44
Copy link
Author

krmax44 commented Sep 4, 2019

Usage: npx gist:f8f196dc35aede18ee3c2905cdc4afc7
Also available as a Runkit endpoint: https://676hqs3h5q82.runkit.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment