This repository was archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRemarkable.js
More file actions
204 lines (181 loc) · 6.18 KB
/
Remarkable.js
File metadata and controls
204 lines (181 loc) · 6.18 KB
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
198
199
200
201
202
203
204
AUTH_HOST = "https://webapp-production-dot-remarkable-production.appspot.com";
class RemarkableAPI {
constructor(deviceId = null, deviceToken = null, oneTimeCode = null) {
// oneTimeCode from ${AUTH_HOST}/device/connect/mobile
if (deviceToken === null && oneTimeCode === null) {
throw "Need at least either device-token or one-time-code";
}
if (deviceId === null) {
Logger.log("Creating new Remarkable device id..");
this.deviceId = Utilities.getUuid();
}
else {
Logger.log("Using existing Remarkable device id..");
this.deviceId = deviceId;
}
if (deviceToken === null) {
Logger.log("Requesting new Remarkable device token from one time code..");
this.deviceToken = this.constructor._getDeviceToken(this.deviceId, oneTimeCode);
}
else {
Logger.log("Using existing Remarkable device token..");
this.deviceToken = deviceToken;
}
this.userToken = this.constructor._getUserToken(this.deviceToken);
this.storageHost = this.constructor._getStorageHost(this.userToken);
}
// https://github.com/splitbrain/ReMarkableAPI/wiki/Authentication
static _getDeviceToken(deviceId, oneTimeCode) {
let data = {
"code": oneTimeCode, // one-time code from website
"deviceDesc": "desktop-windows",
"deviceID": deviceId
};
let options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(data)
};
// https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
let response = UrlFetchApp.fetch(`${AUTH_HOST}/token/json/2/device/new`, options);
let deviceToken = response.getContentText()
//Logger.log(`Received device token: ${deviceToken}`);
return deviceToken;
}
static _getUserToken(deviceToken) {
let options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify({}),
'headers': {
'Authorization': `Bearer ${deviceToken}`
}
};
let response = UrlFetchApp.fetch(`${AUTH_HOST}/token/json/2/user/new`, options);
let userToken = response.getContentText()
//Logger.log(`Received user Token: ${userToken}`);
return userToken;
}
// https://github.com/splitbrain/ReMarkableAPI/wiki/Service-Discovery
static _getStorageHost(userToken) {
let options = {
'method': 'get',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${userToken}`
}
};
let response = UrlFetchApp.fetch('https://service-manager-production-dot-remarkable-production.appspot.com/service/json/1/document-storage?environment=production&group=auth0%7C5a68dc51cb30df3877a1d7c4&apiVer=2', options);
let text = response.getContentText()
let data = JSON.parse(text);
if (data["Status"] == "OK") {
Logger.log(`Remarkable cloud storage host: ${data["Host"]}`);
return data["Host"];
}
else {
return null;
}
}
// https://github.com/splitbrain/ReMarkableAPI/wiki/Storage
listDocs(docUuid4 = null, withBlob = null) {
Logger.log("Fetching doc list from Remarkable cloud");
let options = {
'method': 'get',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
}
};
let params = [];
if (docUuid4 !== null) {
params.push(`doc=${docUuid4}`);
}
if (withBlob !== null) {
params.push(`withBlob=1`);
}
let urlParams = "";
if (params.length > 0) {
urlParams = "?" + params.join("&");
}
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/docs${urlParams}`, options);
let text = response.getContentText();
let data = JSON.parse(text);
return data;
}
findDocUUID(name) {
// TODO: should accept a path
let allDocs = this.listDocs();
let filteredDocs = allDocs.filter((r) => r["VissibleName"] == name);
if (filteredDocs.length > 0) {
return filteredDocs[0]["ID"];
}
else {
return null;
}
}
uploadRequest(data) {
let payloadData = data.map((r) => (
({ ID, Type, Version }) => ({ ID, Type, Version }))(r));
let options = {
'method': 'put',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
},
'payload': JSON.stringify(payloadData)
};
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/upload/request`, options);
let text = response.getContentText()
let res = JSON.parse(text);
return res;
}
blobUpload(url, zipBlob) {
let bytes = zipBlob.getBytes();
let options = {
'method': 'put',
'contentType': "", // needs to blank!
'contentLength': bytes.length,
//'payload': bytes,
'payload': zipBlob,
//'muteHttpExceptions': true // for debugging
};
let response = UrlFetchApp.fetch(url, options);
//let response = UrlFetchApp.getRequest(url, options);
if (response.getResponseCode() != 200) {
throw "Blob upload failed.";
}
}
uploadUpdateStatus(data) {
let payloadData = data.map((r) => (
({ ID, Type, Version, Parent, VissibleName }) => ({ ID, Type, Version, Parent, VissibleName }))(r));
let options = {
'method': 'put',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
},
'payload': JSON.stringify(payloadData)
};
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/upload/update-status`, options);
let text = response.getContentText()
let res = JSON.parse(text);
return res;
}
delete(data) {
let payloadData = data.map((r) => (
({ ID, Version }) => ({ ID, Version }))(r));
let options = {
'method': 'put',
'contentType': 'application/json',
'headers': {
'Authorization': `Bearer ${this.userToken}`
},
'payload': JSON.stringify(payloadData)
};
let response = UrlFetchApp.fetch(`https://${this.storageHost}/document-storage/json/2/delete`, options);
let text = response.getContentText()
let res = JSON.parse(text);
Logger.log(res);
return res;
}
}