Files
abot/main.py
2025-05-20 10:34:17 +08:00

106 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import threading
from async_job import async_job
from configuration import Config
from robot import Robot
from loguru import logger
# INFO 日志(包含 INFO、DEBUG但不包含 WARNING、ERROR
logger.add(
f"wx_info.log",
level="INFO",
filter=lambda record: record["level"].name in ["INFO", "DEBUG"],
rotation="10 MB",
retention="7 days",
encoding="utf-8"
)
# ERROR 日志(仅 ERROR 及以上)
logger.add(
f"wx_error.log",
level="ERROR",
rotation="10 MB",
retention="7 days",
encoding="utf-8"
)
# ERROR 日志(仅 ERROR 及以上)
logger.add(
f"wx_debug.log",
level="DEBUG",
rotation="10 MB",
retention="7 days",
encoding="utf-8"
)
def main():
config = Config()
# 创建机器人实例
robot = Robot(config)
robot.LOG.info(f"WeChatRobot 正在启动...")
# 初始化并启动wechat_ipad客户端
if robot.init_wechat_ipad():
robot.LOG.info("wechat_ipad客户端启动成功")
else:
robot.LOG.error("wechat_ipad客户端启动失败")
# 注册定时任务
jobs(robot)
# 启动Dashboard服务器
dashboard_server = None
try:
# 创建Dashboard服务器实例共享robot对象
from admin.dashboard.server import DashboardServer
dashboard_server = DashboardServer(robot_instance=robot)
# 在单独的线程中启动Dashboard服务器
dashboard_thread = threading.Thread(target=dashboard_server.run, daemon=True)
dashboard_thread.start()
robot.LOG.info(f"Dashboard服务器已在 http://{dashboard_server.host}:{dashboard_server.port} 启动")
except Exception as e:
robot.LOG.error(f"Dashboard服务器启动失败: {e}")
asyncio.run(async_job.run_all())
# 让机器人一直跑
robot.keep_running_and_block_process()
def jobs(robot: Robot):
# # 每天 8:30 发送新闻
@async_job.at_times(["08:30"])
async def news_baidu_report_auto_job():
await robot.news_baidu_report_auto()
@async_job.at_times(["10:36"])
async def test_job():
await robot.job_test()
# #
# # # epic
# robot.onEveryTime("10:30", robot.send_epic_free_games)
# #
# # # message report 1:数据自动从redis 转到sqllite
# robot.onEveryTime("02:30", robot.message_count_to_db)
# # # 从db中提取并发送给相关群
# robot.onEveryTime("09:30", robot.generate_and_send_ranking)
# #
# # # sehuatang
# robot.onEveryTime("15:30", robot.generate_sehuatang_pdf)
# #
# # # 秀人网每天自动下载帖子
# robot.onEveryTime("01:30", robot.xiu_ren_download_task)
# #
# # # 秀人网每天自动发pdf
# # robot.onEveryTime("17:30", robot.xiu_ren_pdf_send)
#
# # 每天进行二次登录检查
# robot.onEveryHours(3, robot.login_twice_auto_auth)
if __name__ == "__main__":
main()