-
Notifications
You must be signed in to change notification settings - Fork 15
/
myblog.py
106 lines (87 loc) · 3.37 KB
/
myblog.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
# -*- coding: utf-8 -*-
# @Time : 2022/12/03 16:42
# @Author : huangwb8; Suzuran
# @File : myblog.py
# @Function: Update an existing post in WordPress with a local Markdown file
# @Software: VSCode
# @Reference: original
# ===============================Dependency
from m2w import read_json_as_dict, up
import sys
import asyncio
import time
# ===============================Parameters
# Please adjust the parameters according to the actual situation.
# m2w version: 2.5.7+
# The path of the config folder, where contains user.json and legacy*.json
path_m2w = "E:/我的坚果云/样式备份/网站/m2w 2.5"
# Whether to force uploading a new post.
# `force_upload=False` is suggested for routine maintaining.
# `force_upload=True` is suggested for intensive uploading for a brand-new site.
force_upload = False
# Whether to report running messages.
verbose = True
# Whether to update the last update time of the post. Only work in REST API mode.
last_update_time_change = False
# Retry time when meeting failure
max_retries = 10
# ===============================Program
async def main():
path_user_json = path_m2w + "/config/user.json"
websites = read_json_as_dict(path_user_json)
for i in websites:
# Select a WordPress website
website = websites[i]
# Parameters of the website
domain = website["domain"]
username = website["username"]
path_markdown = website["path_markdown"]
post_metadata = website["post_metadata"]
path_legacy_json = path_m2w + website["path_legacy_json"] + "_" + i + ".json"
# Whether use REST API mode
use_rest_api = (
"application_password" in website
and len(website["application_password"]) > 10
)
if not use_rest_api and "password" not in website:
print("API or password are missing. Please add one of them. Stop m2w!")
sys.exit(0)
elif not use_rest_api and "password" in website:
rest_api = False
application_password = None
password = website["password"]
elif use_rest_api and "password" in website:
print(
"Warning: You have REST API. Password would be ignored. You can remove password in the 'user.json' to make the use of m2w safer!"
)
rest_api = True
application_password = website["application_password"]
password = None
else:
rest_api = True
application_password = website["application_password"]
password = None
# Connect the WordPress website
print("========Website: " + i)
await up(
# The path of files
path_markdown = path_markdown,
path_legacy_json = path_legacy_json,
# Website data
domain = domain,
username = username,
password = password,
application_password = application_password,
post_metadata = post_metadata,
# Parameters
last_update_time_change = last_update_time_change,
force_upload = force_upload,
verbose = verbose,
rest_api = rest_api,
max_retries = max_retries
)
if __name__ == "__main__":
start = time.time()
asyncio.run(main())
end = time.time()
print(f"All done! Total time : {round(end - start, 3)}s")