-
Notifications
You must be signed in to change notification settings - Fork 0
/
readwise.js
131 lines (110 loc) · 3.1 KB
/
readwise.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
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
const SOURCE_TYPE = "drafts";
const BASE_URL = "https://readwise.io/api/v2/";
let selectedText;
if (editor.getSelectedText().length > 0) {
selectedText = editor.getSelectedText();
} else {
selectedText = draft.content;
}
let selectedBook = selectBook(getEntriesFromReadwise);
if (selectedBook != "cancelled") {
postToReadwise(selectedText, selectedBook);
}
function selectBook(data) {
let actionPrompt = new Prompt();
actionPrompt.isCancellable = true;
entries = getEntriesFromReadwise();
entries.forEach(function (bookTitle) {
actionPrompt.addButton(bookTitle);
});
actionPrompt.addButton("New Book");
let userSelectedAButton = actionPrompt.show();
let selectedBook = "userCancelled";
if (userSelectedAButton) {
selectedBook = actionPrompt.buttonPressed;
} else {
app.displayInfoMessage("Prompt was cancelled.");
context.cancel();
return "cancelled";
}
if (selectedBook == "New Book") {
let newBookPrompt = new Prompt();
newBookPrompt.addTextField("bookName", "New Book", "Name");
newBookPrompt.addButton("OK");
let userSelectedAButtonNewBook = newBookPrompt.show();
if (userSelectedAButtonNewBook) {
selectedBook = newBookPrompt.fieldValues["bookName"];
} else {
app.displayInfoMessage("Prompt was cancelled.");
context.cancel();
return "cancelled";
}
}
return selectedBook;
}
function getEntriesFromReadwise() {
const GET_URL = BASE_URL + "books/?source=" + SOURCE_TYPE + "&page_size=5";
let credReadwise = Credential.create(
"Readwise",
"Highlight surfacing service."
);
credReadwise.addPasswordField("token", "API Token");
credReadwise.authorize();
let httpMain = HTTP.create();
let respMain = httpMain.request({
url: GET_URL,
method: "GET",
headers: {
Authorization: `Token ${credReadwise.getValue("token")}`,
},
});
let entries = [];
if (respMain.statusCode == 200) {
let responseText = respMain.responseText;
let responseData = JSON.parse(responseText);
let bookResults = responseData.results;
bookResults.forEach(function (book) {
entries.push(book.title);
});
entries.push("Quotes");
} else {
console.log("Error in getting books");
}
return entries;
}
function postToReadwise(selectedText, selectedBook) {
const POST_URL = BASE_URL + "highlights/";
let credReadwise = Credential.create(
"Readwise",
"Highlight surfacing service."
);
credReadwise.addPasswordField("token", "API Token");
credReadwise.authorize();
if (selectedBook) {
let httpMain = HTTP.create();
let respMain = httpMain.request({
url: POST_URL,
method: "POST",
data: {
highlights: [
{
text: selectedText,
title: selectedBook.toString(),
source_type: SOURCE_TYPE,
},
],
},
headers: {
Authorization: `Token ${credReadwise.getValue("token")}`,
},
});
if (respMain.success) {
return true;
} else {
console.log(`[${respMain.statusCode}] ${respMain.error}`);
return false;
}
} else {
// Nothing to do
}
}