Help us understand the problem. What is going on with this article?

Kindle蔵書一覧を取得する方法

Kindleの蔵書が1万冊を超えてきて、そろそろ蔵書管理したくなり、
蔵書一覧を取得する方法を調べたので、まとめておく。

概要

蔵書一覧の取得方法としては大きく2つあり、
コンテンツと端末の管理ページからスクレイピングする方法と、
Kindle Cloud Readerが使っているWeb SQL Databaseのクライアント側のDBからそのまま取得する方法がある。
後者のほうが簡単なため、ここでは後者の方法について記載する。
(前者の方法が知りたい方は、https://qiita.com/yshr1982/items/072e8b44d456f6d9358bなどを参考にしてください。)

取得方法

  1. Kindle Cloud Readerにアクセス。本が大量にあると読み込みが終わるまで時間がかかるためしばらく待つ。
  2. F12を押して、デベロッパーツールを立ち上げる。
  3. Consoleタブに移動して、以下のコードを貼り付けて実行。Kindle.csvとして出力される。
getKindleCsv = function () {
    const FILE_NAME = 'Kindle.csv';
    var db = openDatabase('K4W', '3', 'thedatabase', 1024 * 1024);

    // Set CSV header
    var csvData = "ASIN,Title,Authors,PurchaseDate\n";

    db.transaction(function (tx) {
        tx.executeSql('SELECT * FROM bookdata order by title;', [], function (tx, results) {
            var len = results.rows.length;

            for (i = 0; i < len; i++) {
                var result = results.rows.item(i);
                var asin = result.asin;
                var title = result.title;
                var authors = JSON.parse(result.authors);
                var purchaseDate = new Date(result.purchaseDate).toLocaleDateString();

                // Remove double quotes and CRLF from title
                title = title.replace(/"/g, '');
                title = title.replace(/\n|\r\n|\r/g, '');
                var authorList = authors[0];

                // Write out CSV Style
                csvData += '"' + asin + '","' + title + '","' + authorList + '","' + purchaseDate + '"\n'
            }

            // Export CSV File
            var bom = new Uint8Array([0xEF, 0xBB, 0xBF]);
            var blob = new Blob([bom, csvData], {
                type: 'text/csv'
            });
            var url = (window.URL || window.webkitURL).createObjectURL(blob);
            var link = document.createElement('a');
            link.download = FILE_NAME;
            link.href = url;
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        });
    });
};

getKindleCsv();

頻繁に利用するようであれば、以下のコードをブックマークレットとして登録してクリックひとつで利用することもできる。

javascript: (function () {const FILE_NAME = 'Kindle.csv';var db = openDatabase('K4W', '3', 'thedatabase', 1024 * 1024);var csvData = "ASIN,Title,Authors,PurchaseDate\n";db.transaction(function (tx) {tx.executeSql('SELECT * FROM bookdata order by title;', [], function (tx, results) {var len = results.rows.length;for (i = 0; i < len; i++) {var result = results.rows.item(i);var asin = result.asin;var title = result.title;var authors = JSON.parse(result.authors);var purchaseDate = new Date(result.purchaseDate).toLocaleDateString();title = title.replace(/"/g, '');title = title.replace(/\n|\r\n|\r/g, '');var authorList = authors[0];csvData += '"' + asin + '","' + title + '","' + authorList + '","' + purchaseDate + '"\n'}var bom = new Uint8Array([0xEF, 0xBB, 0xBF]);var blob = new Blob([bom, csvData], {type: 'text/csv'});var url = (window.URL || window.webkitURL).createObjectURL(blob);var link = document.createElement('a');link.download = FILE_NAME;link.href = url;document.body.appendChild(link);link.click();document.body.removeChild(link);});});})();

参考

Gist - Amazon Kindle Export

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした