crawler_task_management/main.py

169 lines
6.5 KiB
Python
Raw Normal View History

2025-11-26 17:40:11 +08:00
import os
import yaml
import uuid
import asyncio
import uvicorn
from pathlib import Path
from typing import Dict, Any, Optional, List
from fastapi import FastAPI, HTTPException, Depends
2025-11-27 16:18:12 +08:00
from public_function.public_func import read_config, create_logger
2025-11-27 16:00:41 +08:00
from model.model import GoodsInfo, DataReceive, AccountCreate, AccountUpdate, DeviceResetData
2025-11-26 17:40:11 +08:00
app = FastAPI()
2025-11-27 16:18:12 +08:00
logger = create_logger(file_name="crawler_main")
2025-11-26 17:40:11 +08:00
def get_config():
"""获取配置文件"""
config_path = os.path.join(Path(__file__).resolve().parent, 'public_function/config.yaml')
try:
# 这里假设read_config函数存在
from public_function.public_func import read_config
return read_config(config_path)
except ImportError:
2025-11-27 16:00:41 +08:00
print(f"未找到read_config函数使用默认配置")
2025-11-26 17:40:11 +08:00
return {'host': 'localhost', 'port': 3306, 'user': 'root', 'password': 'password', 'db': 'test_db', 'max_overflow': 10}
def get_account_manager():
"""获取账号管理器实例"""
config = get_config()
try:
from account_management.deal_account import DealAccount
return DealAccount(config)
except ImportError:
2025-11-27 16:00:41 +08:00
print(f"未找到DealAccount类返回模拟实例")
2025-11-26 17:40:11 +08:00
return None
def get_task_manager():
"""获任务管理器实例"""
config = get_config()
try:
from task_management.all_task_management import AllTask
return AllTask(config)
except ImportError:
2025-11-27 16:00:41 +08:00
print(f"未找到AllTask类返回模拟实例")
return None
def get_reset_manager():
"""设备重置管理器"""
config = get_config()
try:
from task_management.all_task_management import AllTask
return AllTask(config)
except ImportError:
print("未找到 类,返回模拟实例")
2025-11-26 17:40:11 +08:00
return None
# 账号处理相关
@app.get("/obtain_account", summary="获取可用账号")
async def obtain_account(app_name: str, country: str, account_manager: Any = Depends(get_account_manager)):
"""
获取指定应用的可用账号
- **app_name**: 应用名称
"""
if not app_name or not app_name.strip():
raise HTTPException(status_code=400, detail="应用名称不能为空")
if not country or not country.strip():
raise HTTPException(status_code=400, detail="国家不能为空会")
try:
result = await account_manager.obtain_account_info(app_name, country)
if result:
return {"code": 200, "message": "获取账号成功", "data": result[0]}
else:
raise HTTPException(status_code=404, detail="没有可用的账号")
except Exception as e:
print(f"获取账号失败: {e}")
raise HTTPException(status_code=404, detail="{}".format(e))
@app.post("/add_account", summary="新增账号")
async def add_account(account_data: AccountCreate, account_manager: Any = Depends(get_account_manager)):
"""
新增爬虫账号
- **account_id**: 账号ID
- **password**: 密码
- **app_name**: 应用名称
"""
try:
2025-11-27 11:39:28 +08:00
result = await account_manager.add_account([account_data.model_dump()])
2025-11-27 10:44:09 +08:00
if result:
return {"code": 200, "message": "新增账号成功", "data": result}
raise HTTPException(status_code=404, detail="新增账号失败")
2025-11-26 17:40:11 +08:00
except Exception as e:
print(f"新增账号失败: {e}")
raise HTTPException(status_code=500, detail="新增账号失败,失败原因:{}".format(e))
2025-11-27 10:44:09 +08:00
@app.post("/update_account", summary="删除账号")
async def update_account(account_data: AccountUpdate, account_manager: Any = Depends(get_account_manager)):
2025-11-26 18:06:19 +08:00
"""
删除爬虫账号
- **account_id**: 账号ID
- **password**: 密码
- **app_name**: 应用名称
"""
try:
2025-11-27 11:39:28 +08:00
data = account_data.model_dump()
2025-11-27 10:44:09 +08:00
set_param = {"status": data["status"]}
params = (data["account_id"], data["app_name"])
where_conditions = "account_id = %s and app_name = %s "
result = await account_manager.update_account_info(set_param=set_param, where_conditions=where_conditions, params=params)
if result:
return {"code": 200, "message": "删除账号状态修改成功", "data": result}
raise HTTPException(status_code=404, detail="删除账号状态修改失败")
2025-11-26 18:06:19 +08:00
except Exception as e:
print(f"新增账号失败: {e}")
raise HTTPException(status_code=500, detail="删除账号失败,请重试,失败原因:{}".format(e))
2025-11-26 18:14:35 +08:00
@app.post("/receive_data", summary="接收抓取得数据")
2025-11-26 18:56:26 +08:00
async def receive_data(task_data: DataReceive, task_manager: Any = Depends(get_task_manager)):
2025-11-26 17:40:11 +08:00
"""数据接收接口"""
2025-11-26 18:56:26 +08:00
try:
2025-11-27 11:39:28 +08:00
params = task_data.model_dump()
2025-11-26 18:56:26 +08:00
print(params)
result = await task_manager.deal_receive_data(params)
if result:
return {"code": 200, "message": "数据保存成功", "data": result}
raise HTTPException(status_code=404, detail="抓取商品数据失败,请重新尝试")
except Exception as e:
2025-11-27 16:00:41 +08:00
print(f"{get_local_time()},商品数据处理失败,失败原因: {e}")
2025-11-26 18:56:26 +08:00
raise HTTPException(status_code=500, detail="获取数据失败;失败原因{}".format(e))
2025-11-26 17:40:11 +08:00
2025-11-27 14:57:57 +08:00
@app.post("/get_goods_info", summary="客户端获取商品数据")
async def get_goods_info(task_data: GoodsInfo, task_manager: Any = Depends(get_task_manager)):
"""客户端获取商品数据接口"""
2025-11-26 17:40:11 +08:00
try:
2025-11-27 11:39:28 +08:00
params = task_data.model_dump()
2025-11-26 17:40:11 +08:00
result = await task_manager.task_distribution(params)
if result:
2025-11-27 14:57:57 +08:00
return {"code": 200, "message": "数据获取成功", "data": result}
2025-11-26 17:40:11 +08:00
raise HTTPException(status_code=404, detail="抓取商品数据失败,请重新尝试")
except Exception as e:
print(f"<UNK>: {e}")
raise HTTPException(status_code=500, detail="获取数据失败;失败原因{}".format(e))
2025-11-27 16:00:41 +08:00
@app.get("/device_reset", summary="设备重置")
async def device_reset(task_data: DeviceResetData, reset_manager: Any = Depends(get_reset_manager)):
"""设备重置接口"""
try:
params = task_data.model_dump()
result = await reset_manager.task_distribution(params)
if result:
return {"code": 200, "message": "<UNK>", "data": result}
raise HTTPException(status_code=404, detail="抓取商品数据失败,请重新尝试")
except Exception as e:
print(f"设备重置失败,失败原因: {e}")
raise HTTPException(status_code=500, detail="获取数据失败;失败原因{}".format(e))
2025-11-27 10:20:20 +08:00
2025-11-26 17:40:11 +08:00
if __name__ == '__main__':
uvicorn.run(app)