Skip to content

Instantly share code, notes, and snippets.

@anvarazizov
Forked from julians/ExportKindle.js
Created August 3, 2020 20:56
Show Gist options
  • Save anvarazizov/57e1fe497ff45cb930cd67203e8b04c2 to your computer and use it in GitHub Desktop.
Save anvarazizov/57e1fe497ff45cb930cd67203e8b04c2 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