forked from DeppWang/youdaonote-pull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull.py
746 lines (608 loc) · 30.7 KB
/
pull.py
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import sys
import time
import hashlib
import os
import json
import xml.etree.ElementTree as ET
from urllib.parse import urlparse
import re
import logging
from markdownify import markdownify as md
# logging.basicConfig(level=logging.INFO)
__author__ = 'Depp Wang ([email protected])'
__github__ = 'https//github.com/DeppWang/youdaonote-pull'
def timestamp() -> str:
return str(int(time.time() * 1000))
def check_config(config_name) -> dict:
""" 检查 config.json 文件格式 """
with open(config_name, 'rb') as f:
config_str = f.read().decode('utf-8')
logging.info('config_str 格式:\n %s', config_str)
try:
# 将字符串转换为字典
config_dict = eval(config_str)
except SyntaxError:
raise SyntaxError('请检查「config.json」格式是否为 utf-8 的 json!建议使用 Sublime 编辑「config.json」')
# 如果某个 key 不存在,抛出异常
try:
config_dict['username']
config_dict['password']
config_dict['local_dir']
config_dict['ydnote_dir']
config_dict['smms_secret_token']
except KeyError:
raise KeyError('请检查「config.json」的 key 是否分别为 username, password, local_dir, ydnote_dir, smms_secret_token')
if config_dict['username'] == '' or config_dict['password'] == '':
raise ValueError('账号密码不能为空,请检查「config.json」!')
return config_dict
def covert_cookies(file_name) -> list:
if not os.path.exists(file_name):
logging.info('%s is null', file_name)
raise OSError(file_name + ' 不存在')
with open(file_name, 'r', encoding='utf-8') as f:
json_str = f.read()
try:
# 将字符串转换为字典
cookies_dict = eval(json_str)
cookies = cookies_dict['cookies']
except Exception:
raise Exception('转换「' + file_name + '」为字典时出现错误')
return cookies
class LoginError(ValueError):
pass
class YoudaoNoteSession(requests.Session):
""" 继承于 requests.Session,能像浏览器一样,完成一个完整的 Session 操作"""
# 类变量,不随着对象改变
WEB_URL = 'https://note.youdao.com/web/'
SIGN_IN_URL = 'https://note.youdao.com/signIn/index.html?&callback=https%3A%2F%2Fnote.youdao.com%2Fweb%2F&from=web' # 浏览器在传输链接的过程中是否都将符号转换为 Unicode?
LOGIN_URL = 'https://note.youdao.com/login/acc/urs/verify/check?app=web&product=YNOTE&tp=urstoken&cf=6&fr=1&systemName=&deviceType=&ru=https%3A%2F%2Fnote.youdao.com%2FsignIn%2F%2FloginCallback.html&er=https%3A%2F%2Fnote.youdao.com%2FsignIn%2F%2FloginCallback.html&vcode=&systemName=&deviceType=×tamp='
COOKIE_URL = 'https://note.youdao.com/yws/mapi/user?method=get&multilevelEnable=true&_=%s'
ROOT_ID_URL = 'https://note.youdao.com/yws/api/personal/file?method=getByPath&keyfrom=web&cstk=%s'
DIR_MES_URL = 'https://note.youdao.com/yws/api/personal/file/%s?all=true&f=true&len=200&sort=1&isReverse=false&method=listPageByParentId&keyfrom=web&cstk=%s'
FILE_URL = 'https://note.youdao.com/yws/api/personal/sync?method=download&keyfrom=web&cstk=%s'
# 莫有类方法
def __init__(self):
# 使用父类的构造函数初始化 self
requests.Session.__init__(self)
self.headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
}
# 属于对象变量
self.cstk = None
self.local_dir = None
self.smms_secret_token = None
def check_and_login(self, username, password) -> str:
try:
cookies = covert_cookies('cookies.json')
except Exception as err:
logging.info('covert_cookies error: %s', format(err))
cookies = None
# 如果 cookies 不为 null,使用 cookies 登录
if cookies is not None:
# 如果 Cookies 被修改或过期等原因导致 Cookies 登录失败,改用使用账号密码登录
try:
root_id = self.cookies_login(cookies)
print('本次使用 Cookies 登录')
except KeyError as err:
logging.info('cookie 登录出错:%s', format(err))
root_id = self.login(username, password)
print('本次使用账号密码登录,已将 Cookies 保存到「cookies.json」中,下次使用 Cookies 登录')
else:
root_id = self.login(username, password)
print('本次使用账号密码登录,已将 Cookies 保存到「cookies.json」中,下次使用 Cookies 登录')
return root_id
def login(self, username, password) -> str:
""" 模拟浏览器用户操作,使用账号密码登录,并保存 Cookie """
# 模拟打开网页版
self.get(self.WEB_URL)
# 模拟设置上一步链接
self.headers['Referer'] = self.WEB_URL
# 模拟重定向跳转到登录页
self.get(self.SIGN_IN_URL)
# 模拟设置上一步链接
self.headers['Referer'] = self.SIGN_IN_URL
# 模拟跳转到登录页后的请求连接
self.get('https://note.youdao.com/login/acc/pe/getsess?product=YNOTE&_=%s' % timestamp())
self.get('https://note.youdao.com/auth/cq.json?app=web&_=%s' % timestamp())
self.get('https://note.youdao.com/auth/urs/login.json?app=web&_=%s' % timestamp())
data = {
'username': username,
'password': hashlib.md5(password.encode('utf-8')).hexdigest()
}
logging.info('cookies: %s', self.cookies)
# 模拟登陆
self.post(self.LOGIN_URL,
data=data, allow_redirects=True)
# 登录成功后的链接,里面包含可用于登录的最新 Cookie: YNOTE_CSTK
self.get(self.COOKIE_URL % timestamp())
logging.info('new cookies: %s', self.cookies)
# 设置 cookies
cstk = self.cookies.get('YNOTE_CSTK')
if cstk is None:
logging.info('cstk: %s', cstk)
raise LoginError('请检查账号密码是否正确!也可能因操作频繁导致需要验证码,请切换网络(改变 ip)或等待一段时间后重试!')
self.cstk = cstk
self.save_cookies()
return self.get_root_id()
def save_cookies(self) -> None:
""" 将 Cookies 保存到 cookies.json """
cookies_dict = {}
cookies = []
# cookiejar 为 RequestsCookieJar,相当于是一个 Map 对象
cookiejar = self.cookies
for cookie in cookiejar:
cookie_eles = [cookie.name, cookie.value, cookie.domain, cookie.path]
cookies.append(cookie_eles)
cookies_dict['cookies'] = cookies
with open('cookies.json', 'wb') as f:
f.write(str(json.dumps(cookies_dict, indent=4, sort_keys=True)).encode())
def cookies_login(self, cookies_dict) -> str:
""" 使用 Cookies 登录,其实就是设置 Cookies """
cookiejar = self.cookies
for cookie in cookies_dict:
cookiejar.set(cookie[0], cookie[1], domain=cookie[2], path=cookie[3])
self.cstk = cookies_dict[0][1]
return self.get_root_id()
def get_root_id(self) -> str:
"""
获取有道云笔记 root_id
root_id 始终不会改变?可保存?可能会改变,几率很小。可以保存,保存又会带来新的复杂度。只要登录后,获取一下也没有影响
"""
data = {
'path': '/',
'entire': 'true',
'purge': 'false',
'cstk': self.cstk
}
response = self.post(self.ROOT_ID_URL % self.cstk, data=data)
json_obj = json.loads(response.content)
try:
return json_obj['fileEntry']['id']
# Cookie 登录时可能错误
except KeyError:
raise KeyError('Cookie 中没有 cstk')
# parsed = json.loads(response.content.decode('utf-8'))
# raise LoginError('请检查账号密码是否正确!也可能因操作频繁导致需要验证码,请切换网络(改变 ip)或等待一段时间后重试!接口返回内容:',
# json.dumps(parsed, indent=4, sort_keys=True))
def get_all(self, local_dir, ydnote_dir, smms_secret_token, root_id) -> None:
""" 下载所有文件 """
# 如果本地为指定文件夹,下载到当前路径的 youdaonote 文件夹中,如果是 Windows 系统,将路径分隔符(\\)替换为 /
if local_dir == '':
local_dir = os.path.join(os.getcwd(), 'youdaonote').replace('\\', '/')
# 如果指定的本地文件夹不存在,创建文件夹
if not os.path.exists(local_dir):
try:
os.mkdir(local_dir)
except FileNotFoundError:
raise FileNotFoundError('请检查「%s」上层文件夹是否存在,并使用绝对路径!' % local_dir)
# 有道云笔记指定导出文件夹名不为 '' 时,获取文件夹 id
if ydnote_dir != '':
root_id = self.get_dir_id(root_id, ydnote_dir)
logging.info('root_id: %s', root_id)
if root_id is None:
raise ValueError('此文件夹「%s」不是顶层文件夹,暂不能下载!' % ydnote_dir)
self.local_dir = local_dir # 此处设置,后面会用,避免传参
self.smms_secret_token = smms_secret_token # 此处设置,后面会用,避免传参
self.get_file_recursively(root_id, local_dir)
def get_dir_id(self, root_id, ydnote_dir) -> str:
""" 获取有道云笔记指定文件夹 id,目前指定文件夹只能为顶层文件夹,如果要指定文件夹下面的文件夹,请自己改用递归实现 """
url = self.DIR_MES_URL % (root_id, self.cstk)
response = self.get(url)
json_obj = json.loads(response.content)
try:
entries = json_obj['entries']
except KeyError:
raise KeyError('有道云笔记修改了接口地址,此脚本暂时不能使用!请提 issue')
for entry in entries:
file_entry = entry['fileEntry']
name = file_entry['name']
if name == ydnote_dir:
return file_entry['id']
def get_file_recursively(self, id, local_dir) -> None:
""" 递归遍历,根据 id 找到目录下的所有文件 """
url = self.DIR_MES_URL % (id, self.cstk)
response = self.get(url)
json_obj = json.loads(response.content)
try:
json_obj['count']
# 如果 json_obj 不是 json,退出
except KeyError:
logging.info('json_obj: %s', json_obj)
raise KeyError('有道云笔记修改了接口地址,此脚本暂时不能使用!请提 issue')
for entry in json_obj['entries']:
file_entry = entry['fileEntry']
id = file_entry['id']
name = file_entry['name']
logging.info('name: %s', name)
# 如果是目录,继续遍历目录下文件
if file_entry['dir']:
sub_dir = os.path.join(local_dir, name)
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
self.get_file_recursively(id, sub_dir)
else:
self.judge_add_or_update(id, name, local_dir, file_entry)
def judge_add_or_update(self, id, name, local_dir, file_entry) -> None:
""" 判断是新增还是更新 """
name = self.optimize_name(name)
youdao_file_suffix = os.path.splitext(name)[1] # 笔记后缀
local_file_path = os.path.join(local_dir, name).replace('\\', '/') # 用于将后缀 .note 转换为 .md
original_file_path = os.path.join(local_dir, name).replace('\\', '/') # 保留本身后缀
tip = '' # 用于输出云笔记原格式
# 如果有有道云笔记是「笔记」类型,则设置提示类型
if youdao_file_suffix == '.note':
tip = ',云笔记原格式为 .note'
local_file_basename = os.path.join(local_dir, os.path.splitext(name)[0]) # 没有后缀的本地文件
# 使用 .md 后缀判断是否在本地存在
local_file_path = local_file_basename + '.md'
# 如果不存在,则下载
if not os.path.exists(local_file_path):
try:
self.get_file(id, original_file_path, youdao_file_suffix)
print('新增「%s」%s' % (local_file_path, tip))
except Exception as error:
logging.info(error)
print('「%s」转换为 Markdown 失败!请检查文件!' % original_file_path)
print('错误提示:%s' % format(error))
# 如果已经存在,判断是否需要更新
else:
# 如果有道云笔记文件更新时间小于本地文件时间,说明没有更新。跳过本地更新步骤
if file_entry['modifyTimeForSort'] < os.path.getmtime(local_file_path):
# print('此文件不更新,跳过 ...,最好一行动态变化')
logging.info('此文件「%s」不更新,跳过', local_file_path)
return
print('-----------------------------')
print('local file modifyTime: ' + str(int(os.path.getmtime(local_file_path))))
print('youdao file modifyTime: ' + str(file_entry['modifyTimeForSort']))
try:
# 考虑到使用 f.write() 直接覆盖原文件,在 Windows 下报错(WinError 183),先将其删除
if os.path.exists(local_file_path):
os.remove(local_file_path)
self.get_file(id, original_file_path, youdao_file_suffix)
print('更新「%s」%s' % (local_file_path, tip))
except Exception as error:
print('「%s」转换为 Markdown 失败!请检查文件!' % original_file_path)
print('错误提示:%s' % format(error))
def optimize_name(self, name):
""" 避免 open() 函数失败(因为目录名错误),修改文件名 """
regex = re.compile(r'[\\/:\*\?"<>\|]') # 替换 \ / : * ? " < > | 为 _
name = regex.sub('_', name)
return name
def get_file(self, file_id, file_path, youdao_file_suffix) -> None:
""" 下载文件。先不管什么类型文件,均下载。如果是 .note 类型,转换为 Markdown """
data = {
'fileId': file_id,
'version': -1,
'convert': 'true',
'editorType': 1,
'cstk': self.cstk
}
url = self.FILE_URL % self.cstk
response = self.post(url, data=data)
# 权限问题,导致下载内容为接口错误提醒值。contentStr = response.content.decode('utf-8')
# 如果登录失败,是否会走到这个方法?不会走到这个方法,前面将中断
# if is_json(response.content):
# pares = json.loads(response)
if youdao_file_suffix == '.md':
content = response.content.decode('utf-8')
content = self.covert_markdown_file_image_url(content, file_path)
try:
with open(file_path, 'wb') as f:
f.write(content.encode())
except UnicodeEncodeError as err:
print('错误提示:%s' % format(err))
return
with open(file_path, 'wb') as f:
f.write(response.content) # response.content 本身就是字节类型
# 如果文件是 .note 类型,将其转换为 MarkDown 类型
if youdao_file_suffix == '.note':
try:
self.covert_xml_to_markdown(file_path)
except ET.ParseError:
print('此 note 笔记应该为 17 年以前新建,格式为 html,将转换为 Markdown...')
self.covert_html_to_markdown(file_path)
def covert_xml_to_markdown(self, file_path) -> None:
""" 转换 xml 为 Markdown """
# 如果文件为 null,结束
if os.path.getsize(file_path) == 0:
base = os.path.splitext(file_path)[0]
os.rename(file_path, base + '.md')
return
# 使用 xml.etree.ElementTree 将 xml 文件转换为多维数组
tree = ET.parse(file_path)
root = tree.getroot()
flag = 0 # 用于输出转换提示
nl = '\r\n' # 考虑 Windows 系统,换行符设为 \r\n
new_content = f'' # f-string 多行字符串
# list_item 的 id 与 type 的对应
list_item = {}
for child in root[0]:
if 'list' in child.tag:
list_item[child.attrib['id']] = child.attrib['type']
# 得到多维数组中的文本,因为是数组,不是对象(json),所以只能遍历
# root[1] 为 body
catalogue = False # 目录 catalogue 只替换一次 替换成功后 变成 True
for child in root[1]:
# 正常文本
if 'para' in child.tag:
for child2 in child:
if 'text' in child2.tag:
# 将 None 转为 "
if child2.text is None:
child2.text = ''
new_content += f'%s{nl}{nl}' % child2.text
break
# 目录 catalogue 只替换一次
elif 'catalogue' in child.tag:
if catalogue == False :
new_content += f'[toc]{nl}{nl}'
catalogue = True
# 标题
elif 'heading' in child.tag:
level = child.attrib['level']
if level == 'a' or level == 'b':
level = 1
for child2 in child:
if 'text' in child2.tag:
# 将 None 转为 "
if child2.text is None:
child2.text = ''
else:
new_content += f'%s ' % ("#" * int(level))
new_content += f'%s{nl}{nl}' % child2.text
break
elif 'image' in child.tag:
if flag == 0:
self.print_ydnote_file_name(file_path)
for child2 in child:
# source 在 text 前
if 'source' in child2.tag:
image_url = ''
if child2.text is not None:
image_url = self.get_new_down_or_upload_url(child2.text, file_path)
flag += 1
elif 'text' in child2.tag:
image_name = child2.text
if child2.text is None:
image_name = ''
new_content += f'![%s](%s){nl}{nl}' % (image_name, image_url)
break
elif 'attach' in child.tag:
# resource 在 filename 前
for child2 in child:
if 'resource' in child2.tag:
attach_url = ''
if child2.text is not None:
attach_url = child2.text
elif 'filename' in child2.tag:
attach_name = child2.text
if child2.text is None:
attach_name = '未命名'
attach_url = self.get_new_down_or_upload_url(attach_url, file_path, attach_name)
new_content += f'[%s](%s){nl}{nl}' % (attach_name, attach_url)
break
# 代码块
elif 'code' in child.tag:
for child2 in child:
# text 在 language 前
if 'text' in child2.tag:
code = child2.text
elif 'language' in child2.tag:
language = child2.text
if language is None:
language = ''
new_content += f'```%s{nl}%s{nl}```{nl}{nl}' % (language, code)
break
elif 'list-item' in child.tag:
# logging.info('list-item child: %s' % child)
# 无序列表
if list_item.get(child.attrib['list-id']) == 'unordered':
for child2 in child:
if 'text' in child2.tag:
text = child2.text
if text is None:
text = ''
new_content += f'- %s{nl}{nl}' % text
# 有序列表
elif list_item.get(child.attrib['list-id']) == 'ordered':
for child2 in child:
if 'text' in child2.tag:
count = 1
text = child2.text
if text is None:
text = ''
new_content += f'%s. %s{nl}{nl}' % (count, text)
# count += 1
# 复选框
elif 'todo' in child.tag:
for child2 in child:
if 'text' in child2.tag:
text = child2.text
if text is None:
text = ''
new_content += f'- [ ] %s{nl}{nl}' % text
# 引用
elif 'quote' in child.tag:
for child2 in child:
if 'text' in child2.tag:
text = child2.text
if text is None:
text = ''
new_content += f'> %s{nl}{nl}' % text
# 表格
elif 'table' in child.tag:
for child2 in child:
if 'content' in child2.tag:
new_content += f'```{nl}原来格式为表格(table),转换较复杂,未转换,需要手动复制一下{nl}%s{nl}```{nl}{nl}' % child2.text
# 其他
else:
for child2 in child:
if 'text' in child2.tag:
text = child2.text
if text is None:
text = ''
new_content += f'%s{nl}{nl}' % text
self.write_content(file_path, new_content)
def covert_html_to_markdown(self, file_path):
with open(file_path, 'rb') as f:
content_str = f.read().decode('utf-8')
new_content = md(content_str)
self.write_content(file_path, new_content)
def write_content(self, file_path, new_content):
" File is **.note,new_content is markdown string "
base = os.path.splitext(file_path)[0]
new_file_path = base + '.md'
os.rename(file_path, new_file_path)
with open(new_file_path, 'wb') as f:
f.write(new_content.encode())
def covert_markdown_file_image_url(self, content, file_path) -> str:
""" 将 Markdown 中的有道云图床图片转换为 sm.ms 图床 """
reg = r'!\[.*?\]\((.*?note\.youdao\.com.*?)\)'
p = re.compile(reg)
urls = p.findall(content)
if len(urls) > 0:
self.print_ydnote_file_name(file_path)
for url in urls:
new_url = self.get_new_down_or_upload_url(url, file_path)
content = content.replace(url, new_url)
return content
def print_ydnote_file_name(self, file_path) -> None:
ydnote_dirName = file_path.replace(self.local_dir, '')
print('正在转换有道云笔记「%s」中的有道云图片链接...' % ydnote_dirName)
def get_new_down_or_upload_url(self, url, file_path, attach_name='') -> str:
""" 根据是否存在 smms_secret_token 判断是否需要上传到 sm.ms """
if 'note.youdao.com' not in url:
return url
# 当 smms_secret_token 为空(不上传到 SM.MS)和是附件时,下载到图片和附件到本地
if self.smms_secret_token == '' or attach_name != '':
return self.download_file(url, file_path, attach_name)
new_url = self.upload_to_smms(url, self.smms_secret_token)
if new_url != url:
return new_url
return self.download_file(url, file_path, attach_name)
def download_file(self, url, file_path, attach_name) -> str:
""" 下载文件到本地,返回相对 url """
try:
response = self.get(url)
# 如果此处不抓异常,将退出运行
except requests.exceptions.ProxyError as err:
print('网络错误,「%s」下载失败' % url)
print('错误提示:%s' % format(err))
return url
content_type = response.headers.get('Content-Type')
file_type = '图片' if attach_name == '' else '附件'
if response.status_code != 200 or content_type is None:
self.print_download_yd_file_error(url, file_type)
return url
if attach_name != '':
# 默认下载附件到 youdaonote-attachments 文件夹
file_dirname = 'youdaonote-attachments'
file_suffix = attach_name
else:
# 默认下载图片到 youdaonote-images 文件夹
file_dirname = 'youdaonote-images'
# 后缀 png 和 jpeg 后可能出现 ; `**.png;`, 原因未知
file_suffix = '.' + content_type.split('/')[1].replace(';', '')
local_file_dir = os.path.join(self.local_dir, file_dirname)
if not os.path.exists(local_file_dir):
os.mkdir(local_file_dir)
file_basename = os.path.basename(urlparse(url).path)
file_name = file_basename + file_suffix
local_file_path = os.path.join(local_file_dir, file_name)
try:
with open(local_file_path, 'wb') as f:
f.write(response.content) # response.content 本身就为字节类型
if attach_name != '':
print('已将附件「%s」转换为「%s」,SM.MS 只能上传图片,所以附件只下载到本地' % (url, local_file_path))
else:
print('已将图片「%s」转换为「%s」' % (url, local_file_path))
except:
print(url + ' %s有误!' % file_type)
return url
relative_file_path = self.set_relative_file_path(file_path, file_name, local_file_dir)
return relative_file_path
def set_relative_file_path(self, file_path, file_name, local_file_dir):
""" 图片/附件设置为相对地址 """
note_file_dir = os.path.dirname(file_path)
rel_file_dir = os.path.relpath(local_file_dir, note_file_dir)
rel_file_path = os.path.join(rel_file_dir, file_name)
new_file_path = rel_file_path.replace('\\', '/')
return new_file_path
def upload_to_smms(self, old_url, smms_secret_token) -> str:
""" 上传图片到 sm.ms """
try:
smfile = self.get(old_url).content
except:
self.print_download_yd_image_error(old_url)
return old_url
smms_upload_api = 'https://sm.ms/api/v2/upload'
headers = {'Authorization': smms_secret_token}
files = {'smfile': smfile}
try:
res = requests.post(smms_upload_api, headers=headers, files=files)
except requests.exceptions.ProxyError as err:
logging.info('网络错误,请重试')
print('网络错误,上传「%s」到 SM.MS 失败!将下载图片到本地' % old_url)
print('错误提示:%s' % format(err))
return old_url
try:
res_json = res.json()
except ValueError:
print('SM.MS 免费版每分钟限额 20 张图片,每小时限额 100 张图片,大小限制 5 M,上传失败!「%s」未转换,将下载图片到本地' % old_url)
return old_url
url = old_url
if res_json['success'] is False:
if res_json['code'] == 'image_repeated':
url = res_json['images']
elif res_json['code'] == 'flood':
print('SM.MS 免费版每分钟限额 20 张图片,每小时限额 100 张图片,大小限制 5 M,上传失败!「%s」未转换,将下载图片到本地' % url)
return url
else:
print(
'上传「%s」到 SM.MS 失败,请检查图片 url 或 smms_secret_token(%s)是否正确!将下载图片到本地' % (url, smms_secret_token))
return url
else:
url = res_json['data']['url']
print('已将图片「%s」转换为「%s」' % (old_url, url))
return url
def print_download_yd_file_error(self, url, file_type) -> None:
print('下载「%s」失败!%s可能已失效,可浏览器登录有道云笔记后,查看%s是否能正常加载(验证登录才能查看)' % (url, file_type, file_type))
def print_download_yd_image_error(self, url) -> None:
print('下载「%s」失败!图片可能已失效,可浏览器登录有道云笔记后,查看图片是否能正常加载(验证登录才能查看)' % url)
def main():
start_time = int(time.time())
try:
config_dict = check_config('config.json')
session = YoudaoNoteSession()
root_id = session.check_and_login(config_dict['username'], config_dict['password'])
print('正在 pull,请稍后 ...')
session.get_all(config_dict['local_dir'], config_dict['ydnote_dir'], config_dict['smms_secret_token'], root_id)
except requests.exceptions.ProxyError as proxyErr:
print('请检查网络代理设置;也有可能是调用有道云笔记接口次数达到限制,请等待一段时间后重新运行脚本,若一直失败,可删除「cookies.json」后重试')
print('错误提示:' + format(proxyErr))
print('已终止执行')
sys.exit(1)
except requests.exceptions.ConnectionError as connectionErr:
print('网络错误,请检查网络是否正常连接。若突然执行中断,可忽略此错误,重新运行脚本')
print('错误提示:' + format(connectionErr))
print('已终止执行')
sys.exit(1)
except LoginError as loginErr:
print('错误提示:' + format(loginErr))
print('已终止执行')
sys.exit(1)
# 链接错误等异常
except Exception as err:
print('错误提示:' + format(err))
print('已终止执行')
sys.exit(1)
end_time = int(time.time())
print('运行完成!耗时 %s 秒' % str(end_time - start_time))
if __name__ == '__main__':
main()