Created
July 25, 2020 06:32
-
-
Save hubgit/38ce7c373a64f5609fa291262ab1e54b to your computer and use it in GitHub Desktop.
Search Wikidata for a book by ISBN, then use Citation.js to fetch the metadata
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
const Cite = require('citation-js') | |
const isbn = '978-1-4088-4564-6' | |
// search ISBN-13 or ISBN-10 (note hyphens and case-sensitivity) | |
const sparql = ` | |
SELECT DISTINCT ?subject WHERE { | |
?subject wdt:P31 wd:Q7725634. | |
{ ?subject wdt:P212 '${isbn}'. } UNION { ?subject wdt:P957 '${isbn}'. } | |
} | |
` | |
const url = | |
'https://query.wikidata.org/sparql?format=json&query=' + | |
encodeURIComponent(sparql) | |
console.log(url) | |
fetch(url) | |
.then(response => response.json()) | |
.then(data => data.results.bindings[0].subject.value.split('/').pop()) | |
.then(Cite.async) | |
.then(item => { | |
console.log(item.format('data')) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment