qnloft-spider/PT/pt_operation.py

184 lines
6.1 KiB
Python
Raw Permalink Normal View History

2023-12-26 09:35:00 +00:00
import json
2024-01-08 10:23:55 +00:00
import logging
2023-12-25 16:26:55 +00:00
import sys
2023-12-20 09:53:57 +00:00
2023-12-25 16:26:55 +00:00
from loguru import logger
import time
import requests
2023-12-20 09:53:57 +00:00
import toml
2023-12-26 09:35:00 +00:00
from bs4 import BeautifulSoup
2023-12-25 16:26:55 +00:00
2024-01-01 15:47:59 +00:00
from main_notify import pushme, pushplus_bot
2023-12-29 02:14:40 +00:00
2023-12-27 03:09:52 +00:00
'''
0 0 15 * * ?
'''
2023-12-28 09:44:24 +00:00
2023-12-20 09:53:57 +00:00
class PtOperation:
2023-12-25 16:26:55 +00:00
def __init__(self):
2024-01-14 16:35:01 +00:00
logger.add("../log/PtOperation_{time:YYYY-MM-DD}.log", rotation="1 day", level="INFO")
2023-12-25 16:26:55 +00:00
logger.add(sys.stderr, level="INFO")
2023-12-27 03:09:52 +00:00
self.toml_file = 'PT/pt_config.toml'
2023-12-25 16:26:55 +00:00
self.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,zh-CN;q=0.9',
'cache-control': 'max-age=0',
'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'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/119.0.0.0 Safari/537.36',
}
2023-12-20 09:53:57 +00:00
2024-11-22 01:15:58 +00:00
async def attendances(self, section_data):
2023-12-25 16:26:55 +00:00
"""
签到
:param cookie:
:param url:
:return:
"""
2024-11-22 01:15:58 +00:00
section_name, url, cookie, attendance_uri = section_data.get('name'), \
section_data.get('url'), \
section_data.get('cookie'), \
section_data.get('attendance_uri')
2024-11-20 08:54:13 +00:00
res_text, log_txt = "", ""
2023-12-25 16:26:55 +00:00
self.headers["cookie"] = cookie
2024-01-04 08:25:02 +00:00
# cookie不为空时候可以签到
if cookie is not None and len(cookie.strip()) > 0:
# 获取网站自定义的签到链接
attendance_uri = section_data.get('attendance_uri')
if attendance_uri is not None and len(attendance_uri.strip()) > 0:
url = url + attendance_uri
else:
url = url + "/attendance.php"
2024-11-20 08:54:13 +00:00
self.headers["referer"] = url
for i in range(5):
2024-01-04 08:25:02 +00:00
try:
response_result = requests.get(url, headers=self.headers)
2024-11-22 01:15:58 +00:00
# print(response_result.status_code)
# print(self.headers)
# print("=" * 20)
# print(response_result.text)
2024-11-20 08:54:13 +00:00
if response_result.status_code == 200 or response_result.text in "签到成功":
res_text = '签到成功!'
2024-01-04 08:25:02 +00:00
break
2024-11-20 08:54:13 +00:00
else:
res_text = f"签到失败,网站请求结果 {response_result.status_code}"
break
except Exception:
2024-01-04 08:25:02 +00:00
time.sleep(2)
2024-11-20 08:54:13 +00:00
res_text = f"{i + 1} 次出现错误,请关注!!!"
finally:
log_txt = f"开始对 [{section_name}] 进行签到操作! 请求连接为:[{url}],签到结果:[{res_text}]"
logger.info(log_txt)
2024-01-04 08:25:02 +00:00
else:
2024-11-20 08:54:13 +00:00
# log_txt = f"开始对 [{section_name}] 进行签到操作! 请求连接为:[{url}],签到结果:[{res_text}]"
# logger.error(log_txt)
pushme("签到错误提示:" + section_name, log_txt)
pushplus_bot("签到错误提示:" + section_name, log_txt)
2024-01-04 08:25:02 +00:00
self.headers["cookie"] = ""
2023-12-20 09:53:57 +00:00
2024-11-22 01:15:58 +00:00
async def signup(self, section_data):
2023-12-25 16:26:55 +00:00
"""
是否开注册
:return:
"""
2024-11-22 01:15:58 +00:00
section_name, url = section_data.get('name'), section_data.get('url')
2023-12-25 16:26:55 +00:00
request_url = url + "/signup.php"
text = f"网站名:{section_name}, 网址:{request_url}"
2023-12-26 09:35:00 +00:00
logger.info(f"开始 -->> {text}")
2023-12-26 15:09:00 +00:00
html = self.request_get(request_url, text)
if len(html) == 0:
return
try:
# 打印结果
2023-12-28 09:44:24 +00:00
if "再次输入密码" in html and "submit" in html:
2023-12-29 02:14:40 +00:00
remind_me = f"现在 {text} 正在开放注册,请前往~"
pushme("开注提醒" + section_name, remind_me)
pushplus_bot("开注提醒" + section_name, remind_me)
logger.info(remind_me)
2024-03-26 10:27:49 +00:00
elif "对不起" in html or ("关闭" in html and "自由注册" in html) or "维护" in html:
remind_me = f"现在 {text} 关闭注册,请知晓~"
logger.info(remind_me)
2023-12-26 15:09:00 +00:00
else:
soup = BeautifulSoup(html, 'html.parser')
text_content = soup.find(class_='text').get_text()
print(text_content)
2024-11-20 08:54:13 +00:00
except Exception:
2023-12-26 15:09:00 +00:00
logger.error(f"{text} , 页面无法解析,请知晓!!!")
logger.info("=" * 100)
def request_get(self, url, text):
self.headers["Referer"] = url
for _ in range(5):
try:
2024-01-08 10:23:55 +00:00
response = requests.get(url, headers=self.headers, timeout=5 * 60)
2024-03-26 10:27:49 +00:00
response.encoding = "utf-8"
2023-12-26 15:09:00 +00:00
if response.status_code == 403 or response.text in "Just a moment":
return self.flaresolverr_get(url, text)
elif response.status_code == 200:
return response.text
else:
2024-11-20 08:54:13 +00:00
logger.error(f"{text} , 出现错误code码是{response.status_code}")
2023-12-31 03:01:02 +00:00
return ""
2024-11-20 08:54:13 +00:00
except Exception:
2023-12-26 15:09:00 +00:00
time.sleep(2)
else:
logger.error(f"{text} , 5次出现错误无法访问")
2023-12-28 09:44:24 +00:00
self.headers["Referer"] = ""
2023-12-27 05:25:12 +00:00
return ""
2023-12-26 15:09:00 +00:00
def flaresolverr_get(self, url, text):
2024-01-08 10:23:55 +00:00
# 5 * 1000 * 60 代表 5分钟
2023-12-25 16:26:55 +00:00
for _ in range(5):
try:
2023-12-26 09:35:00 +00:00
flaresolverr_url = "http://152.136.50.100:7024/v1"
payload = json.dumps({
"cmd": "request.get",
2023-12-26 15:09:00 +00:00
"url": url,
2024-01-08 10:23:55 +00:00
"maxTimeout": 5 * 1000 * 60
2023-12-26 09:35:00 +00:00
})
headers = {
'Content-Type': 'application/json'
}
response = requests.post(flaresolverr_url, headers=headers, data=payload)
res = json.loads(response.text)
if res['status'] == 'ok' and res['solution']['status'] == 200:
2024-03-26 10:27:49 +00:00
logging.info(f"最终耗时:{(res['endTimestamp'] - res['startTimestamp']) / 1000 / 60:.2f} 分钟")
2023-12-26 15:09:00 +00:00
return res['solution']['response']
2024-01-08 10:23:55 +00:00
elif res['status'] == 'error':
logger.error(f"{text} , 访问返回 {res['message']} ")
return ""
2024-11-20 08:54:13 +00:00
except Exception:
2023-12-25 16:26:55 +00:00
time.sleep(2)
else:
logger.error(f"{text} , 5次出现错误无法访问")
2023-12-27 05:25:12 +00:00
return ""
2023-12-20 09:53:57 +00:00
2023-12-25 16:26:55 +00:00
def opt(self):
try:
with open(self.toml_file, 'r', encoding='utf-8') as file:
config_data = toml.load(file)
# 迭代每个 section
for section_name, section_data in config_data.items():
2024-01-01 15:42:45 +00:00
url, cookie, flag = section_data.get('url'), section_data.get('cookie'), section_data.get('flag')
if flag != 1:
2024-03-01 08:57:33 +00:00
print(f"Processing section: {section_name} --- {section_data.get('url')}")
2024-01-04 08:25:02 +00:00
# 签到
2024-11-22 01:15:58 +00:00
self.attendances(section_data)
2024-01-01 15:42:45 +00:00
# 检测是否可以注册
2024-11-22 01:15:58 +00:00
self.signup(section_data)
2023-12-25 16:26:55 +00:00
except FileNotFoundError:
print(f"Error: The file '{self.toml_file}' was not found.")
except toml.TomlDecodeError as e:
print(f"Error decoding TOML: {e}")