import mysql.connector
import re
import urllib.request
from lxml import etree
import urllib3
import requests
import json
import time
from datetime import datetime, timedelta
urllib3.disable_warnings()
config = {'host': '',
'user': '',
'password': '',
'port': 3306,
'database': 'ztbu_notice',
'charset': 'utf8mb4'
}
def acquire_link_data() :
cnn = mysql.connector.connect(**config)
cursor = cnn.cursor()
sql = "select * from link_data"
cursor.execute(sql)
data = cursor.fetchall()
for i in data :
link, headline, date, text = acquire_link_webdata(str(i[4]))
if i[5] != headline :
update_link_data(i[1], headline, date)
acquire_notice_list_user_send(i[1], headline, link, date, text)
else :
print(f"{i[2]},无更新!")
def acquire_notice_list_user_send(linkcode, headline, link, date, text) :
cnn = mysql.connector.connect(**config)
cursor = cnn.cursor()
sql = f"select * from notice_list where {linkcode} = 1"
cursor.execute(sql)
data = cursor.fetchall()
for i in data :
massage_send(i[1], i[2], i[3], headline, link, date, text)
time.sleep(1)
def update_link_data(linkcode, Title, Time) :
cnn = mysql.connector.connect(**config)
cursor = cnn.cursor()
sql = f"update link_data set Title='{Title}' ,Time='{Time}' where code like '%{linkcode}%'"
cursor.execute(sql)
cnn.commit()
print(f"高校:{linkcode}保留数据更新成功!")
return True
def acquire_link_webdata(url) :
header = {
'Connection': 'close' ,
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36"}
response = requests.get(url=url, headers=header, timeout=10, verify=False)
response.encoding = 'utf-8'
notice_data = re.findall(
'<a href="(.*?)" target="_blank" title="(.*?)">(.*?)</a><span class="datatime">(.*?)</span>', response.text)
notice_data = notice_data[0]
link = notice_data[0]
headline = notice_data[1]
date = notice_data[3]
request = urllib.request.Request(url=link, headers=header)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
parse_html = etree.HTML(content)
link_content_data = parse_html.xpath('//div[@class="layout_txtcontent_content"]//text()')
text = ""
for i in link_content_data :
text += i
link_data_fujian = re.findall(
'<a href="(.*?)" target="_blank" style="font-family: 微软雅黑; font-size: 14pt;">(.*?)</a>', content)
for i in link_data_fujian :
text += f'<a href="{i[0]}">{i[1]}</a><br>'
text += i[1] + "\n"
return link, headline, date, text
def massage_send(user_id, push_select, push_token, web_head, web_link, web_time, web_text) :
if push_select == "1" :
url = "http://www.pushplus.plus/send/"
data = {
"token": push_token ,
"title": web_head ,
"content": web_text ,
"template": "html"
}
res = requests.post(url, data=data)
name = "pushplus"
print(f"用户:{user_id}\n状态:{res}推送成功!\n推送方式:{name}\n")
elif push_select == "2" :
name = "Wxpusher"
p = push_token.split("#")
header = {
"Content-Type": "application/json" ,
"User-Agent": "PostmanRuntime/7.26.5" ,
"Accept": "*/*" ,
"Postman-Token": "8b253f0e-292c-4b0d-8b37-86cc344bb199" ,
"Host": "wxpusher.zjiecode.com" ,
"Accept-Encoding": "gzip, deflate, br" ,
"Content-Length": "221" ,
"Connection": "keep-alive"
}
data = {
"appToken": p[0] ,
"content": web_text ,
"summary": web_head ,
"contentType": 2,
"uids": [p[1]] ,
"url": web_link,
"verifyPay": False,
}
res = requests.post("https://wxpusher.zjiecode.com/api/send/message", data=json.dumps(data), headers=header ,
timeout=10, verify=False).json()["data"][0]["status"]
print(f"用户:{user_id}\n状态:{res}推送成功!\n推送方式:{name}\n")
elif push_select == "3" :
name = "酷推"
data = web_head + "\n" + web_text
res = requests.post(url="https://push.xuthus.cc/send/" + push_token, data=data.encode('utf-8'), timeout=10 ,
verify=False).text
print(f"用户:{user_id}\n状态:{res}推送成功!\n推送方式:{name}\n")
elif push_select == "4" :
name = "Hi提醒"
server_url = f'https://api.caicenter.com/send.php?appkey={push_token}&title={web_head}&describes={web_text}&channel=1'
res = requests.post(server_url, timeout=10, verify=False)
print(f"用户:{user_id}\n状态:{res}推送成功!\n推送方式:{name}\n")
elif push_select == "5" :
name = "方糖"
server_url = f"https://sctapi.ftqq.com/{push_token}.send?title={web_head}&desp={web_text}"
res = requests.post(server_url, timeout=10, verify=False)
print(f"用户:{user_id}\n状态:{res}推送成功!\n推送方式:{name}\n")
else:
print(f"用户:{user_id} 推送失败!\n")
time.sleep(2)
def main() :
print("开始启动!")
acquire_link_data()
def timing(times) :
print(f"定时任务已启动:{times}秒")
start_time = datetime.strptime('2022-12-27 15:00:00', '%Y-%m-%d %H:%M:%S')
next_run_time = start_time + timedelta(seconds=times)
i = 1
while True :
current_time = datetime.now()
if current_time >= next_run_time :
time_tuple = time.localtime(time.time())
nowtime = "{}年{}月{}日".format(time_tuple[0], time_tuple[1], time_tuple[2])
main()
print(f"{nowtime} 第{i}次执行")
i += 1
next_run_time = current_time + timedelta(seconds=times)
else :
time.sleep(1)
if __name__ == '__main__' :
timing(30)