forked from Doragd/Algorithm-Practice-in-Industry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintain.py
178 lines (163 loc) · 4.95 KB
/
maintain.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
import re
import ast
import os
import random
import json
import argparse
import openpyxl
import requests
import datetime
FEISHU_URL = os.environ.get("FEISHU_URL", None)
def set_args():
parser = argparse.ArgumentParser()
parser.add_argument("--issue", "-i", type=str, help="input issue", required=True)
args = parser.parse_args()
return args
def parse_issue(issue):
try:
issue = issue.replace("\\n", "").replace("\\r", "")
info = ast.literal_eval(issue)
print(info)
assert isinstance(info, list)
assert len(info) > 0 and info[0].get("公司") and info[0].get("内容") and info[0].get("标签") and info[0].get("时间") and info[0].get("链接")
except:
raise Exception("[-] Wrong input!")
return info
def write_item(wb, item):
columns = ["公司", "内容", "标签", "时间"]
values = [item[column] for column in columns]
ws = wb.active
ws.append(values)
ws.cell(row=ws.max_row, column=2).style = 'Hyperlink'
ws.cell(row=ws.max_row, column=2).hyperlink = item["链接"]
print("[+] Write item: {}".format(values))
def update_excel(args):
print("[+] Add new items into excel...")
wb = openpyxl.load_workbook("source.xlsx")
info = parse_issue(args.issue)
for item in info:
assert item.keys() == {"公司", "内容", "标签", "时间", "链接"}
write_item(wb, item)
wb.save("source.xlsx")
print("[+] Add items Done!")
def update_readme(args, info=None):
print("[+] Add new items into readme...")
if info is None:
info = parse_issue(args.issue)
with open("README.md", "r", encoding="utf-8") as f:
lines = f.readlines()
# 查找表格位置
table_title = "## 大厂实践文章"
table_index = None
for i, line in enumerate(lines):
if line.strip() == table_title:
table_index = i
break
# 如果找到表格位置,则更新表格
if table_index is not None:
j = table_index + 2
while j < len(lines) and not re.search(r"\|(\s-+\s\|)+", lines[j]):
j += 1
index = j + 1
newlines = []
for item in info:
newline = "".join("| {} | [{}]({}) | {} | {} |\n".format(
item["公司"], item["内容"], item["链接"], item["标签"], item["时间"]))
newlines.append(newline)
lines = lines[:index] + newlines + lines[index:]
with open("README.md", "w", encoding="utf-8") as f:
f.writelines(lines)
print("[+] Add items Done!")
else:
print("[-] Table not found!")
def update_message(args):
infos = parse_issue(args.issue)
today = datetime.datetime.now().strftime('%Y-%m-%d')
title = f"🌸大厂实践文章自动更新@{today}"
content = []
for info in infos:
emoji = random.choice("🌱🌿🍀🪴🎋🍃🪷🌸✨")
meta_info = f"🥹 {info['公司']} 📆 {info['时间']} 🍉 {info['标签']}"
info_title = f"✅ {info['内容']}"
line_len = max(len(meta_info), len(info_title))
sepline = emoji * line_len
content.append(
[{
"tag": "text",
"text": ""
}]
)
# content.append(
# [{
# "tag": "text",
# "text": sepline
# }]
# )
# content.append(
# [{
# "tag": "text",
# "text": ""
# }]
# )
content.append(
[{
"tag": "text",
"text": meta_info
}]
)
content.append(
[{
"tag": "text",
"text": ""
}]
)
content.append(
[{
"tag": "a",
"text": info_title,
"href": f"{info['链接']}"
}]
)
content.append(
[{
"tag": "text",
"text": ""
}]
)
content.append(
[{
"tag": "text",
"text": "-----",
}]
)
content.append(
[{
"tag": "a",
"text": "➡️ 点击查看完整大厂实践文章列表",
"href": "https://github.com/Doragd/Algorithm-Practice-in-Industry"
}]
)
send_feishu_message(title, content, url=FEISHU_URL)
def send_feishu_message(title, content, url=FEISHU_URL):
raw_data = {
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": title,
"content": content
}
}
}
}
body = json.dumps(raw_data)
headers = {"Content-Type":"application/json"}
ret = requests.post(url=url, data=body, headers=headers)
print(ret.text)
def main():
args = set_args()
update_excel(args)
update_readme(args)
update_message(args)
if __name__ == "__main__":
main()