Skip to content

Instantly share code, notes, and snippets.

@jrzmurray
Created February 15, 2022 16:02
Show Gist options
  • Save jrzmurray/d14611220de2349d4c1d9cfc83a85257 to your computer and use it in GitHub Desktop.
Save jrzmurray/d14611220de2349d4c1d9cfc83a85257 to your computer and use it in GitHub Desktop.
Download Amazon Kindle library listing from browser console @ read.amazon.com to CSV for Goodreads import
// init
let xhr = new XMLHttpRequest()
let domain = 'https://read.amazon.com/'
let items = []
let csvData = ""
// function
function getItemsList(paginationToken = null, callback) {
let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + ( paginationToken ? '&paginationToken=' + paginationToken : '' ) + '&sortType=recency&querySize=50'
xhr.open('GET', url, async=false)
console.log("Loading from " + paginationToken);
// request result
xhr.onreadystatechange = function() {
switch ( xhr.readyState ) {
case 0:
console.log('uninitialized')
break
case 1:
//console.log('loading...')
break
case 4:
if(xhr.status == 200) {
let data = xhr.responseText
data = JSON.parse(data)
if(data.itemsList) {
items.push(...data.itemsList)
}
if(data.paginationToken) {
if (typeof callback === "function") {
// apply() sets the meaning of "this" in the callback
callback(data.paginationToken);
}
} else {
return null;
}
} else {
console.log('Failed')
}
break
}
}
xhr.send()
}
var c = "0";
while ( c !== null ) {
if ( c != null ) {
//console.log("Invoking with c = " + c);
if ( items.length <= c ) {
getItemsList(c, function(t) {
c = t;
//console.log("Set c to " + c);
});
} else {
break;
}
}
}
// to csv
items.forEach(item => {
csvData += '"' + item.asin + '","' + item.title.replaceAll('"', '""') + '","' + item.authors.join(';').replace(/:$/, '').replaceAll(':', "; ") + '"\n'
})
window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(csvData)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment