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
  • Star 28 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • 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)
@darryllee
Copy link

This is awesome, @usayamadx. Thank you for writing this!

One bug: I have this book in my collection, which has a ridiculously long title that happens to include a backslashed double-quote in it. Your script got confused by that:

"title": "The Awkward Thoughts of W. Kamau Bell: Tales of a 6' 4\", African American, Heterosexual, Cisgender, Left-Leaning, Asthmatic, Black and Proud Blerd, Mama's Boy, Dad, and Stand-Up Comedian",

Feature request:

It would be awesome if your export could include all attributes for each book. So then:

  • authors[0] (Out of my 723 books there was never more than one author. Even for books with multiple authors they use colons. Bad Amazon!)
  • webReaderUrl (this might be useful if people want to use the data as a personal bookmark list to the web Reader)
  • productUrl (this actually is a link to cover images, which is actually really handy. Like I mentioned, these can be displayed in Google Sheets, etc.)
  • percentageRead (people like to know if they've finished books, although I don't think this data is actually valid. Just tested, and NOPE. Maybe someday Amazon will fix this?)
  • resourceType (perhaps people would like to distinguish EBOOK from EBOOK_SAMPLE? Those were the only two options I found.
  • originType (also only found two options in my collection: PRIME and PURCHASE. Might be useful. Wish it also included LIBRARY, but alas, no.)

I would try fixing this myself and creating a pull request, but hey, this is just a gist. :-} LMK if you make it into a real repo.

@usayamadx
Copy link
Author

@darryllee OH! Thank you, your nice comment!!
I will think the best solution.

@readytheory
Copy link

Thank you @usayamadx. This worked for me, got all my stuff (about 1500). In case anyone else reading was unsure like me how you use this: copy the text (github "raw" button is one way), login to read.amazon.com, hit F12 to get developer tools, find the Console, paste the code into the console.... and the script will run and download the contents.

Thanks @darryllee for "this good comment"

I'm interested in parsing my notes, and one way in is https://read.amazon.com/notebook... if anyone does/knows of any work on this, would be grateful for info.

Background: a 2016 answer to a 2010 Stack overflow answer: https://stackoverflow.com/a/39958932/514608

@betsy-2063
Copy link

betsy-2063 commented Sep 28, 2021

Thanks, @usayamadx -- it worked beuatifully for me, and I'm a Java novice. I too would like more details about my books (like author, purchase date, read/unread, Prime vs. purchased). Have you had a chance to think about any solutions??

@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