Created
January 10, 2025 13:06
-
-
Save zimathon/d700371578b36512c05212871f31e933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://read.amazon.co.jp/kindle-library | |
// consoleで実行 | |
let xhr = new XMLHttpRequest(); | |
let domain = 'https://read.amazon.co.jp/'; | |
let items = []; | |
let csvData = ""; | |
// function | |
function getItemsList(paginationToken = null) { | |
let url = domain + 'kindle-library/search?query=&libraryType=BOOKS' + (paginationToken ? '&paginationToken=' + paginationToken : '') + '&sortType=recency&querySize=50'; | |
// xhr.onreadystatechange を xhr.open() の前に移動 | |
xhr.onreadystatechange = function() { | |
switch (xhr.readyState) { | |
case 0: | |
console.log('uninitialized'); | |
break; | |
case 1: | |
console.log('loading...'); | |
break; | |
case 2: | |
console.log('loaded.'); | |
break; | |
case 3: | |
console.log('interactive... '); | |
break; | |
case 4: | |
if (xhr.status == 200) { | |
let data = xhr.responseText; | |
data = JSON.parse(data); | |
if (data.itemsList) { | |
items.push(...data.itemsList); | |
} | |
if (data.paginationToken) { | |
getItemsList(data.paginationToken); | |
} else { | |
// 全てのデータ取得が完了したらCSVデータを作成してダウンロード | |
items.forEach(item => { | |
// タイトルに含まれる",をエスケープする | |
const escapedTitle = item.title.replace(/"/g, '""'); | |
csvData += '"' + item.asin + '","' + escapedTitle + '"\n'; | |
}); | |
// CSVファイルとしてダウンロードさせる | |
let a = document.createElement('a'); | |
a.href = 'data:text/csv;charset=utf-8,%EF%BB%BF' + encodeURIComponent(csvData); | |
a.download = 'amazon_books.csv'; | |
document.body.appendChild(a); | |
a.click(); | |
document.body.removeChild(a); | |
} | |
} else { | |
console.log('Failed'); | |
} | |
break; | |
} | |
}; | |
xhr.open('GET', url, true); // 非同期通信を有効にするために true を指定 | |
xhr.send(); | |
} | |
// action | |
getItemsList(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment