PyBot/Core.py

248 lines
8.3 KiB
Python
Raw Normal View History

2024-12-06 16:32:34 +08:00
# -*- coding: utf-8 -*-
"""
@Author: MasonLiu
@Description: 本程序可以爬取各安全资讯源并发送到飞书群组
"""
2024-12-10 00:00:44 +08:00
import os
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
2024-12-05 12:20:37 +08:00
from SendBot import SendToFeishu, gen_sign
from media.common import run, seebug_main, M_4hou_main, anquanke_main, sec_wiki_main, huawei_main, doonsec_main, qianxin_main
2024-12-03 00:03:14 +08:00
from media.freebuf import freebuf_main
from media.xianzhi import xianzhi_main
2024-12-09 23:12:32 +08:00
from GotoSend.M_4hou import Src_4hou
from GotoSend.anquanke import Src_anquanke
from GotoSend.doonsec import Src_doonsec
from GotoSend.xianzhi import Src_xianzhi
from GotoSend.freebuf import Src_freebuf
from GotoSend.qianxin import Src_qianxin
from GotoSend.seebug import Src_seebug
2024-12-09 23:52:49 +08:00
from loguru import logger
logger.add("./log/spider.log",
format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {name}:{function}:{line} - {message}",
rotation="10 MB",
compression="zip",
encoding="utf-8")
# shell终端打印日志
# logger.add(lambda msg: print(msg),
# format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {name}:{function}:{line} - {message}")
2024-12-03 00:03:14 +08:00
# 加载参数
2024-12-05 09:49:17 +08:00
with open('./config.yaml', 'r', encoding="utf-8") as file:
config = yaml.safe_load(file)
# sleep_time = int(f"{config['sleep_time']}")
e_hour = int(f"{config['e_hour']}")
2024-12-05 12:20:37 +08:00
webhook_url_once, timestamp_once, sign_once = gen_sign()
2024-12-10 00:00:44 +08:00
def rename_log_with_timestamp():
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
original_filename = "./log/spider.log"
new_filename = f"./log/spider_{timestamp}.log"
try:
os.rename(original_filename, new_filename)
logger.info(f"旧日志文件已成功重命名为: {new_filename}")
except FileNotFoundError:
logger.error(f"旧日志文件 {original_filename} 未找到。")
except Exception as e:
logger.error(f"重命名旧日志文件时发生错误: {e}")
2024-12-05 12:20:37 +08:00
def send_job(time_1):
# 爬取数据
2024-12-10 00:00:44 +08:00
logger.info("正在启动各爬虫并获取资源中...")
2024-12-08 00:18:31 +08:00
seebug_main()
anquanke_main()
huawei_main()
doonsec_main()
qianxin_main()
2024-12-03 00:03:14 +08:00
freebuf_main()
2024-12-05 12:20:37 +08:00
xianzhi_main()
2024-12-09 23:03:35 +08:00
M_4hou_main()
2024-12-03 00:03:14 +08:00
2024-12-05 12:20:37 +08:00
# 分析各个数据源的结果
reslt_4hou = Src_4hou(time_1)
reslt_anquanke = Src_anquanke(time_1)
reslt_doonsec = Src_doonsec(time_1)
reslt_xianzhi = Src_xianzhi(time_1)
reslt_freebuf = Src_freebuf(time_1)
reslt_qianxin = Src_qianxin(time_1)
2024-12-05 21:25:31 +08:00
reslt_seebug = Src_seebug(time_1)
2024-12-05 12:20:37 +08:00
webhook_url, timestamp, sign = gen_sign()
# 发送嘶吼资讯
if reslt_4hou:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("嘶吼资讯递送中:")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_4hou, "嘶吼资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 12:20:37 +08:00
print("-" * 40 + "\n")
time.sleep(60)
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("嘶吼数据为空,跳过执行。")
2024-12-05 12:20:37 +08:00
# 发送安全客资讯
if reslt_anquanke:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("安全客资讯递送中:")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_anquanke, "安全客资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 12:20:37 +08:00
print("-" * 40 + "\n")
time.sleep(60)
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("安全客数据为空,跳过执行。")
2024-12-05 12:20:37 +08:00
# 发送洞见微信安全资讯
if reslt_doonsec:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("洞见微信安全资讯递送中:")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_doonsec, "洞见微信安全资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 12:20:37 +08:00
print("-" * 40 + "\n")
time.sleep(60)
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("洞见微信安全数据为空,跳过执行。")
2024-12-05 12:20:37 +08:00
# 发送先知社区资讯
if reslt_xianzhi:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("先知社区资讯递送中:")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_xianzhi, "先知社区资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 12:20:37 +08:00
print("-" * 40 + "\n")
time.sleep(60)
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("先知社区数据为空,跳过执行。")
2024-12-05 12:20:37 +08:00
# 发送FreeBuf资讯
if reslt_freebuf:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("FreeBuf资讯递送中")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_freebuf, "FreeBuf资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 12:20:37 +08:00
print("-" * 40 + "\n")
time.sleep(60)
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("FreeBuf数据为空跳过执行。")
2024-12-05 12:20:37 +08:00
# 发送奇安信攻防社区资讯
if reslt_qianxin:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("奇安信攻防社区资讯递送中:")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_qianxin, "奇安信攻防社区资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 12:20:37 +08:00
print("-" * 40 + "\n")
time.sleep(60)
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("奇安信攻防社区数据为空,跳过执行。")
2024-12-05 21:25:31 +08:00
# 发送Seebug资讯
if reslt_seebug:
reslt_seebug = Src_seebug(1000)
webhook_url, timestamp, sign = gen_sign()
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("Seebug社区资讯递送中")
2024-12-09 23:03:35 +08:00
result = SendToFeishu(reslt_seebug, "Seebug社区资讯递送", webhook_url, timestamp, sign)
logger.info(result)
2024-12-05 21:25:31 +08:00
print("-" * 40 + "\n")
else:
print("-" * 40)
2024-12-08 00:18:31 +08:00
logger.info("Seebug社区数据为空跳过执行。")
2024-12-05 21:25:31 +08:00
2024-12-03 00:03:14 +08:00
def signal_handler(sig, frame):
2024-12-08 00:18:31 +08:00
logger.info("接收到退出信号,程序即将退出...")
2024-12-03 00:03:14 +08:00
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-08 00:18:31 +08:00
logger.info(f"{n}次执行,当前时间为:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
2024-12-04 17:21:26 +08:00
send_job(e_hour)
2024-12-08 00:18:31 +08:00
logger.info("执行完毕,等待下一次执行...")
2024-12-05 00:03:51 +08:00
n += 1
2024-12-05 12:20:37 +08:00
time.sleep(e_hour * 60 * 60 - 5 * 60)
2024-12-04 17:21:26 +08:00
2024-12-03 00:03:14 +08:00
except Exception as e:
2024-12-08 00:18:31 +08:00
logger.error(f"发生错误: {e}, 程序已暂停")
2024-12-09 23:03:35 +08:00
# result = SendToFeishu(f"发生错误: {e}, 程序已退出", "报错信息")
# logger.info(result)
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():
2024-12-05 12:20:37 +08:00
rss_sources = {
"奇安信": "https://forum.butian.net/Rss",
"洞见": "https://wechat.doonsec.com/bayes_rss.xml",
# "华为": "https://www.huawei.com/cn/rss-feeds/psirt/rss",
# "安全维基": "https://www.sec_wiki.com/news/rss",
"安全客": "https://api.anquanke.com/data/v1/rss",
"嘶吼": "https://www.4hou.com/feed",
2024-12-05 21:25:31 +08:00
"Seebug社区": "https://paper.seebug.org/rss/",
2024-12-05 12:20:37 +08:00
"FreeBuf社区": "https://www.freebuf.com/feed",
"先知社区": "https://xz.aliyun.com/feed"
}
2024-12-04 09:14:50 +08:00
rss_info = ""
2024-12-05 12:20:37 +08:00
for name, url in rss_sources.items():
status = check_rss_status(url)
if status:
rss_info += f"{name} 源正常\n"
else:
rss_info += f"{name} 源异常: {status}\n"
2024-12-04 09:14:50 +08:00
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-08 00:18:31 +08:00
result = SendToFeishu(start_info, "程序信息", webhook_url_once, timestamp_once, sign_once)
logger.info(result)
print("-" * 40)
2024-12-04 09:14:50 +08:00
# print(start_info)
2024-12-08 00:18:31 +08:00
result = SendToFeishu(rss_info, "RSS源状态", webhook_url_once, timestamp_once, sign_once)
logger.info(rss_info)
logger.info(result)
print("-" * 40)
2024-12-04 09:14:50 +08:00
# print(rss_info)
2024-12-04 17:21:26 +08:00
# 首次运行先暂停两分钟
2024-12-08 00:18:31 +08:00
# time.sleep(2 * 60)
2024-12-04 17:21:26 +08:00
# 主程序
2024-12-03 00:03:14 +08:00
main_loop()