-
Notifications
You must be signed in to change notification settings - Fork 0
/
download_all_documents.py
138 lines (106 loc) · 4.35 KB
/
download_all_documents.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
import argparse
import re
from pathlib import Path
import yaml
from yuque_py import Yuque
def get_repo_indices_from_yuque(yuque: Yuque, user: str):
return yuque.repos.list(user=user)["data"]
def get_repo_data_from_yuque(yuque: Yuque, namespace: str):
return yuque.repos.get(namespace=namespace)["data"]
def get_doc_data_from_yuque(yuque: Yuque, namespace: str, slug: str):
return yuque.docs.get(namespace=namespace, slug=slug)["data"]
def get_doc_indices_from_repo_data(repo_data: dict):
repo_toc_yml = yaml.load(stream=repo_data["toc_yml"], Loader=yaml.SafeLoader)
doc_indices = []
for doc_index in repo_toc_yml:
if "title" not in doc_index:
continue
if doc_index["type"] == "DOC":
doc_indices.append(doc_index)
return doc_indices
def get_doc_content_from_doc_data(doc_data):
doc_title = doc_data["title"]
doc_slug = doc_data["slug"]
doc_created_at = doc_data["created_at"]
doc_updated_at = doc_data["updated_at"]
doc_description = doc_data["description"]
doc_author = doc_data["creator"]["name"]
book_slug = doc_data["book"]["slug"]
book_description = doc_data["book"]["description"]
book_author = doc_data["book"]["user"]["login"]
book_yuque_link = f"https://www.yuque.com/{book_author}/{book_slug}"
doc_yuque_link = f"https://www.yuque.com/{book_author}/{book_slug}/{doc_slug}"
doc_body: str = substitute_invalid_text(doc_data["body"])
doc_content = [
"---",
f"title: {doc_title}",
f"created: {doc_created_at}",
f"updated: {doc_updated_at}",
f"description: {doc_description}",
f"author: {doc_author}",
f"original_link: {doc_yuque_link}",
f"book:",
f" - title: {book_slug}",
f" - author: {book_author}",
f" - description: {book_description}",
f" - original_link: {book_yuque_link}",
"---",
f"\n# {doc_title}",
doc_body,
]
return doc_content
def substitute_invalid_text(text: str):
patterns = {
r"<a.*?>": "",
f"</a>": "",
r"<br ?/>": "\n\n",
"\n -": "\n -",
}
for p, r in patterns.items():
text = re.sub(pattern=p, repl=r, string=text, flags=re.IGNORECASE)
return text
def convert_string_to_valid_window_path(string):
patterns = {
r"[/\\\\:\*\?\|\.]": "_",
r"\n": " ",
}
for p, r in patterns.items():
string = re.sub(pattern=p, repl=r, string=string)
return string
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--user", type=str, required=True, help="用户名")
parser.add_argument("--token", type=str, required=True, help="用户token")
parser.add_argument("--output", type=str, default="download", help="下载文档的存放目录")
parser.add_argument("--verbose", action="store_true", help="是否打印中间信息")
return parser.parse_args()
def main():
args = get_args()
output_dir = Path(args.output)
yuque = Yuque(api_host="https://www.yuque.com/api/v2", user_token=args.token)
repo_indices = get_repo_indices_from_yuque(yuque=yuque, user=args.user)
for repo_index in repo_indices:
if repo_index["type"] != "Book":
continue
repo_data = get_repo_data_from_yuque(
yuque=yuque, namespace=repo_index["namespace"]
)
repo_name = convert_string_to_valid_window_path(repo_data["name"])
doc_indices = get_doc_indices_from_repo_data(repo_data=repo_data)
num_docs = len(doc_indices)
for i, doc_index in enumerate(doc_indices, start=1):
doc_title = str(doc_index["title"])
if args.verbose:
print(f"[{i}/{num_docs}] {repo_name}/{doc_title}")
doc_data = get_doc_data_from_yuque(
yuque=yuque, namespace=repo_data["namespace"], slug=doc_index["url"]
)
doc_content = get_doc_content_from_doc_data(doc_data=doc_data)
doc_title = convert_string_to_valid_window_path(doc_title)
doc_file = output_dir.joinpath(repo_name, doc_title + ".md")
doc_file.parent.mkdir(exist_ok=True)
doc_file.write_text("\n".join(doc_content), encoding="utf-8")
if args.verbose:
print(f"[{i}/{num_docs}] 文档保存为 {doc_file}")
if __name__ == "__main__":
main()