Skip to content

Instantly share code, notes, and snippets.

@usayamadx
Forked from jkubecki/ExportKindle.js
Last active March 23, 2024 17:53
Show Gist options
  • Save usayamadx/9c638d9b70bc714d6dd6043fcd54085f to your computer and use it in GitHub Desktop.
Save usayamadx/9c638d9b70bc714d6dd6043fcd54085f to your computer and use it in GitHub Desktop.
Amazon Kindle Export
// init
let xhr = new XMLHttpRequest()
let domain = 'https://read.amazon.com/'
let items = []
let csvData = ""
// function
function getItemsList(paginationToken = null) {
let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + ( paginationToken ? '&paginationToken=' + paginationToken : '' ) + '&sortType=recency&querySize=50'
xhr.open('GET', url, false)
xhr.send()
}
// 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) {
getItemsList(data.paginationToken)
}
} else {
console.log('Failed')
}
break
}
}
// action
getItemsList()
// to csv
items.forEach(item => {
csvData += '"' + item.asin + '","' + item.title + '"\n'
})
window.location = 'data:text/csv;charset=utf8,' + encodeURIComponent(csvData)
@MrMikey59
Copy link

MrMikey59 commented Nov 28, 2021

@usayamadx: This is great - a BIG THANKS! I was able to download 10000 of my 17600+ books. Some minor formatting was required due to title details as mentioned above - but those are easy to fix in the spreadsheet.

Looks like the limitation on the Pagination Token is 9999 and the Query Size is limited to 50! Both prevent a complete download of the list. Has anyone else seen this limit? Any ideas on how to get the rest of the books on my list?

I'm trying to find how to modify the URL. So far I see these options:

  • &sortType=recency
  • &sortType=acquisition_asc
    Where do I get the reference to see others?

I'm currently building this reference at: https://github.com/MrMikey59/Kindle-Book-List

@nbijbi
Copy link

nbijbi commented Dec 4, 2021

I am a js novice, but this isn't working for me. on a new install of win 11, getting this error.

image

seeing some data, just titles, but can't parse this.

image

@pdollar88
Copy link

I appreciate this! This export all of my titles, but did not export any additional details like author information. Would love if that would be captured.

@nickbaldwin-monster
Copy link

nickbaldwin-monster commented Jul 26, 2022

if you'd like some of the extra properties, use this. I've commented out the percentageRead (as it always shows 0 for me), originType (same for all my books) and mangaOrComicAsin (not relevant for my stuff) - but you remove all the // preceding those lines, you'll get everything

it looks like that all authors are included in a single item in an array, but this code will deal with additional items

finally, the search order is changed to use the order of purchase - which will make it easier to identify just new items when exporting the list again later

@pdollar88 @betsy-2063 @darryllee - very late but just in case you're interested...

// init
let xhr = new XMLHttpRequest()
let domain = 'https://read.amazon.com/'
let items = []
let csvData = ""

// function
function getItemsList(paginationToken = null) {
  let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + ( paginationToken ? '&paginationToken=' + paginationToken : '' ) + '&sortType=acquisition_desc&querySize=50'
  xhr.open('GET', url, false)
  xhr.send()  
}

// 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) {
          getItemsList(data.paginationToken)
        }
      } else {
        console.log('Failed')
      }
      break
  }
}

// action
getItemsList()

// to csv
items.forEach(item => {

  let authorNames = "";
  item.authors.forEach(author => {
    let authorsList = author.split(":");
    let prevAuthor = "";
    authorsList.forEach((a, _i) => {
      // ignore duplicate name and last item (empty string)
      if (a !== prevAuthor && _i < authorsList.length - 1) {
        authorNames += a + '; '
        prevAuthor = a;
      }
    }); 
  }); 

  csvData += 
    '"' + item.asin + 
    '","' + 
    item.title + 
    '","' + 
    authorNames + 
    '","' + 
    item.webReaderUrl + 
    '","' + 
    item.productUrl + 
    '","' + 
    item.resourceType + 
    // '","' + 
    // item.percentageRead + 
    // '","' +  
    // item.originType + 
    // '","' + 
    // mangaOrComicAsin +
    '"\n'

})
window.location = 'data:text/csv;charset=utf8,' + `encodeURIComponent(csvData)

@nickbaldwin-monster
Copy link

thanks so much to @usayamadx - really appreciate this!

@AppMakerSupreme
Copy link

I improved upon @usayamadx logic and made a Chrome extension to solve this problem: Kindle Book List Exporter

You can now get a csv file in 1 click. No need to deal with any code or console.

Please let me know if it works for you.

@readytheory
@nickbaldwin-monster
@MrMikey59
@pdollar88
image

@berkeleysquare
Copy link

THANKS @usayamadx ! Awesome.
I, too, had something that prevented the download. I put it in a blob and it works great:

// init
let xhr = new XMLHttpRequest()
let domain = 'https://read.amazon.com/'
let items = []
let csvData = ""

const formatBook = item => '"' + item.asin + '","' + item.title + '","' + (item.authors || [])[0] + '"\n';

// function
function getItemsList(paginationToken = null) {
let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + ( paginationToken ? '&paginationToken=' + paginationToken : '' ) + '&sortType=recency&querySize=50'
xhr.open('GET', url, false)
xhr.send()
}

// 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) {
getItemsList(data.paginationToken)
}
} else {
console.log('Failed')
}
break
}
}

// action
getItemsList()

// to csv
items.forEach(item => {
csvData += formatBook(item);
})
const blobCSV = new Blob([csvData], {type: 'text/plain;charset=utf-8'});
blobURL = URL.createObjectURL(blobCSV);
window.location = blobURL;

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