PyBot/Dev_test.py
2025-01-24 17:36:53 +08:00

150 lines
5.3 KiB
Python

# -*- coding: utf-8 -*-
"""
@Author: MasonLiu
@Description: 本程序可以爬取各安全资讯源,并发送到飞书群组。
"""
import schedule
import os
import signal
import sys
import time
import yaml
import requests
from datetime import datetime, timedelta
from SendCore.FeishuSendBot import SendToFeishu, gen_sign
from SendCore.QiweiSendBot import SendToWX
from spider.common import run, seebug_main, M_4hou_main, anquanke_main, sec_wiki_main, huawei_main, doonsec_main, qianxin_main
from spider.freebuf import freebuf_main
from spider.xianzhi import xianzhi_main
from spider.sougou_wx import sougou_wx_main
from spider.github import github_main, load_github_config
from spider.baidu import baidu_main
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
from GotoSend.sougou_wx import Src_sougou_wx
from GotoSend.github import Src_github
from GotoSend.baidu import Src_baidu
from config.check_config import get_core_config, get_debug_config, get_keywords_config
from loguru import logger
# 清除所有已有的日志记录器配置
logger.remove()
logger.add("./resources/log/core.log",
format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {name}:{function}:{line} - {message}",
rotation="100 MB",
compression="zip",
encoding="utf-8")
# shell终端打印日志
debug = get_debug_config()
if debug == "True":
logger.add(lambda msg: print(msg),
format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {name}:{function}:{line} - {message}")
def signal_handler(sig, frame):
logger.info("接收到退出信号,程序即将退出...")
sys.exit(0)
# 全局变量
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # kill命令
webhook_url_once, timestamp_once, sign_once = gen_sign()
e_hour, time_choice, choice, fs_activate, wx_activate, ding_activate, lx_activate, url_web = get_core_config()
def check_avaliable(info_long, info_short, title):
if info_short: # 发送精简文章相关内容
# 企业微信相关
if wx_activate == "True":
# logger.info(f"{title} 递送中(企业微信):")
print("正在发送精简文章内容...")
for info in info_short:
result = SendToWX(info, title)
print(result)
logger.info(result)
time.sleep(15)
else:
print("精简文章内容为空,跳过执行。")
pass
if info_long: # 发送完整文章相关内容
if fs_activate == "True":
# logger.info(f"{title} 递送中(飞书):")
webhook_url, timestamp, sign = gen_sign()
result = SendToFeishu(info_long, title, webhook_url, timestamp, sign)
logger.info(result)
time.sleep(15)
else:
pass
if not info_long and not info_short:
logger.info(f"{title}数据为空,跳过执行。")
def send_job_RSS(time_1):
Doonsec_switch, Doonsec = get_keywords_config('Doonsec')
results = Src_doonsec(Doonsec_switch, Doonsec)
if results != False:
result_doonsec_long, result_doonsec_short = results
check_avaliable(result_doonsec_long, result_doonsec_short, "洞见微信安全资讯")
def main_job(e_hour):
logger.info(f"发送程序启动,当前时间为:{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
logger.info("正在启动各爬虫并获取资源中...")
if 0 in choice:
send_job_RSS(e_hour)
logger.info("单次运行结束,等待下一次运行...")
def main_loop(time_choice):
if time_choice == 1:
while True:
try:
# 执行任务
main_job(e_hour)
time.sleep(e_hour * 60 * 60 - 3 * 60)
except Exception as e:
logger.error(f"发生错误: {e}, 程序已暂停")
# result = SendToFeishu(f"发生错误: {e}, 程序已退出", "报错信息")
exit()
elif time_choice == 0:
# 设置每天的特定时间点执行job函数
schedule.every().day.at("09:00").do(main_job, 12)
schedule.every().day.at("12:00").do(main_job, 3)
schedule.every().day.at("15:00").do(main_job, 3)
schedule.every().day.at("18:00").do(main_job, 3)
schedule.every().day.at("21:00").do(main_job, 3)
while True:
schedule.run_pending()
time.sleep(60) # 每分钟检查一次是否有任务需要执行
if __name__ == "__main__":
logger.info("程序正在运行当中。")
results = Src_anquanke(4)
if results != False:
result_long, short_results = results
# 打印长文本结果
print("长文本结果:")
print(result_long)
print("\n" + "-" * 40 + "\n")
# 打印分块的短文本结果
print("分块的短文本结果:")
for short_result in short_results:
print(short_result)
print("\n" + "-" * 40 + "\n")
else:
# 如果为空,则跳过执行
print("-" * 40)
print("安全客数据为空,跳过执行。")
# main_loop(time_choice)