-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (67 loc) · 2.43 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
import re
from openai import OpenAI
from config import CONFIG
from slack import to_slack_json, write_to_file
from telegram import to_telegram_json
def init():
CONFIG["posts_folder"].mkdir(exist_ok=True)
diff_index = len([*CONFIG["diffs_folder"].iterdir()])
diff_file = CONFIG["diffs_folder"] / f"{diff_index}.txt"
post_index = len([*CONFIG["posts_folder"].iterdir()]) + 1
post_file = CONFIG["posts_folder"] / f"{post_index}.md"
previous_post_file = CONFIG["posts_folder"] / f"{post_index - 1}.md"
diff = diff_file.read_text(encoding="utf-8")
previous_post = previous_post_file.read_text(encoding="utf-8") if previous_post_file.exists() else None
return {
"diff": diff,
"previous_post": previous_post,
"post": post_file,
}
def get_prompt(init_data):
if init_data["previous_post"]:
post_description = CONFIG["next_post_template"]
else:
post_description = CONFIG["first_post_template"]
return CONFIG["prompt_template"].format(
post_description=post_description,
diff=init_data["diff"],
project_description=CONFIG["repo"]["description"],
repo_url=CONFIG["repo"]["html_url"],
previous_post=init_data["previous_post"] or "No previous post",
)
def clean_content(content):
# replace Markdown links with plain text links
link_regex = re.compile(r"\[([^]]+)]\(([^)]+)\)")
content = link_regex.sub(r"\2", content)
return content
def main():
init_data = init()
prompt = get_prompt(init_data)
client = OpenAI(api_key=CONFIG["open_ai_key"])
response = client.chat.completions.create(
model=CONFIG["openai_model"],
messages=[
{"role": "system", "content": CONFIG["system_message"]},
{"role": "user", "content": prompt},
]
)
content = response.choices[0].message.content
content = clean_content(content)
init_data["post"].write_text(content, encoding="utf-8")
slack_json = to_slack_json(
content,
init_data["post"],
CONFIG["repo"]["full_name"],
CONFIG["base_repo_url"]
)
telegram_json = to_telegram_json(
content,
init_data["post"],
CONFIG["repo"]["full_name"],
CONFIG["base_repo_url"],
CONFIG["telegram_chat_id"]
)
write_to_file(CONFIG["slack_file"], slack_json)
write_to_file(CONFIG["telegram_file"], telegram_json)
if __name__ == "__main__":
main()