Files
CET-Notice-Monitor/main.py
T
2026-08-05 12:12:41 +08:00

104 lines
2.2 KiB
Python

import os
import json
import hashlib
from datetime import datetime, timezone
import requests
from bs4 import BeautifulSoup
URL = "https://cet.neea.edu.cn/"
STATE_FILE = "data/state.json"
PUSH_URL = "https://push.showdoc.com.cn/server/api/push"
def fetch_notices():
r = requests.get(URL, headers={"User-Agent": "Mozilla/5.0"}, timeout=20)
r.encoding = "utf-8"
soup = BeautifulSoup(r.text, "lxml")
result = []
for a in soup.find_all("a"):
title = a.get_text(strip=True)
if title and any(k in title for k in ["四六级", "CET", "成绩", "报名", "考试", "准考证"]):
result.append({
"title": title,
"url": a.get("href")
})
return result[:20]
def h(item):
return hashlib.sha256(
(item["title"] + str(item["url"])).encode()
).hexdigest()
def load_state():
if not os.path.exists(STATE_FILE):
return {"items": []}
with open(STATE_FILE, encoding="utf-8") as f:
return json.load(f)
def save_state(items):
os.makedirs("data", exist_ok=True)
with open(STATE_FILE, "w", encoding="utf-8") as f:
json.dump({
"items": items,
"last_check_utc": datetime.now(timezone.utc).isoformat()
}, f, ensure_ascii=False, indent=2)
def push(items):
token = os.getenv("PUSH_TOKEN")
if not token:
print("PUSH_TOKEN missing")
return False
content = "📢 CET四六级新通知\n\n"
for x in items:
content += f"【{x['title']}\n{x['url']}\n\n"
r = requests.post(
PUSH_URL,
data={
"token": token,
"title": "四六级新通知",
"content": content
},
timeout=10
)
print(r.status_code, r.text)
return r.ok
def main():
notices = fetch_notices()
state = load_state()
old = {x["hash"] for x in state.get("items", [])}
force = os.getenv("FORCE_PUSH", "false").lower() == "true"
new = []
for item in notices:
item["hash"] = h(item)
if force or item["hash"] not in old:
new.append(item)
if new:
if push(new):
save_state(notices)
else:
print("no update")
if __name__ == "__main__":
main()