Skip to content

Instantly share code, notes, and snippets.

@david-bakin
Last active June 28, 2022 05:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save david-bakin/255660af79c386460cdf0ee6a2a96291 to your computer and use it in GitHub Desktop.
Save david-bakin/255660af79c386460cdf0ee6a2a96291 to your computer and use it in GitHub Desktop.
Grab list of all Kindle books purchased from the currently logged in Amazon account - saves as a JSON file.
// Grab list of all kindle books purchased from the currently logged in
// Amazon account. Lets you save this list as a JSON file.
// The following data should be run in the console while viewing the page https://read.amazon.com/
// Kindle booklist fetch derived from jkubecki/ExportKindle.js
// (https://gist.github.com/jkubecki/d61d3e953ed5c8379075b5ddd8a95f22)
// console.save grabbed from DevTools Snipppets
// (https://bgrins.github.io/devtools-snippets/#console-save)
// First define the console.save function - writes the console to a file
(function(console){
console.save = function(data, filename){
if(!data) {
console.error('Console.save: No data')
return;
}
if(!filename) filename = 'console.json'
if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}
var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')
a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
})(console)
// Now open the database and grab the data
var db = openDatabase('K4W', '5', 'thedatabase', 2 * 1024 * 1024); // database version changed 2021-05
getAmazonCsv = function() {
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM bookdata;', [], function(tx, results) {
// Want to add a field to each element. Return value from
// tx.executeSql is readonly. So I convert it to an array (easy
// to iterate over) via the following roundabout technique. (I'm
// such a JavaScript novice...)
var as = Array.from(Object.values(JSON.parse(JSON.stringify(results.rows))));
console.log(as.length);
for (i = 0; i < as.length; i++) {
as[i].purchaseDateInterpreted = new Date(as[i].purchaseDate).toLocaleDateString();
}
console.save(as, 'amazon-kindle-booklist.json');
});
});
};
getAmazonCsv();
db ='';
@david-bakin
Copy link
Author

  1. In Chrome browser.
  2. Navigate to https://read.amazon.com. Log in (if necessary). Let it refresh with your booklist.
  3. Run developer tools.
  4. Copy this into a snippet (easiest way).
  5. Run the snippet.
  6. It'll pop up with a file save dialog - that's your booklist, in JSON, it wants to save.
  7. Console shows number of books fetched.

Fields for each book include ASIN, Title, Authors, and Purchase Date.

@david-bakin
Copy link
Author

Apparently read.amazon.com has changed and they're no longer using/updating the K4W database, so this code no longer works.

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