-
Notifications
You must be signed in to change notification settings - Fork 0
/
remarkable.go
296 lines (250 loc) · 7.82 KB
/
remarkable.go
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package remarkableadaptor
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"path"
"strings"
"time"
)
type ReFile struct {
ReDocument
FileType string `json:"fileType"`
FormatVersion int `json:"formatVersion"`
Margins int `json:"margins"`
Orientation string `json:"orientation"`
RedirectionPageMap []int `json:"redirectionPageMap"`
SizeInBytes string `json:"sizeInBytes"`
FontName string `json:"fontName"`
TextAlignment string `json:"textAlignment"`
TextScale int `json:"textScale"`
LineHeight int `json:"lineHeight"`
Pages []string `json:"pages"`
OriginalPageCount int `json:"originalPageCount"`
CurrentPage int `json:"CurrentPage"`
PageCount int `json:"pageCount"`
CoverPageNumber int `json:"coverPageNumber"`
DummyDocument bool `json:"dummyDocument"`
ExtraMetadata struct {
LastBallpointColor string `json:"LastBallpointColor"`
LastBallpointSize string `json:"LastBallpointSize"`
LastBallpointv2Color string `json:"LastBallpointv2Color"`
LastBallpointv2Size string `json:"LastBallpointv2Size"`
LastCalligraphyColor string `json:"LastCalligraphyColor"`
LastCalligraphySize string `json:"LastCalligraphySize"`
LastEraseSectionColor string `json:"LastEraseSectionColor"`
LastEraseSectionSize string `json:"LastEraseSectionSize"`
LastEraserColor string `json:"LastEraserColor"`
LastEraserSize string `json:"LastEraserSize"`
LastEraserTool string `json:"LastEraserTool"`
LastFinelinerv2Color string `json:"LastFinelinerv2Color"`
LastFinelinerv2Size string `json:"LastFinelinerv2Size"`
LastHighlighterv2Color string `json:"LastHighlighterv2Color"`
LastHighlighterv2Size string `json:"LastHighlighterv2Size"`
LastMarkerv2Color string `json:"LastMarkerv2Color"`
LastMarkerv2Size string `json:"LastMarkerv2Size"`
LastPaintbrushv2Color string `json:"LastPaintbrushv2Color"`
LastPaintbrushv2Size string `json:"LastPaintbrushv2Size"`
LastPen string `json:"LastPen"`
LastPencilColor string `json:"LastPencilColor"`
LastPencilSize string `json:"LastPencilSize"`
LastPencilv2Color string `json:"LastPencilv2Color"`
LastPencilv2Size string `json:"LastPencilv2Size"`
LastSelectionToolColor string `json:"LastSelectionToolColor"`
LastSelectionToolSize string `json:"LastSelectionToolSize"`
LastSharpPencilv2Color string `json:"LastSharpPencilv2Color"`
LastSharpPencilv2Size string `json:"LastSharpPencilv2Size"`
LastTool string `json:"LastTool"`
LastUndefinedColor string `json:"LastUndefinedColor"`
LastUndefinedSize string `json:"LastUndefinedSize"`
} `json:"extraMetadata"`
}
type ReFolder struct {
ReDocument
}
type ReDocument struct {
Bookmarked bool `json:"Bookmarked"`
ID string `json:"ID"`
ModifiedClient time.Time `json:"ModifiedClient"`
Parent string `json:"Parent"`
Type string `json:"Type"`
Version int `json:"Version"`
VissibleName string `json:"VissibleName"`
}
type ReDocuments []ReDocument
type ReFolders []ReFolder
type ReFiles []ReFile
type ReMarkable struct {
host string
Documents ReDocuments
Folders []ReFolder
FoldersCache map[string]ReFolder
Files []ReFile
currentFolder *ReFolder
path string
}
func (tablet *ReMarkable) setHost(providedHost string) {
tablet.host = fmt.Sprintf("http://%s", providedHost)
}
func (folder ReDocument) String() string {
return folder.VissibleName
}
func (tablet *ReMarkable) MoveToRoot() {
tablet.currentFolder = nil
tablet.path = ""
}
func (tablet *ReMarkable) MoveFolder(folder *ReFolder) error {
if folder == nil {
return errors.New("folder is nil")
}
tablet.currentFolder = folder
tablet.path = tablet.currentFolder.ID
if _, err := tablet.FetchDocuments(); err != nil {
return err
}
return nil
}
func (tablet *ReMarkable) MoveParent() error {
if tablet.currentFolder == nil {
return errors.New("no parent folder")
}
if tablet.currentFolder.Parent == "" {
tablet.MoveToRoot()
if _, err := tablet.FetchDocuments(); err != nil {
return err
}
return nil
}
if tablet.currentFolder == nil {
return errors.New("no current folder")
}
cacheIndex := tablet.currentFolder.Parent
cache := tablet.FoldersCache[cacheIndex]
if err := tablet.MoveFolder(&cache); err != nil {
return err
}
return nil
}
func (tablet *ReMarkable) GetCurrentFolder() ReFolder {
return *tablet.currentFolder
}
func (tablet *ReMarkable) GetCurrentFolderName() string {
if tablet.currentFolder == nil {
return "Root"
}
return tablet.currentFolder.String()
}
func (tablet *ReMarkable) resetDocuments() {
tablet.Documents = ReDocuments{}
tablet.Folders = ReFolders{}
tablet.Files = ReFiles{}
}
func (tablet *ReMarkable) appendToCache(folder ReFolder) {
tablet.FoldersCache[folder.ID] = folder
}
func (tablet *ReMarkable) request() ([]byte, error) {
resp, err := http.Post(tablet.host+"/documents/"+tablet.path, "text/plain", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Decode the JSON to String
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func (tablet *ReMarkable) FetchDocuments() (*ReDocuments, error) {
// Fill the ReMarkable struct with the data from the API
b, err := tablet.request()
if err != nil {
return nil, err
}
tablet.resetDocuments()
var tmpFiles []ReFile
var tmpFolders []ReFolder
json.Unmarshal(b, &tablet.Documents)
json.Unmarshal(b, &tmpFiles)
json.Unmarshal(b, &tmpFolders)
for i, v := range tablet.Documents {
if v.Type == "CollectionType" {
tablet.appendToCache(tmpFolders[i])
tablet.Folders = append(tablet.Folders, tmpFolders[i])
} else if v.Type == "DocumentType" {
tablet.Files = append(tablet.Files, tmpFiles[i])
}
}
return &tablet.Documents, nil
}
func (tablet *ReMarkable) getTree(tab int, append string) string {
for _, file := range tablet.Files {
append += fmt.Sprintf("%s├─ 🗒️ %s\n", strings.Repeat("| ", tab), file.VissibleName)
}
for _, folder := range tablet.Folders {
append += fmt.Sprintf("%s├─ 📂 %s/\n", strings.Repeat("| ", tab), folder.VissibleName)
tablet.MoveFolder(&folder)
append = tablet.getTree(tab+1, append)
tablet.MoveParent()
}
return append
}
func (tablet *ReMarkable) Download(file *ReFile, filepath string) error {
if file == nil {
return errors.New("file is nil")
}
resp, err := http.Get(tablet.host + "/download/" + file.ID + "/placeholder")
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func (tablet *ReMarkable) Upload(filePath string, fileName string) error {
fileExt := path.Ext(filePath)
print(fileExt)
if fileExt != ".pdf" && fileExt != ".epub" {
return errors.New("filetype not supported")
}
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, _ := writer.CreateFormFile("file", fileName)
io.Copy(part, file)
writer.Close()
r, _ := http.NewRequest("POST", tablet.host+"/upload", body)
r.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
_, err = client.Do(r)
return err
}
func (tablet *ReMarkable) GetTree() string {
return tablet.getTree(0, "📂 "+tablet.GetCurrentFolderName()+":\n")
}
func (tablet *ReMarkable) Load(providedHost string) (*ReMarkable, error) {
tablet.setHost(providedHost)
tablet.path = ""
tablet.FoldersCache = make(map[string]ReFolder)
if _, err := tablet.FetchDocuments(); err != nil {
return nil, err
}
return tablet, nil
}