Skip to content

Instantly share code, notes, and snippets.

@jamtur01
Forked from jkubecki/ExportKindle.js
Last active June 19, 2020 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamtur01/737047913c012e6afa5e6b1cb4eb379d to your computer and use it in GitHub Desktop.
Save jamtur01/737047913c012e6afa5e6b1cb4eb379d 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
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,ISBN,Title,Authors,PurchaseDate\n";
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM bookdata;', [], function(tx, results) {
var len = results.rows.length;
console.log(results)
for (i = 1; i < len; i++) {
// Get the data
var asin = results.rows.item(i).asin;
var isbn = results.rows.item(i).isbn13;
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();
// Remove 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 + '","' + isbn + '","' + title + '","' + authorList + '","' + purchaseDate + '"\n'
}
// "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