-
Notifications
You must be signed in to change notification settings - Fork 0
/
booklog.js
96 lines (89 loc) · 2.93 KB
/
booklog.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @param {} books
* @param {'読みたい' | 'いま読んでる' | '読み終わった' | '積読 | ''} defaultStatus
*/
async function exportBooklogCSV(books, defaultStatus = '') {
/**
* @param {[string, string, string, string, string, string, string, string, string, string][]} csv2dArray
* @returns {string}
*/
function csv2dArrayToText(csv2dArray) {
return csv2dArray.map((csvArray) => csvArray.join(',')).join('\r\n');
}
/**
* @param {string} csvText
* @returns {void}
*/
function download(csvText) {
const downloadUrl = URL.createObjectURL(
new Blob([csvText], {
type: 'text/csv',
})
);
const downloadLink = document.createElement('a');
downloadLink.href = downloadUrl;
downloadLink.download = 'booklog.csv';
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
function convertBooksDataToCSV2dArray(books) {
return books.map((book) => [
/* ServiceID */ '1', //Amazon.co.jp ServiceID is 1
/* ItemID */ book.asin,
/* ISBN13 */ '',
/* Category */ '-',
/* Evaluation */ '',
/* Status */ '',
/* Review */ '',
/* Tag */ '',
/* Memo */ '',
/* RegistDate */ '',
/* FinishDate */ '',
]);
}
download(csv2dArrayToText(convertBooksDataToCSV2dArray(books)));
}
/**
* @param {'EBOOK' | 'COMICS' | undefined} resourceType
* @param {'acquisition_asc' | 'acquisition_desc' | 'title' | 'author' | 'recency'}
*/
async function fetchBooksViaKindleWeb({ resourceType, sortType } = {}) {
/**
* @param {number} paginationToken
*/
function buildUrl(paginationToken = 0) {
// @note MAX query size seems to be 50
return `https://read.amazon.co.jp/kindle-library/search?query=${
resourceType ? `&resourceType=${resourceType}` : ''
}${paginationToken ? `&paginationToken=${paginationToken}` : ''}&sortType=${
sortType ?? 'acquisition_asc'
}&querySize=50`;
}
/**
* @param {number} pagenationToken
* @returns {Promise<{
* asin: string,
* authors: string[],
* mangaOrComicAsin: boolean,
* originType: string,
* percentageRead: number,
* productUrl: string,
* resourceType: string,
* title: string,
* webReaderUrl: string
* }>} books
*/
async function fetchBooks(paginationToken) {
const res = await fetch(buildUrl(paginationToken)).then((res) =>
res.json()
);
if (res.paginationToken) {
return [...res.itemsList, ...(await fetchBooks(res.paginationToken))];
} else {
return res.itemsList;
}
}
return fetchBooks();
}
fetchBooksViaKindleWeb({ resourceType: 'EBOOK' }).then((books) => exportBooklogCSV(books));