Skip to content

Instantly share code, notes, and snippets.

@cgillinger
Last active February 19, 2025 22:18
Show Gist options
  • Save cgillinger/6b301279e519a6890cfa82883aec37d1 to your computer and use it in GitHub Desktop.
Save cgillinger/6b301279e519a6890cfa82883aec37d1 to your computer and use it in GitHub Desktop.
Kindle Library Exporter – Export Kindle Library as CSV from read.amazon.com
/**
* 📚 Kindle Library Exporter + ISBN Lookup 📚
*
* This script extracts book data (ASIN, Title, Authors) from https://read.amazon.com/kindle-library
* and searches for ISBN using Open Library API. The data is exported as a CSV file.
*
* 🚨 IMPORTANT: YOU MUST USE GOOGLE CHROME 🚨
* 🛑 Why Google Chrome?
* - Amazon Kindle Library uses advanced security restrictions.
* - Other browsers (Firefox, Edge, Safari) may block access to book data due to security policies.
* - Google Chrome allows us to extract book metadata safely using the DOM.
*
* ⚠️ IMPORTANT INSTRUCTIONS: READ BEFORE RUNNING ⚠️
* 1️⃣ Open **Google Chrome** and go to **https://read.amazon.com/kindle-library**
* 2️⃣ **Manually scroll down** until you reach the end of your Kindle Library
* 3️⃣ **Ensure all books are loaded and visible on the page**
* 4️⃣ **Click anywhere on the page** to ensure focus
* 5️⃣ Press **F12** (Windows/Linux) or **Cmd + Option + I** (Mac) to open Developer Tools
* 6️⃣ Click on the **"Console"** tab
* 7️⃣ Copy & paste this script into the console and press **Enter**
* 8️⃣ A file named **'kindle_books.csv'** will be downloaded automatically
*/
(async function() {
console.clear(); // Clears the console for better readability
console.log("📚 Kindle Library Exporter: Starting...");
// CSV Header
let csvData = "ASIN,Title,Authors,ISBN-13,ISBN-10\n";
// Function to fetch ISBN from Open Library API
async function fetchISBN(title, author) {
console.log(`📚 Fetching ISBN for "${title}" by ${author}...`);
let cleanTitle = title.replace(/\s*\(.*?\)\s*/g, ""); // Remove text in parentheses
let searchUrl = `https://openlibrary.org/search.json?title=${encodeURIComponent(cleanTitle)}&author=${encodeURIComponent(author)}&limit=5`;
try {
// Fetch search results
let searchResponse = await fetch(searchUrl);
let searchData = await searchResponse.json();
if (searchData.docs.length > 0) {
console.log("✅ API Response:", searchData); // Debugging log
for (let book of searchData.docs) {
if (book.cover_edition_key) {
let editionUrl = `https://openlibrary.org/books/${book.cover_edition_key}.json`;
console.log(`🔍 Fetching edition data from: ${editionUrl}`);
// Fetch edition details
let editionResponse = await fetch(editionUrl);
let editionData = await editionResponse.json();
if (editionData.isbn_13 || editionData.isbn_10) {
let isbn13 = editionData.isbn_13 ? editionData.isbn_13[0] : "N/A";
let isbn10 = editionData.isbn_10 ? editionData.isbn_10[0] : "N/A";
console.log(`✅ Found ISBN: ${isbn13} / ${isbn10}`);
return { isbn13, isbn10 };
}
}
}
}
} catch (error) {
console.error(`❌ Error fetching ISBN for "${title}" by ${author}:`, error);
}
return { isbn13: "N/A", isbn10: "N/A" };
}
// Collect book data
let books = [];
let elements = document.querySelectorAll('[id^="title-"]');
for (let titleDiv of elements) {
let asin = titleDiv.id.replace("title-", ""); // Extract ASIN
let title = titleDiv.querySelector("p") ? titleDiv.querySelector("p").innerText.trim().replace(/"/g, '') : "Unknown";
let cleanTitle = title.replace(/\s*\(.*?\)\s*/g, ""); // Remove text in parentheses
let authorDiv = document.querySelector(`#author-${asin}`);
let authors = authorDiv && authorDiv.querySelector("p") ? authorDiv.querySelector("p").innerText.trim() : "Unknown";
// Fetch ISBN asynchronously
let { isbn13, isbn10 } = await fetchISBN(cleanTitle, authors);
books.push({ asin, title, authors, isbn13, isbn10 });
// Log progress
console.log(`✅ Processed: ${title} by ${authors} → ISBN-13: ${isbn13} | ISBN-10: ${isbn10}`);
}
// Convert to CSV
books.forEach(book => {
csvData += `"${book.asin}","${book.title}","${book.authors}","${book.isbn13}","${book.isbn10}"\n`;
});
// Create and download CSV file
let encodedUri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csvData);
let link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "kindle_books.csv");
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log("📚 Kindle Library Exporter: Done! CSV file downloaded.");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment