mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
Merge pull request #416 from RockChinQ/413-feature-json-format-anouns
[Feat] 支持JSON格式的公告
This commit is contained in:
commit
20df9ded3d
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -17,5 +17,6 @@ scenario/
|
|||
override.json
|
||||
cookies.json
|
||||
res/announcement_saved
|
||||
res/announcement_saved.json
|
||||
cmdpriv.json
|
||||
tips.py
|
5
main.py
5
main.py
|
@ -303,8 +303,9 @@ def start(first_time_init=False):
|
|||
try:
|
||||
import pkg.utils.announcement as announcement
|
||||
new_announcement = announcement.fetch_new()
|
||||
if new_announcement != "":
|
||||
logging.critical("[公告] {}".format(new_announcement))
|
||||
if len(new_announcement) > 0:
|
||||
for announcement in new_announcement:
|
||||
logging.critical("[公告] {}".format(announcement))
|
||||
except Exception as e:
|
||||
logging.warning("获取公告失败:{}".format(e))
|
||||
|
||||
|
|
|
@ -1,47 +1,68 @@
|
|||
import base64
|
||||
import os
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
import pkg.utils.network as network
|
||||
|
||||
|
||||
def read_latest() -> str:
|
||||
def read_latest() -> list:
|
||||
import pkg.utils.network as network
|
||||
resp = requests.get(
|
||||
url="https://api.github.com/repos/RockChinQ/QChatGPT/contents/res/announcement",
|
||||
url="https://api.github.com/repos/RockChinQ/QChatGPT/contents/res/announcement.json",
|
||||
proxies=network.wrapper_proxies()
|
||||
)
|
||||
obj_json = resp.json()
|
||||
b64_content = obj_json["content"]
|
||||
# 解码
|
||||
content = base64.b64decode(b64_content).decode("utf-8")
|
||||
return content
|
||||
return json.loads(content)
|
||||
|
||||
|
||||
def read_saved() -> str:
|
||||
def read_saved() -> list:
|
||||
# 已保存的在res/announcement_saved
|
||||
# 检查是否存在
|
||||
if not os.path.exists("res/announcement_saved"):
|
||||
with open("res/announcement_saved", "w", encoding="utf-8") as f:
|
||||
f.write("")
|
||||
if not os.path.exists("res/announcement_saved.json"):
|
||||
with open("res/announcement_saved.json", "w", encoding="utf-8") as f:
|
||||
f.write("[]")
|
||||
|
||||
with open("res/announcement_saved", "r", encoding="utf-8") as f:
|
||||
with open("res/announcement_saved.json", "r", encoding="utf-8") as f:
|
||||
content = f.read()
|
||||
|
||||
return content
|
||||
return json.loads(content)
|
||||
|
||||
|
||||
def write_saved(content: str):
|
||||
def write_saved(content: list):
|
||||
# 已保存的在res/announcement_saved
|
||||
with open("res/announcement_saved", "w", encoding="utf-8") as f:
|
||||
f.write(content)
|
||||
f.write(json.dumps(content, indent=4, ensure_ascii=False))
|
||||
|
||||
|
||||
def fetch_new() -> str:
|
||||
def fetch_new() -> list:
|
||||
latest = read_latest()
|
||||
saved = read_saved()
|
||||
if latest.replace(saved, "").strip() == "":
|
||||
return ""
|
||||
else:
|
||||
write_saved(latest)
|
||||
return latest.replace(saved, "").strip()
|
||||
|
||||
to_show: list = []
|
||||
|
||||
for item in latest:
|
||||
# 遍历saved检查是否有相同id的公告
|
||||
for saved_item in saved:
|
||||
if saved_item["id"] == item["id"]:
|
||||
break
|
||||
else:
|
||||
# 没有相同id的公告
|
||||
to_show.append(item)
|
||||
|
||||
write_saved(latest)
|
||||
return to_show
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
resp = requests.get(
|
||||
url="https://api.github.com/repos/RockChinQ/QChatGPT/contents/res/announcement.json",
|
||||
)
|
||||
obj_json = resp.json()
|
||||
b64_content = obj_json["content"]
|
||||
# 解码
|
||||
content = base64.b64decode(b64_content).decode("utf-8")
|
||||
print(json.dumps(json.loads(content), indent=4, ensure_ascii=False))
|
||||
|
|
Loading…
Reference in New Issue
Block a user