代码

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
import os
import shutil
import re
import time
import requests


# ===============================================
def login_get_doc(yuque_token):
# 用户信息
url_user = 'https://www.yuque.com/api/v2/user'
header = {"X-Auth-Token": yuque_token}
resu = requests.get(url_user, headers=header).json()
user_name = resu["data"]["login"]

# 获取仓库信息
url_repo = 'https://www.yuque.com/api/v2/users/' + user_name + "/repos"
Repo_Result = requests.get(url_repo, headers=header).json()['data']
# print(Repo_Result)

# 获取所有文章链接
article_url_list = []
for item in Repo_Result:
if item['type'] == "Book":
if item['name'] == '博客文章-公开':
pass
else:
break
repo_id = item['id']
url_docs = 'https://www.yuque.com/api/v2/repos/' + str(repo_id) + '/docs'
Doc_Result = requests.get(url_docs, headers=header).json()['data']

for ii in Doc_Result:
slug = ii['slug']
url = f"https://www.yuque.com/api/v2/repos/{repo_id}/docs/{slug}"
article_url_list.append(url)
info = {"header":header, "Repo_Result":Repo_Result, "article_url_list":article_url_list }
return info

def get_pic_url(info):
"""获得文档及图片"""

header = info["header"]
article_url_list = info["article_url_list"]
table = str.maketrans('\/:*?"<>|' + "\n\r", "___________") # 映射表
PIC_Url = []
count = 0
file_num = len(article_url_list)

for url in article_url_list:
count = count + 1
single_doc = requests.get(url, headers=header).json()
article_title = single_doc["data"]["title"].translate(table)
print(f"{count}/{file_num}: {article_title}")
article_body = single_doc["data"]["body"]
pattern = r'!\[.*?\]\(.*?\)'
matches = re.findall(pattern, article_body)
for ii in matches:
PIC_Url.append(article_title+": "+ii)

return PIC_Url


if __name__ == "__main__":
yuque_token = "***"
info = login_get_doc(yuque_token)
PIC_Url = get_pic_url(info)
print(PIC_Url)

# 将列表写入txt文件
with open('PIC_Url.txt', 'w', encoding='utf-8') as file:
for link in PIC_Url:
if 'https://mypic2016.oss-cn-beijing.aliyuncs.com' not in link:
file.write(link + '\n')

# 打开txt文件
os.startfile('PIC_Url.txt')

语雀 Token

此处为语雀卡片,点击链接查看