Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created July 25, 2020 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubgit/38ce7c373a64f5609fa291262ab1e54b to your computer and use it in GitHub Desktop.
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
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