forked from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathreadwiseApi.ts
197 lines (161 loc) · 5.65 KB
/
readwiseApi.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import Notify from 'notify';
import fetchBuilder from 'fetch-retry';
const fetch = fetchBuilder(global.fetch, { retries: 3, retryDelay: 1000 });
const API_ENDPOINT = 'https://readwise.io/api/v2';
const API_PAGE_SIZE = 1000; // number of results per page, default 100 / max 1000
export interface BooksAndHighlights {
books: Book[];
highlights: Highlight[];
}
export interface Highlight {
id: number;
text: string;
note: string;
location: number;
location_type: string;
highlighted_at: string;
url: string | null;
color: string;
updated: string;
book_id: number;
tags: Tag[];
}
export interface Tag {
id: number;
name: string;
}
export interface Book {
id: number;
title: string;
author: string;
category: string;
num_highlights: number;
last_highlight_at: string;
updated: string;
cover_image_url: string;
highlights_url: string;
source_url: string | null;
asin: string;
highlights: Highlight[];
tags: Tag[];
}
export interface Books {
[key: string]: Book;
}
export interface Library {
categories: Set<String>;
books: Books;
highlightCount: number;
}
export class ReadwiseApi {
private apiToken: string;
private notify: Notify;
constructor(apiToken: string, notify: Notify) {
if (!apiToken) {
throw new Error('API Token Required!');
}
this.setToken(apiToken);
this.notify = notify;
}
setToken(apiToken: string) {
this.apiToken = apiToken;
}
get headers() {
return {
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${this.apiToken}`,
},
};
}
async checkToken() {
const results = await fetch(`${API_ENDPOINT}/auth`, this.headers);
return results.status === 204; // Returns a 204 response if token is valid
}
// If lastUpdated or bookID aren't provided, fetch everything.
async fetchData(contentType = 'highlights', lastUpdated?: string, bookId?: Number): Promise<Highlight[] | Book[]> {
let url = `${API_ENDPOINT}/${contentType}?page_size=${API_PAGE_SIZE}`;
if (lastUpdated) url += `&updated__gt=${lastUpdated}`;
if (bookId) url += `&book_id=${bookId}`;
let data;
const results = [];
do {
console.info(`Readwise: Fetching ${contentType}`);
if (lastUpdated) console.info(`Readwise: Checking for new content since ${lastUpdated}`);
if (bookId) console.info(`Readwise: Checking for all highlights on book ID: ${bookId}`);
let statusBarText = `Readwise: Fetching ${contentType}`;
if (data && data['count']) statusBarText += ` (${results.length} / ${data.count})`;
this.notify.setStatusBarText(statusBarText);
const response = await fetch(url, this.headers);
data = await response.json();
if (response.status === 429) {
// Error handling for rate limit throttling
const rateLimitedDelayTime = parseInt(response.headers.get('Retry-After')) * 1000 + 1000;
console.warn(`Readwise: API Rate Limited, waiting to retry for ${rateLimitedDelayTime}`);
this.notify.setStatusBarText(`Readwise: API Rate Limited, waiting ${rateLimitedDelayTime}`);
await new Promise((_) => setTimeout(_, rateLimitedDelayTime));
console.info('Readwise: Trying to fetch highlights again...');
this.notify.setStatusBarText(`Readwise: Attempting to retry...`);
data.next = url;
} else {
results.push(...data.results);
if (data.next) {
const remainingRecords = data.count - results.length;
console.info(
`Readwise: There are ${remainingRecords} more records left, proceeding to next page:` + data.next
);
url = `${data.next}`;
}
}
} while (data.next);
if (results.length > 0)
console.info(`Readwise: Processed ${results.length} total ${contentType} results successfully`);
return results;
}
async fetchUpdatedContent(lastUpdated: string): Promise<BooksAndHighlights> {
if (!lastUpdated) throw new Error('Date required to fetch updates');
const updatedHighlights = [];
const updatedBooks = (await this.fetchData('books', lastUpdated)) as Book[];
// Iterate through Newly Updated Books, fetching all of their highlights
for (let bookId of updatedBooks.map((book: Book) => book.id)) {
const highlights = (await this.fetchData('highlights', null, bookId)) as Highlight[];
updatedHighlights.push(...highlights);
}
return {
books: updatedBooks as Book[],
highlights: updatedHighlights as Highlight[],
};
}
async fetchAllHighlightsAndBooks(): Promise<BooksAndHighlights> {
const books = (await this.fetchData('books')) as Book[];
const highlights = (await this.fetchData('highlights')) as Highlight[];
return {
books,
highlights,
};
}
async mergeHighlightsWithBooks(books: Book[], highlights: Highlight[]): Promise<Library> {
const library: Library = {
categories: new Set(),
books: {},
highlightCount: highlights.length,
};
for (const book of books) {
book['highlights'] = [];
library['books'][book['id']] = book;
library['categories'].add(book.category);
}
for (const highlight of highlights) {
library['books'][highlight['book_id']]['highlights'].push(highlight);
}
return library;
}
async downloadFullLibrary(): Promise<Library> {
const { books, highlights } = await this.fetchAllHighlightsAndBooks();
return await this.mergeHighlightsWithBooks(books, highlights);
}
async downloadUpdates(lastUpdated: string): Promise<Library> {
const { highlights, books } = await this.fetchUpdatedContent(lastUpdated);
return await this.mergeHighlightsWithBooks(books, highlights);
}
}