Skip to content

Instantly share code, notes, and snippets.

@julians
Forked from jkubecki/ExportKindle.js
Last active August 3, 2020 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save julians/3a30006efa0b4fb63893a2863c9b7017 to your computer and use it in GitHub Desktop.
Save julians/3a30006efa0b4fb63893a2863c9b7017 to your computer and use it in GitHub Desktop.
Amazon Kindle Export
/*
The following data should be run in the console while viewing the page https://read.amazon.com/
It will export a CSV file called "download" which can (and should) be renamed with a .csv extension
modified version of the original script: https://gist.github.com/jkubecki/d61d3e953ed5c8379075b5ddd8a95f22
changes:
* adds a column indicating samples (EBSP)
* updates the database version
* escapes double quotes instead of stripping them
*/
var db = openDatabase("K4W", "3", "thedatabase", 1024 * 1024);
getAmazonCsv = function() {
// Set header for CSV export line - change this if you change the fields used
var csvData = "ASIN,Title,Authors,PurchaseDate,Sample";
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM bookdata;", [], function(tx, results) {
var len = results.rows.length;
for (i = 1; i < len; i++) {
console.log(results.rows.item(i));
// Get the data
var asin = results.rows.item(i).asin;
var title = results.rows.item(i).title;
var authors = JSON.parse(results.rows.item(i).authors);
var purchaseDate = new Date(
results.rows.item(i).purchaseDate
).toLocaleDateString();
var isSample = results.rows.item(i).contentType == "EBSP";
// Escape double quotes from titles to not interfere with CSV double-quotes
title = title.replace(/"/g, '""');
// Concatenate the authors list - uncomment the next line to get all authors separated by ";"
var authorList = authors.join(";");
// OR Take only first author - comment the next line if you uncommented the previous one
// var authorList = authors[0];
// Write out the CSV line
csvData += `
"${asin}","${title}","${authorList}","${purchaseDate}","${isSample}"`;
}
// "Export" the data
window.location =
"data:text/csv;charset=utf8," + encodeURIComponent(csvData);
console.log("Sample Row:");
console.log(results.rows.item(1));
});
});
};
getAmazonCsv();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment