forked from Lvzhenqian/LeanoteMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
367 lines (339 loc) · 7.62 KB
/
main.go
File metadata and controls
367 lines (339 loc) · 7.62 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/mewbak/gopass"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
)
var (
UserName string
Password string
LeanoteUrl = "https://leanote.com"
UserId string
Token string
DirRoot = "."
)
const UserAgent = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36`
type querystring struct {
key string
value string
}
func httpClientDo(req *http.Request) ([]byte, error) {
//fmt.Printf("%s\n",req.URL.String())
client := http.DefaultClient
client.Timeout = time.Second * 10
resp, ReqErr := client.Do(req)
if ReqErr != nil {
return nil, ReqErr
}
if resp.StatusCode != 200 {
log.Println(resp.StatusCode)
return nil, errors.New("Respones is not 200")
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func Login() (ApiResponse, error) {
var ret ApiResponse
url := fmt.Sprintf("%s/api/auth/login", LeanoteUrl)
req, err := http.NewRequest("GET", url, nil)
query := req.URL.Query()
query.Add("email", UserName)
query.Add("pwd", Password)
req.URL.RawQuery = query.Encode()
req.Header.Set("User-Agent", UserAgent)
if err != nil {
return ApiResponse{}, err
}
if body, ok := httpClientDo(req); ok == nil {
//fmt.Println(string(body))
if err := json.Unmarshal(body, &ret); err != nil {
panic(err)
}
return ret, nil
} else {
return ApiResponse{}, ok
}
}
func Logout(user *ApiResponse) bool {
var ret ApiResponse
url := fmt.Sprintf("%s/api/auth/logout", LeanoteUrl)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false
}
query := req.URL.Query()
query.Add("token", user.Token)
req.URL.RawQuery = query.Encode()
req.Header.Set("User-Agent", UserAgent)
if body, ok := httpClientDo(req); ok == nil {
if err := json.Unmarshal(body, &ret); err != nil {
panic(err)
}
}
if ret.Ok {
fmt.Printf("bye bye! %s\n", user.UserName)
return true
}
return false
}
func AuthGetRequest(url string, q ...querystring) *http.Request {
req, ReqErr := http.NewRequest("GET", url, nil)
if ReqErr != nil {
panic(ReqErr)
}
query := req.URL.Query()
query.Add("userId", UserId)
query.Add("token", Token)
req.Header.Set("User-Agent", UserAgent)
for _, v := range q {
query.Add(v.key, v.value)
}
req.URL.RawQuery = query.Encode()
return req
}
func GetAllNoteBook() []Notebook {
var (
errResp ApiResponse
noteBooks []Notebook
)
api := fmt.Sprintf("%s/api/notebook/getNotebooks", LeanoteUrl)
req := AuthGetRequest(api)
body, readErr := httpClientDo(req)
if readErr != nil {
panic(readErr)
}
if err := json.Unmarshal(body, ¬eBooks); err != nil {
json.Unmarshal(body, &errResp)
}
return noteBooks
}
func hasNote(notebookId string) ([]Note, bool) {
var (
noteList []Note
errResp ApiResponse
)
api := fmt.Sprintf("%s/api/note/getNotes", LeanoteUrl)
req := AuthGetRequest(api, querystring{
key: "notebookId",
value: notebookId,
})
body, GetErr := httpClientDo(req)
if GetErr != nil {
panic(GetErr)
}
err := json.Unmarshal(body, ¬eList)
if err != nil {
json.Unmarshal(body, &errResp)
log.Println(errResp.Msg)
return nil, false
}
return noteList, true
}
func GetNoteContent(noteId string) (Note, error) {
var (
n Note
errResp ApiResponse
)
api := fmt.Sprintf("%s/api/note/getNoteAndContent", LeanoteUrl)
req := AuthGetRequest(api, querystring{
key: "noteId",
value: noteId,
})
body, GetErr := httpClientDo(req)
if GetErr != nil {
panic(GetErr)
}
if err := json.Unmarshal(body, &n); err != nil {
json.Unmarshal(body, errResp)
log.Fatalln(errResp.Msg)
}
if n.IsTrash {
return Note{}, errors.New("delete!!")
}
return n, nil
}
func GetImage(fileId string) ([]byte, error) {
api := fmt.Sprintf("%s/api/file/getImage", LeanoteUrl)
req := AuthGetRequest(api, querystring{
key: "fileId",
value: fileId,
})
return httpClientDo(req)
}
func GetAttach(fileId string) ([]byte, error) {
api := fmt.Sprintf("%s/api/file/getAttach", LeanoteUrl)
req := AuthGetRequest(api, querystring{
key: "fileId",
value: fileId,
})
return httpClientDo(req)
}
func MakeDirTrees(books []Notebook) []*Book {
var (
roots []*Book
childs []*Book
)
// filter list
for _, value := range books {
if value.ParentNotebookId == "" {
roots = append(roots, &Book{
Id: value.NotebookId,
Title: value.Title,
ParentId: "",
})
} else {
childs = append(childs, &Book{
Id: value.NotebookId,
Title: value.Title,
ParentId: value.ParentNotebookId,
})
}
}
for _, v := range roots {
MakeTree(childs, v)
}
return roots
}
func Write(b []byte, p string) error {
f, e := os.OpenFile(p, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0755)
if e != nil {
return e
}
_, wErr := f.Write(b)
return wErr
}
func Writefile(p string, n Note) error {
var (
abs string
content string
attachPath string
)
if n.IsMarkdown {
abs = path.Join(p, n.Title+".md")
} else {
abs = path.Join(p, n.Title+".txt")
}
content = n.Content
if len(n.Files) > 0 {
for _, v := range n.Files {
if v.IsAttach {
b, e := GetAttach(v.FileId)
if e != nil {
return e
}
attachdir:= path.Join(p,"attach")
os.MkdirAll(attachdir,0755)
filename := v.Title
if filename == "" {
filename = fmt.Sprintf("%s.%s",v.FileId,v.Type)
}
attachPath = path.Join(attachdir, filename)
Write(b, attachPath)
} else {
b, e := GetImage(v.FileId)
if e != nil {
return e
}
imagedir := path.Join(p,"images")
os.MkdirAll(imagedir,0755)
filetype := v.Type
if filetype == ""{
filetype = ".png"
}
rp := path.Join(imagedir, v.FileId+filetype)
Write(b, rp)
old := fmt.Sprintf("%s/api/file/getImage?fileId=%s", LeanoteUrl, v.FileId)
shortPath := path.Join("images",v.FileId+filetype)
content = strings.ReplaceAll(content, old, shortPath)
}
}
}
return Write([]byte(content), abs)
}
func GetNoteAndAll(bookID string, dirname string) error {
if notes, ok := hasNote(bookID); ok {
for _, v := range notes {
note, e := GetNoteContent(v.NoteId)
if e != nil {
return e
}
// if is delete !
if note.IsTrash {
continue
}
err := Writefile(dirname, note)
if err != nil {
//log.Println(err)
continue
}
}
}
return nil
}
func MakeDirs(parent string, books *Book) {
P := path.Join(parent, books.Title)
os.MkdirAll(P, 0755)
GetNoteAndAll(books.Id, P)
if len(books.Child) > 0 {
for _, v := range books.Child {
MakeDirs(P, v)
}
}
}
func Exposes(notebooks []*Book) {
for _, v := range notebooks {
MakeDirs(DirRoot,v)
}
}
func main() {
var leanoteurl string
fmt.Printf("Leanote 网址[default: %s]: ", LeanoteUrl)
fmt.Scanln(&leanoteurl)
if leanoteurl != "" {
LeanoteUrl = leanoteurl
}
fmt.Printf("UserName: ")
fmt.Scanln(&UserName)
if passwd, ok := gopass.GetPass("password: "); ok == nil {
Password = passwd
}
fmt.Println(UserName, Password)
UserInfo, UserError := Login()
if UserError != nil {
panic(UserError)
}
//fmt.Printf("%#v\n", UserInfo)
if !UserInfo.Ok {
log.Fatalf("登陆失败:%s", UserInfo.Msg)
} else {
fmt.Printf("登陆成功 %s(%s)!\n", UserInfo.UserName, UserInfo.Email)
}
defer Logout(&UserInfo)
UserId = UserInfo.UserId
Token = UserInfo.Token
books := GetAllNoteBook()
dirTree := MakeDirTrees(books)
if b, err := json.Marshal(&dirTree); err == nil {
f, ferr := os.OpenFile("./dirtrees.json", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if ferr != nil {
panic(ferr)
}
defer f.Close()
f.Write(b)
}
fmt.Printf("保存到[default(.)]: ")
var p string
fmt.Scanln(&p)
if p != ""{
DirRoot = p
}
Exposes(dirTree)
}