first init
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import os,json,hashlib
|
||||
from datetime import datetime,timezone
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
URL="https://cet.neea.edu.cn/"
|
||||
STATE="data/state.json"
|
||||
|
||||
def category(t):
|
||||
if "报名" in t:return "📝 报名"
|
||||
if "成绩" in t:return "📊 成绩"
|
||||
if "准考证" in t:return "🎫 准考证"
|
||||
if "考试" in t:return "📝 考试安排"
|
||||
return "📢 其他"
|
||||
|
||||
def fetch():
|
||||
r=requests.get(URL,headers={"User-Agent":"Mozilla/5.0"},timeout=20)
|
||||
r.encoding="utf-8"
|
||||
soup=BeautifulSoup(r.text,"lxml")
|
||||
out=[]
|
||||
for a in soup.find_all("a"):
|
||||
t=a.get_text(strip=True)
|
||||
if t and any(k in t for k in ["四六级","CET","成绩","报名","考试","准考证"]):
|
||||
out.append({"title":t,"url":a.get("href"),"category":category(t)})
|
||||
return out[:20]
|
||||
|
||||
def load():
|
||||
if not os.path.exists(STATE): return []
|
||||
return json.load(open(STATE,encoding="utf-8")).get("items",[])
|
||||
|
||||
def save(items):
|
||||
os.makedirs("data",exist_ok=True)
|
||||
json.dump({"items":items,"last_check":datetime.now(timezone.utc).isoformat()},open(STATE,"w",encoding="utf-8"),ensure_ascii=False,indent=2)
|
||||
|
||||
def h(x):
|
||||
return hashlib.sha256((x["title"]+str(x["url"])).encode()).hexdigest()
|
||||
|
||||
def push(items):
|
||||
token=os.getenv("PUSH_TOKEN")
|
||||
if not token:return
|
||||
msg="📢 CET四六级新通知\n\n"
|
||||
for x in items:
|
||||
msg+=f"{x['category']}\n{x['title']}\n{x['url']}\n\n"
|
||||
requests.post("https://push.showdoc.com.cn/server/api/push",
|
||||
data={"title":"四六级新通知","content":msg,"token":token},timeout=10)
|
||||
|
||||
def main():
|
||||
current=fetch()
|
||||
old={x.get("hash") for x in load()}
|
||||
fresh=[]
|
||||
saved=[]
|
||||
for x in current:
|
||||
x["hash"]=h(x)
|
||||
saved.append(x)
|
||||
if x["hash"] not in old:
|
||||
fresh.append(x)
|
||||
if fresh: push(fresh)
|
||||
save(saved)
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user