Skip to content

Instantly share code, notes, and snippets.

@tternes
Created December 31, 2020 23:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tternes/b32e0b6540314b2804a3f3d032610524 to your computer and use it in GitHub Desktop.
Save tternes/b32e0b6540314b2804a3f3d032610524 to your computer and use it in GitHub Desktop.
Simple script to resolve Google Books IDs in Reading List export files
// NOTE: filenames hardcoded!
const got = require('got')
const csv = require('csv')
const fs = require('fs')
async function fetchGoogleBookId(isbn) {
const queryUrl = `https://www.googleapis.com/books/v1/volumes?q=isbn:${isbn}`
const response = await got(queryUrl)
const json = JSON.parse(response.body)
if (json.totalItems > 0) {
return json.items[0].id
}
return null
}
const processFile = async () => {
records = []
const writer = fs
.createWriteStream('./output.csv')
const parser = fs
.createReadStream(`./file.csv`)
.pipe(parse());
const stringify = csv.stringify()
stringify.pipe(writer)
for await (const record of parser) {
const isbn = record[0]
if (record[1] == '' && record[0] != '') {
record[1] = await fetchGoogleBookId(isbn)
console.log(`fetch googleBooksId for ${isbn}:`, record[1])
}
records.push(record)
stringify.write(record)
}
writer.close()
return records
}
processFile()
.then((records) => {
console.log(records.length)
})
@tternes
Copy link
Author

tternes commented Dec 31, 2020

This is a workaround for Reading List's UX challenge w/ importing Goodreads exports (and losing artwork).

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