Skip to content

Instantly share code, notes, and snippets.

@akagr
Last active October 20, 2024 16:19
Show Gist options
  • Save akagr/f726d17d9eb36e4336fe8942c6e12ed9 to your computer and use it in GitHub Desktop.
Save akagr/f726d17d9eb36e4336fe8942c6e12ed9 to your computer and use it in GitHub Desktop.
Download kindle books from Amazon web

I was looking to try a non-kindle e-reader, but my library kept weighing me down. While I had calibre and the de-drm plugin, I found the downloaded books on my kindle were in kfx format and weren't playing well with calibre, even with the kfx input plugin.

Further, I found downloading books directly from web (Amazon > content library) did give me an azw3 file, which works well with de-drm.

To automate part of the process, I wrote a short js script. It doesn't walk through all the pages in content library, but it does download all the books currently visible on the list. Amazon lets us view 25 books per page, so I just had to switch pages, run script, repeat.

The meat of this script is constructing the url used to start a download. Device type, serial number, customer id etc. will be different for everyone, so it's best to download one book manually and check the network inspector for the request details. Apart from the book's key, all other details remain same for all the books (at least they did for me).

Notes:

  1. I take no guarantees this will work for you.
  2. I do not support piracy in any form. I used this just to move my library to another device that I own, not to distribute the files to anyone.
  3. If you don't understand any of the code or don't know what to do with it, it might be difficult for you to get this working. It goes in the 'console', which can be accessed from network tools in browser. I used firefox to run this.
/*
I was looking to try a non-kindle e-reader, but my library kept weighing me down. While I had calibre and the de-drm plugin, I found the downloaded books on my kindle were in kfx format and weren't playing well with calibre, even with the kfx input plugin.
Further, I found downloading books directly from web (Amazon > content library) did give me an azw3 file, which works well with de-drm.
To automate part of the process, I wrote a short js script. It doesn't walk through all the pages in content library, but it does download all the books currently visible on the list. Amazon lets us view 25 books per page, so I just had to switch pages, run script, repeat.
The meat of this script is constructing the url used to start a download. Device type, serial number, customer id etc. will be different for everyone, so it's best to download one book manually and check the network inspector for the request details. Apart from the book's `key`, all other details remain same for all the books (at least they did for me).
Note: I take no guarantees this will work for you. Also, I used this just to move my library to another device that I own, not to distribute the files to anyone.
Note 2: If you don't understand any of the code or don't know what to do with it, it might be difficult for you to get this working. It goes in the 'console', which can be accessed from network tools in browser. I used firefox to run this.
*/
// Akash Agrawal <[email protected]>
// To get the following details, download one book manually and check the network inspector for the request url
const FSN = "" // this is your kindle's serial
const DEVICE_TYPE = ""
const CUSTOMER_ID = ""
const timer = ms => new Promise(res => setTimeout(res, ms))
async function load () {
let books = document.querySelectorAll('input[id*="KindleEBook"]')
for (let i = low; i < high; i++) {
let book = books[i]
let id = book.id.split(':')[0]
let url =`https://cde-ta-g7g.amazon.com/FionaCDEServiceEngine/FSDownloadContent?type=EBOK&key=${id}&fsn=${FSN}&device_type=${DEVICE_TYPE}&customerId=${CUSTOMER_ID}&authPool=Amazon`
window.open(url, '_blank')
await timer(1000) // without a small wait, browser blocks popups, even if I allow them
}
}
load();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment