pt-msg/notify.py
Ching 03bf9415ac feat(notify.py): http 状态不为 200 时发送通知
http 状态不为 200 时发送通知

Signed-off-by: Ching <loooching@gmail.com>
2023-09-17 22:20:30 +08:00

108 lines
4.3 KiB
Python

import requests
import re
import bs4
# curl 'https://ptchdbits.co/index.php' \
# -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7' \
# -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6' \
# -H 'Cache-Control: max-age=0' \
# -H 'Connection: keep-alive' \
# -H 'Cookie: c_secure_uid=MTI1MzI2; c_secure_pass=c3fcf31ff6d4240a7502e71b56a8aa46; c_secure_ssl=eWVhaA%3D%3D; c_secure_tracker_ssl=eWVhaA%3D%3D; c_secure_login=bm9wZQ%3D%3D' \
# -H 'Referer: https://ptchdbits.co/aboutnexus.php' \
# -H 'Sec-Fetch-Dest: document' \
# -H 'Sec-Fetch-Mode: navigate' \
# -H 'Sec-Fetch-Site: same-origin' \
# -H 'Sec-Fetch-User: ?1' \
# -H 'Upgrade-Insecure-Requests: 1' \
# -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' \
# -H 'sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"' \
# -H 'sec-ch-ua-mobile: ?0' \
# -H 'sec-ch-ua-platform: "macOS"' \
# --compressed
# curl into python requests
# "Cookie": "c_secure_uid=MTI1MzI2; c_secure_pass=c3fcf31ff6d4240a7502e71b56a8aa46; c_secure_ssl=eWVhaA%3D%3D; c_secure_tracker_ssl=eWVhaA%3D%3D; c_secure_login=bm9wZQ%3D%3D",
try:
# read cookie from file
with open('cookies.txt', 'r') as f:
cookie = f.read().strip()
response = requests.get(
url="https://ptchdbits.co/index.php",
headers={
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
"Cache-Control": "max-age=0",
"Connection": "keep-alive",
"Referer": "https://ptchdbits.co/aboutnexus.php",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
"Sec-Ch-Ua": "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Google Chrome\";v=\"116\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"macOS\"",
"Accept-Encoding": "gzip",
"Cookie": cookie,
},
)
except requests.exceptions.RequestException:
print('HTTP Request failed')
if not response.status_code == 200:
print('HTTP Request failed, status code: ', response.status_code)
# 发送 bark 通知
requests.post(
url="http://bark.tunpok.com/UZ6zC82bKRjQaXiVkosVWh",
params={
"title": "chd 通知: HTTP " + response.status_code,
'group': 'pt-msg',
},
)
exit(1)
soup = bs4.BeautifulSoup(response.text, 'html.parser')
# get element by id (id="outer")
outer = soup.find(id="outer")
# 获取 outer 下第一个 table 元素
main_table = outer.find('table')
# 获取 main_table 下第一个 table 元素
table = main_table.find('table')
# 从文件中获取 max_aid
with open('max_aid', 'r') as f:
max_aid = int(f.read().strip())
max_aid_ = 0
# 获取 table 下所有 href 中有 'klappe_news' 字样的 a 标签的文字内容
# 从后往前遍历
for a_tag in reversed(table.find_all('a', href=lambda href: href and 'klappe_news' in href)):
# for a_tag in table.find_all('a', href=lambda href: href and 'klappe_news' in href):
print(a_tag.text)
# 获取 a_tag 的 href 属性
# href 属性的值为 "javascript: klappe_news('a103')"
# 正则匹配括号中的数字 id
aid = re.search(r"\('a(\d+)'\)", a_tag['href']).group(1)
# 消息内容为 id=ka+aid 的 div 元素的文字内容
div = soup.find(id=f'ka{aid}')
# 如果 aid 大于 max_aid 则更新 max_aid_
if int(aid) > max_aid:
if int(aid) > max_aid_:
max_aid_ = int(aid)
# 发送 bark 通知
requests.post(
url="http://bark.tunpok.com/UZ6zC82bKRjQaXiVkosVWh",
params={
"title": "chd 通知: " + a_tag.text,
"body": div.text,
'group': 'pt-msg',
"url": "https://ptchdbits.co/index.php"
},
)
# 保存 max_aid 到文件
with open('max_aid', 'w') as f:
f.write(str(max_aid_))