-
Notifications
You must be signed in to change notification settings - Fork 134
/
main.py
338 lines (281 loc) · 11.2 KB
/
main.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
from wordpress_xmlrpc import Client, WordPressPost
from wordpress_xmlrpc.methods.posts import GetPosts, NewPost, EditPost
from urllib.parse import urlparse
import frontmatter
import time
import os
from hashlib import md5, sha1
import json
import markdown
import re
import urllib.parse
config_file_txt = ""
if((os.path.exists(os.path.join(os.getcwd(), "diy_config.txt")) == True)):
config_file_txt = os.path.join(os.getcwd(), "diy_config.txt")
else:
config_file_txt = os.path.join(os.getcwd(), "config.txt")
config_info = {}
with open (config_file_txt, 'rb') as f:
config_info = json.loads(f.read())
username = config_info["USERNAME"]
password = config_info["PASSWORD"]
xmlrpc_php = config_info["XMLRPC_PHP"]
try:
if(os.environ["USERNAME"]):
username = os.environ["USERNAME"]
if(os.environ["PASSWORD"]):
password = os.environ["PASSWORD"]
if(os.environ["XMLRPC_PHP"]):
xmlrpc_php = os.environ["XMLRPC_PHP"]
except:
print("无法获取github的secrets配置信息,开始使用本地变量")
url_info = urlparse(xmlrpc_php)
domain_name = url_info.netloc
wp = Client(xmlrpc_php, username, password)
# 获取已发布文章id列表
def get_posts():
print(time.strftime('%Y-%m-%d-%H-%M-%S')+"开始从服务器获取文章列表...")
posts = wp.call(GetPosts({'post_type': 'post', 'number': 1000000000}))
post_link_id_list = []
for post in posts:
post_link_id_list.append({
"id": post.id,
"link": post.link
})
print(post_link_id_list)
print(len(post_link_id_list))
return post_link_id_list
# 创建post对象
def create_post_obj(title, content, link, post_status, terms_names_post_tag, terms_names_category):
post_obj = WordPressPost()
post_obj.title = title
post_obj.content = content
post_obj.link = link
post_obj.post_status = post_status
post_obj.comment_status = "open"
print(post_obj.link)
post_obj.terms_names = {
#文章所属标签,没有则自动创建
'post_tag': terms_names_post_tag,
#文章所属分类,没有则自动创建
'category': terms_names_category
}
return post_obj
# 新建文章
def new_post(title, content, link, post_status, terms_names_post_tag, terms_names_category):
post_obj = create_post_obj(
title = link,
content = content,
link = link,
post_status = post_status,
terms_names_post_tag = terms_names_post_tag,
terms_names_category = terms_names_category)
# 先获取id
id = wp.call(NewPost(post_obj))
# 再通过EditPost更新信息
edit_post(id, title,
content,
link,
post_status,
terms_names_post_tag,
terms_names_category)
# 更新文章
def edit_post(id, title, content, link, post_status, terms_names_post_tag, terms_names_category):
post_obj = create_post_obj(
title,
content,
link,
post_status,
terms_names_post_tag,
terms_names_category)
res = wp.call(EditPost(id, post_obj))
print('==edit_post==res', res)
# 获取markdown文件中的内容
def read_md(file_path):
content = ""
metadata = {}
with open(file_path) as f:
post = frontmatter.load(f)
content = post.content
metadata = post.metadata
# print("==>>", post.content)
print("===>>", post.metadata)
return (content, metadata)
# 获取特定目录的markdown文件列表
def get_md_list(dir_path):
md_list = []
dirs = os.listdir(dir_path)
for i in dirs:
if os.path.splitext(i)[1] == ".md":
md_list.append(os.path.join(dir_path, i))
print(md_list)
return md_list
# 计算sha1
def get_sha1(filename):
sha1_obj = sha1()
with open(filename, 'rb') as f:
sha1_obj.update(f.read())
result = sha1_obj.hexdigest()
print(result)
return result
# 将字典写入文件
def write_dic_info_to_file(dic_info, file):
dic_info_str = json.dumps(dic_info)
file = open(file, 'w')
file.write(dic_info_str)
file.close()
return True
# 将文件读取为字典格式
def read_dic_from_file(file):
file_byte = open(file, 'r')
file_info = file_byte.read()
dic = json.loads(file_info)
file_byte.close()
return dic
# 获取md_sha1_dic
def get_md_sha1_dic(file):
result = {}
if(os.path.exists(file) == True):
result = read_dic_from_file(file)
else:
write_dic_info_to_file({}, file)
return result
# 更新文件hash
def update_md_sha1_dic(hash_file, md_file):
md_sha1_dic = read_dic_from_file(hash_file)
key = os.path.basename(md_file).split(".")[0]
value = get_sha1(md_file)
md_sha1_dic[key] = {
"hash_value": value,
"file_name": key,
"encode_file_name": urllib.parse.quote(key, safe='').lower(),
"update_time": time.strftime('%Y-%m-%d-%H-%M-%S')
}
write_dic_info_to_file(md_sha1_dic, hash_file)
# 重建md_sha1_dic,将结果写入.md_sha1
def rebuild_md_sha1_dic(file, md_dir):
md_sha1_dic = {}
md_list = get_md_list(md_dir)
for md in md_list:
key = os.path.basename(md).split(".")[0]
value = get_sha1(md)
md_sha1_dic[key] = {
"hash_value": value,
"file_name": key,
"encode_file_name": urllib.parse.quote(key, safe='').lower()
}
write_dic_info_to_file(md_sha1_dic, file)
def post_link_id_list_2_link_id_dic(post_link_id_list):
link_id_dic = {}
for post in post_link_id_list:
link_id_dic[post["link"]] = post["id"]
return link_id_dic
def href_info(link):
return "<br/><br/><br/>\n\n\n\n## 本文永久更新地址: \n[" + link + "](" + link + ")"
# 在README.md中插入信息文章索引信息,更容易获取google的收录
def insert_index_info_in_readme():
# 获取_posts下所有markdown文件
md_list = get_md_list(os.path.join(os.getcwd(), "_posts"))
new_posts = wp.call(GetPosts({'post_type': 'post', 'number': 1000000000}))
new_post_link_id_list = []
for post in new_posts:
md_file_name = post.link.split("/")[-2] + ".md"
new_post_link_id_list.append({
"id": post.id,
"link": post.link,
"date": post.date,
"title": post.title,
"md_file_name": md_file_name
})
print('==new_post_link_id_list==', new_post_link_id_list)
# 生成插入列表
insert_info = ""
# 创建只包含markdown文件名的列表
pure_md_file_list = []
for md in md_list:
pure_md_file_list.append(os.path.basename(md))
post_num = 0
for new_post in new_post_link_id_list:
if(new_post["md_file_name"] in pure_md_file_list):
post_num = post_num + 1
insert_info = insert_info + "[" + new_post["title"] +"](" + "https://"+ domain_name + "/p/" + new_post["md_file_name"].split(".")[0] +"/" + ")\n\n"
# 替换 ---start--- 到 ---end--- 之间的内容
insert_info = "---start---\n## 目录(" + time.strftime('%Y年%m月%d日') + "更新, 本仓库共管理" + str(post_num) + "篇文章" +")" +"\n" + insert_info + "---end---"
# 获取README.md内容
with open (os.path.join(os.getcwd(), "README.md"), 'r', encoding='utf-8') as f:
readme_md_content = f.read()
print(insert_info)
new_readme_md_content = re.sub(r'---start---(.|\n)*---end---', insert_info, readme_md_content)
with open (os.path.join(os.getcwd(), "README.md"), 'w', encoding='utf-8') as f:
f.write(new_readme_md_content)
print("==new_readme_md_content==>>", new_readme_md_content)
return True
def main():
# 1. 获取网站数据库中已有的文章列表
post_link_id_list = get_posts()
print(post_link_id_list)
link_id_dic = post_link_id_list_2_link_id_dic(post_link_id_list)
print(link_id_dic)
# link_id_dic_str = json.dumps(link_id_dic)
# link_id_dic_str_json_file = open(os.path.join(os.getcwd(), "link_id_dic_str" + time.strftime('%Y-%m-%d-%H-%M-%S') +".json"), 'w')
# link_id_dic_str_json_file.write(link_id_dic_str)
# link_id_dic_str_json_file.close()
# 2. 获取md_sha1_dic
# 查看目录下是否存在md_sha1.txt,如果存在则读取内容;
# 如果不存在则创建md_sha1.txt,内容初始化为{},并读取其中的内容;
# 将读取的字典内容变量名,设置为 md_sha1_dic
md_sha1_dic = get_md_sha1_dic(os.path.join(os.getcwd(), ".md_sha1"))
# 3. 开始同步
# 读取_posts目录中的md文件列表
md_list = get_md_list(os.path.join(os.getcwd(), "_posts"))
for index, md in enumerate(md_list):
print("==当前进度==", str(index) +"/"+ str(len(md_list)), '开始同步==', md)
sha1_key = os.path.basename(md).split(".")[0]
sha1_value = get_sha1(md)
# 如果sha1与md_sha1_dic中记录的相同,则打印:XX文件无需同步;
if((sha1_key in md_sha1_dic.keys()) and ("hash_value" in md_sha1_dic[sha1_key]) and (sha1_value == md_sha1_dic[sha1_key]["hash_value"])):
print("==无需同步"+"md")
# 如果sha1与md_sha1_dic中记录的不同,则开始同步
else:
# 读取md文件信息
(content, metadata) = read_md(md)
# 获取title
title = metadata.get("title", "")
terms_names_post_tag = metadata.get("tags", domain_name)
terms_names_category = metadata.get("categories", domain_name)
post_status = "publish"
link = urllib.parse.quote(sha1_key , safe='').lower()
content = markdown.markdown(content + href_info("https://"+domain_name+"/p/"+link+"/"), extensions=['tables', 'fenced_code'])
# 如果文章无id,则直接新建
print("==!!key", "https://"+domain_name+"/p/"+link+"/" )
if(("https://"+domain_name+"/p/"+link+"/" in link_id_dic.keys()) == False):
new_post(title, content, link, post_status, terms_names_post_tag, terms_names_category)
print("new_post==>>", {
"title": title,
"content": content,
"link": link,
"post_status": post_status,
"terms_names_post_tag": terms_names_post_tag,
"terms_names_category": terms_names_category
});
# 如果文章有id, 则更新文章
else:
# 获取id
id = link_id_dic["https://"+domain_name+"/p/"+link+"/"]
print("edit_post==>>", {
"id": id,
"title": title,
"content": content,
"link": link,
"post_status": post_status,
"terms_names_post_tag": terms_names_post_tag,
"terms_names_category": terms_names_category
})
edit_post(id, title, content, link, post_status, terms_names_post_tag, terms_names_category)
# 将当前更新完成的文章hash写入.md_sha1
update_md_sha1_dic(os.path.join(os.getcwd(), ".md_sha1"), md)
print("==完成同步"+ md)
# 4. 将链接信息写入insert_index_info_in_readme
insert_index_info_in_readme()
# 5. 为_post创建副本包含所有图片
main()