PyBot/Core.py

155 lines
4.8 KiB
Python
Raw Normal View History

2024-12-03 00:03:14 +08:00
import signal
2024-12-04 17:21:26 +08:00
from datetime import datetime, timedelta
2024-12-03 00:03:14 +08:00
import sys
import time
import yaml
2024-12-04 09:14:50 +08:00
import requests
from SendBot import SendToFeishu
2024-12-03 00:03:14 +08:00
from media.common import run
from media.freebuf import freebuf_main
from media.xianzhi import xianzhi_main
from GotoSend_4hou import Src_4hou
from GotoSend_anquanke import Src_anquanke
from GotoSend_doonsec import Src_doonsec
2024-12-04 17:21:26 +08:00
from GotoSend_xianzhi import Src_xianzhi
2024-12-05 00:03:51 +08:00
from GotoSend_freebuf import Src_freebuf
from GotoSend_qianxin import Src_qianxin
2024-12-03 00:03:14 +08:00
# 加载参数
def get_params():
with open('./config.yaml', 'r', encoding="utf-8") as file:
config = yaml.safe_load(file)
2024-12-04 09:14:50 +08:00
sleep_time = int(f"{config['sleep_time']}")
e_hour = int(f"{config['e_hour']}")
2024-12-04 17:21:26 +08:00
return sleep_time, e_hour
2024-12-04 17:21:26 +08:00
sleep_time, e_hour = get_params()
2024-12-03 00:03:14 +08:00
def crab_job():
print("正在启动各爬虫并获取资源中...")
run()
xianzhi_main()
freebuf_main()
2024-12-04 17:21:26 +08:00
def send_job(time_1):
Src_4hou(time_1)
Src_anquanke(time_1)
Src_doonsec(time_1)
Src_xianzhi(time_1)
2024-12-05 00:03:51 +08:00
Src_freebuf(time_1)
Src_qianxin(time_1)
2024-12-03 00:03:14 +08:00
def signal_handler(sig, frame):
print("接收到退出信号,程序即将退出...")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # kill命令
2024-12-04 17:21:26 +08:00
2024-12-03 00:03:14 +08:00
def main_loop():
2024-12-05 00:03:51 +08:00
n = 1
2024-12-03 00:03:14 +08:00
while True:
try:
2024-12-04 17:21:26 +08:00
# 执行任务
2024-12-05 00:03:51 +08:00
print(f"{n}次执行,当前时间为:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
2024-12-04 17:21:26 +08:00
crab_job()
send_job(e_hour)
print("执行完毕,等待下一次执行...")
2024-12-05 00:03:51 +08:00
n += 1
time.sleep(e_hour * 60 * 60 - 3 * 60)
2024-12-04 17:21:26 +08:00
2024-12-03 00:03:14 +08:00
except Exception as e:
2024-12-04 17:21:26 +08:00
print(f"发生错误: {e}, 程序已暂停")
# SendToFeishu(f"发生错误: {e}, 程序已退出", "报错信息")
2024-12-03 00:03:14 +08:00
exit()
2024-12-04 09:14:50 +08:00
# 探测rss源状态
def check_rss_status(url):
try:
response = requests.get(url, timeout=10)
if response.status_code == 200 and len(response.content) > 0:
return True
else:
return f"状态码: {response.status_code}, 内容长度: {len(response.content)}"
except requests.RequestException as e:
return f"请求异常: {str(e)}"
def test_rss_source():
rss_info = ""
2024-12-05 00:03:51 +08:00
url_1 = check_rss_status("https://forum.butian.net/Rss")
if url_1 == True:
rss_info += "奇安信 源正常\n"
else:
rss_info += f"奇安信 源异常: {url_1}\n"
2024-12-04 09:14:50 +08:00
url_2 = check_rss_status("https://wechat.doonsec.com/bayes_rss.xml")
if url_2 == True:
rss_info += "洞见 源正常\n"
else:
rss_info += f"洞见 源异常: {url_2}\n"
2024-12-05 00:03:51 +08:00
url_3 = check_rss_status("https://www.huawei.com/cn/rss-feeds/psirt/rss")
if url_3 == True:
rss_info += "华为 源正常\n"
else:
rss_info += f"华为 源异常: {url_3}\n"
2024-12-04 09:14:50 +08:00
2024-12-04 17:21:26 +08:00
# url_4 = check_rss_status("https://www.sec_wiki.com/news/rss")
# if url_4 == True:
# rss_info += "安全维基 源正常\n"
# else:
# rss_info += f"安全维基 源异常: {url_4}\n"
2024-12-04 09:14:50 +08:00
url_5 = check_rss_status("https://api.anquanke.com/data/v1/rss")
if url_5 == True:
rss_info += "安全客 源正常\n"
else:
rss_info += f"安全客 源异常: {url_5}\n"
url_6 = check_rss_status("https://www.4hou.com/feed")
if url_6 == True:
rss_info += "嘶吼 源正常\n"
else:
rss_info += f"嘶吼 源异常: {url_6}\n"
2024-12-05 00:03:51 +08:00
url_7 = check_rss_status("https://paper.seebug.org/rss/")
if url_7 == True:
rss_info += "Seebug社区 源正常\n"
else:
rss_info += f"Seebug社区 源异常: {url_7}\n"
2024-12-04 09:14:50 +08:00
2024-12-05 00:03:51 +08:00
url_8 = check_rss_status("https://www.freebuf.com/feed")
if url_8 == True:
rss_info += "FreeBuf社区 源正常\n"
else:
rss_info += f"FreeBuf社区 源异常: {url_8}\n"
2024-12-04 09:14:50 +08:00
url_9 = check_rss_status("https://xz.aliyun.com/feed")
if url_9 == True:
rss_info += "先知社区 源正常\n"
else:
rss_info += f"先知社区 源异常: {url_9}\n"
return rss_info
2024-12-03 00:03:14 +08:00
if __name__ == "__main__":
print("程序正在运行当中。")
2024-12-04 09:14:50 +08:00
rss_info = test_rss_source()
start_info = ""
start_info += "程序已启动,当前时间为:" + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "\n"
start_info += "程序作者MasonLiu \t 开源地址:[GM-gitea](https://git.masonliu.com/MasonLiu/PyBot)" + "\n"
2024-12-04 17:21:26 +08:00
start_info += "时间配置:每隔" + str(e_hour) + "小时执行一次推送\n"
2024-12-05 00:03:51 +08:00
start_info += "可启用源:\n嘶吼\n洞见微信安全资讯\n安全客\n先知社区\n"
2024-12-04 09:14:50 +08:00
SendToFeishu(start_info, "程序信息")
# print(start_info)
SendToFeishu(rss_info, "RSS源状态")
# print(rss_info)
2024-12-04 17:21:26 +08:00
# 首次运行先暂停两分钟
time.sleep(2 * 60)
# 主程序
2024-12-03 00:03:14 +08:00
main_loop()