Created
December 31, 2020 23:03
-
-
Save tternes/b32e0b6540314b2804a3f3d032610524 to your computer and use it in GitHub Desktop.
Simple script to resolve Google Books IDs in Reading List export files
This file contains hidden or 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
// 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) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a workaround for Reading List's UX challenge w/ importing Goodreads exports (and losing artwork).