修正 ShowDoc Push 地址

This commit is contained in:
2026-08-05 12:15:35 +08:00 Unverified
parent 7f31448df5
commit c4af664261
2 changed files with 88 additions and 42 deletions
+7 -4
View File
@@ -18,18 +18,21 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v4 - name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-python@v5 - name: Setup Python
uses: actions/setup-python@v5
with: with:
python-version: "3.12" python-version: "3.12"
- run: pip install -r requirements.txt - name: Install
run: pip install -r requirements.txt
- name: Run monitor - name: Run monitor
env: env:
PUSH_TOKEN: ${{ secrets.PUSH_TOKEN }} PUSH_TOKEN: ${{ secrets.PUSH_TOKEN }}
FORCE_PUSH: ${{ inputs.force_push }} FORCE_PUSH: ${{ github.event.inputs.force_push }}
run: python main.py run: python main.py
- name: Commit state UTC - name: Commit state UTC
+65 -22
View File
@@ -7,13 +7,17 @@ from datetime import datetime, timezone
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
URL = "https://cet.neea.edu.cn/" URL = "https://cet.neea.edu.cn/"
STATE_FILE = "data/state.json" STATE_FILE = "data/state.json"
PUSH_URL = "https://push.showdoc.com.cn/server/api/push"
def fetch_notices(): def fetch_notices():
r = requests.get(URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=20) headers = {"User-Agent": "Mozilla/5.0"}
for retry in range(3):
try:
r = requests.get(URL, headers=headers, timeout=20)
r.encoding = "utf-8" r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "lxml") soup = BeautifulSoup(r.text, "lxml")
@@ -21,30 +25,42 @@ def fetch_notices():
for a in soup.find_all("a"): for a in soup.find_all("a"):
title = a.get_text(strip=True) title = a.get_text(strip=True)
if title and any(k in title for k in ["四六级", "CET", "成绩", "报名", "考试", "准考证"]): href = a.get("href")
if title and any(k in title for k in [
"四六级", "CET", "成绩",
"报名", "考试", "准考证"
]):
result.append({ result.append({
"title": title, "title": title,
"url": a.get("href") "url": href
}) })
return result[:20] return result[:20]
except Exception as e:
print(f"获取失败 {retry + 1}/3:", e)
def h(item): return None
def make_hash(item):
return hashlib.sha256( return hashlib.sha256(
(item["title"] + str(item["url"])).encode() (item["title"] + str(item["url"])).encode("utf-8")
).hexdigest() ).hexdigest()
def load_state(): def load_state():
if not os.path.exists(STATE_FILE): if not os.path.exists(STATE_FILE):
return {"items": []} return {"items": []}
with open(STATE_FILE, encoding="utf-8") as f:
with open(STATE_FILE, "r", encoding="utf-8") as f:
return json.load(f) return json.load(f)
def save_state(items): def save_state(items):
os.makedirs("data", exist_ok=True) os.makedirs("data", exist_ok=True)
with open(STATE_FILE, "w", encoding="utf-8") as f: with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump({ json.dump({
"items": items, "items": items,
@@ -54,49 +70,76 @@ def save_state(items):
def push(items): def push(items):
token = os.getenv("PUSH_TOKEN") token = os.getenv("PUSH_TOKEN")
if not token: if not token:
print("PUSH_TOKEN missing") print("ERROR: PUSH_TOKEN 未配置")
return False return False
content = "📢 CET四六级新通知\n\n" content = "📢 CET四六级新通知\n\n"
for x in items: for item in items:
content += f"{x['title']}\n{x['url']}\n\n" content += (
f"{item['title']}\n"
f"{item['url']}\n\n"
)
# ShowDoc Push token 在 URL 路径中
url = f"https://push.showdoc.com.cn/server/api/push/{token}"
try:
r = requests.post( r = requests.post(
PUSH_URL, url,
data={ data={
"token": token,
"title": "四六级新通知", "title": "四六级新通知",
"content": content "content": content
}, },
timeout=10 timeout=10
) )
print(r.status_code, r.text) print("Push status:", r.status_code)
return r.ok print("Push response:", r.text)
return r.status_code == 200
except Exception as e:
print("Push异常:", e)
return False
def main(): def main():
notices = fetch_notices() notices = fetch_notices()
if notices is None:
print("获取公告失败")
return
state = load_state() state = load_state()
old = {x["hash"] for x in state.get("items", [])}
old_hash = {
x["hash"]
for x in state.get("items", [])
}
force = os.getenv("FORCE_PUSH", "false").lower() == "true" force = os.getenv("FORCE_PUSH", "false").lower() == "true"
new = [] new_items = []
for item in notices: for item in notices:
item["hash"] = h(item) item["hash"] = make_hash(item)
if force or item["hash"] not in old:
new.append(item)
if new: if force or item["hash"] not in old_hash:
if push(new): new_items.append(item)
if new_items:
print("发现通知:", len(new_items))
if push(new_items):
print("推送成功")
save_state(notices) save_state(notices)
else: else:
print("no update") print("推送失败,不更新状态")
else:
print("无新增")
if __name__ == "__main__": if __name__ == "__main__":