mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-02 03:55:55 +00:00
feat: 支持JSON格式的公告
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -17,5 +17,6 @@ scenario/
|
|||||||
override.json
|
override.json
|
||||||
cookies.json
|
cookies.json
|
||||||
res/announcement_saved
|
res/announcement_saved
|
||||||
|
res/announcement_saved.json
|
||||||
cmdpriv.json
|
cmdpriv.json
|
||||||
tips.py
|
tips.py
|
||||||
5
main.py
5
main.py
@@ -303,8 +303,9 @@ def start(first_time_init=False):
|
|||||||
try:
|
try:
|
||||||
import pkg.utils.announcement as announcement
|
import pkg.utils.announcement as announcement
|
||||||
new_announcement = announcement.fetch_new()
|
new_announcement = announcement.fetch_new()
|
||||||
if new_announcement != "":
|
if len(new_announcement) > 0:
|
||||||
logging.critical("[公告] {}".format(new_announcement))
|
for announcement in new_announcement:
|
||||||
|
logging.critical("[公告] {}".format(announcement))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.warning("获取公告失败:{}".format(e))
|
logging.warning("获取公告失败:{}".format(e))
|
||||||
|
|
||||||
|
|||||||
@@ -1,47 +1,68 @@
|
|||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
import pkg.utils.network as network
|
|
||||||
|
|
||||||
|
def read_latest() -> list:
|
||||||
def read_latest() -> str:
|
import pkg.utils.network as network
|
||||||
resp = requests.get(
|
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()
|
proxies=network.wrapper_proxies()
|
||||||
)
|
)
|
||||||
obj_json = resp.json()
|
obj_json = resp.json()
|
||||||
b64_content = obj_json["content"]
|
b64_content = obj_json["content"]
|
||||||
# 解码
|
# 解码
|
||||||
content = base64.b64decode(b64_content).decode("utf-8")
|
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
|
# 已保存的在res/announcement_saved
|
||||||
# 检查是否存在
|
# 检查是否存在
|
||||||
if not os.path.exists("res/announcement_saved"):
|
if not os.path.exists("res/announcement_saved.json"):
|
||||||
with open("res/announcement_saved", "w", encoding="utf-8") as f:
|
with open("res/announcement_saved.json", "w", encoding="utf-8") as f:
|
||||||
f.write("")
|
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()
|
content = f.read()
|
||||||
|
|
||||||
return content
|
return json.loads(content)
|
||||||
|
|
||||||
|
|
||||||
def write_saved(content: str):
|
def write_saved(content: list):
|
||||||
# 已保存的在res/announcement_saved
|
# 已保存的在res/announcement_saved
|
||||||
with open("res/announcement_saved", "w", encoding="utf-8") as f:
|
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()
|
latest = read_latest()
|
||||||
saved = read_saved()
|
saved = read_saved()
|
||||||
if latest.replace(saved, "").strip() == "":
|
|
||||||
return ""
|
to_show: list = []
|
||||||
|
|
||||||
|
for item in latest:
|
||||||
|
# 遍历saved检查是否有相同id的公告
|
||||||
|
for saved_item in saved:
|
||||||
|
if saved_item["id"] == item["id"]:
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
|
# 没有相同id的公告
|
||||||
|
to_show.append(item)
|
||||||
|
|
||||||
write_saved(latest)
|
write_saved(latest)
|
||||||
return latest.replace(saved, "").strip()
|
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))
|
||||||
|
|||||||
Reference in New Issue
Block a user