2023-03-15 15:43:36 +08:00
|
|
|
import base64
|
|
|
|
import os
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
2023-03-20 21:40:23 +08:00
|
|
|
import pkg.utils.network as network
|
|
|
|
|
2023-03-15 15:43:36 +08:00
|
|
|
|
|
|
|
def read_latest() -> str:
|
|
|
|
resp = requests.get(
|
|
|
|
url="https://api.github.com/repos/RockChinQ/QChatGPT/contents/res/announcement",
|
2023-03-20 21:40:23 +08:00
|
|
|
proxies=network.wrapper_proxies()
|
2023-03-15 15:43:36 +08:00
|
|
|
)
|
|
|
|
obj_json = resp.json()
|
|
|
|
b64_content = obj_json["content"]
|
|
|
|
# 解码
|
|
|
|
content = base64.b64decode(b64_content).decode("utf-8")
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def read_saved() -> str:
|
|
|
|
# 已保存的在res/announcement_saved
|
|
|
|
# 检查是否存在
|
|
|
|
if not os.path.exists("res/announcement_saved"):
|
2023-04-02 16:30:42 +08:00
|
|
|
with open("res/announcement_saved", "w", encoding="utf-8") as f:
|
2023-03-15 15:43:36 +08:00
|
|
|
f.write("")
|
|
|
|
|
2023-04-02 16:30:42 +08:00
|
|
|
with open("res/announcement_saved", "r", encoding="utf-8") as f:
|
2023-03-15 15:43:36 +08:00
|
|
|
content = f.read()
|
|
|
|
|
|
|
|
return content
|
|
|
|
|
|
|
|
|
|
|
|
def write_saved(content: str):
|
|
|
|
# 已保存的在res/announcement_saved
|
2023-04-02 16:30:42 +08:00
|
|
|
with open("res/announcement_saved", "w", encoding="utf-8") as f:
|
2023-03-15 15:43:36 +08:00
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_new() -> str:
|
|
|
|
latest = read_latest()
|
|
|
|
saved = read_saved()
|
|
|
|
if latest.replace(saved, "").strip() == "":
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
write_saved(latest)
|
|
|
|
return latest.replace(saved, "").strip()
|