This commit is contained in:
HuangHai
2026-01-15 16:35:24 +08:00
parent ba46eb15f6
commit 46770fc820
14 changed files with 366 additions and 272 deletions

View File

@@ -1,40 +1,29 @@
# 采集配置
# 滑动距离比例 (0.1 ~ 0.9),数值越大滑动幅度越大
SCROLL_DISTANCE_RATIO = 0.5
# 最大采集场站数量,达到此次数后停止采集
MAX_STATIONS_COUNT = 30
FIRST_RUN_ONLY_ONE_STATION = False
# 场站去重过期时间(秒),在此时间内重复出现的场站不会再次点击进入详情页
REDIS_STATION_EXPIRE = 120
# 数据库数据保留时长(天),超过此时长的历史数据(is_current=0)将被删除
DATA_RETENTION_DAYS = 365
# 测试配置
# [Testing] 是否在启动时清除 Redis 中的已爬取记录 (用于快速重复测试)
TEST_CLEAR_REDIS = False
# 调试绘图配置
# [Debug] 是否在图片上绘制识别出的绿色矩形框 (计算机图形学可视化)
DRAW_DEBUG_BOXES = True
DEBUG_BOX_COLOR = (0, 255, 0) # BGR 格式的绿色
DEBUG_BOX_THICKNESS = 3 # 线条粗细
# 等待时间配置 (秒)
# 点击进入详情页后等待加载的时间
WAIT_DETAIL_PAGE_LOAD = 2.0
# 从详情页返回列表页后等待页面刷新的时间
WAIT_BACK_TO_LIST = 0.8
# 执行滑动操作后等待页面内容加载和稳定的时间
WAIT_AFTER_SCROLL = 2.0
# 坐标计算与安全防护
# 屏幕顶部安全排除比例 (0.0~1.0),此比例区域内不进行点击(避开状态栏、顶部菜单、横幅广告等)
SAFE_EXCLUDE_RATIO = 0.15
# 屏幕底部安全排除比例 (0.0~1.0),此比例区域内不进行点击(避开底部导航栏、功能按钮等)
BOTTOM_SAFE_EXCLUDE_RATIO = 0.10
# 默认回退屏幕宽度,当无法自动获取设备信息时使用
FALLBACK_WIDTH = 1080
# 默认回退屏幕高度,当无法自动获取设备信息时使用
FALLBACK_HEIGHT = 2400
FIRST_RUN_ONLY_ONE_STATION = False
# 采集配置
SCROLL_DISTANCE_RATIO = 0.5
MAX_STATIONS_COUNT = 30
FIRST_RUN_ONLY_ONE_STATION = False
REDIS_STATION_EXPIRE = 120
DATA_RETENTION_DAYS = 365
# 测试配置
TEST_CLEAR_REDIS = False
# 调试绘图配置
DRAW_DEBUG_BOXES = True
DEBUG_BOX_COLOR = (0, 255, 0)
DEBUG_BOX_THICKNESS = 3
# 等待时间配置 (秒)
WAIT_DETAIL_PAGE_LOAD = 2.0
WAIT_BACK_TO_LIST = 0.8
WAIT_AFTER_SCROLL = 2.0
# 坐标计算与安全防护
SAFE_EXCLUDE_RATIO = 0.15
BOTTOM_SAFE_EXCLUDE_RATIO = 0.10
FALLBACK_WIDTH = 1080
FALLBACK_HEIGHT = 2400
# 分时价格配置
PRICE_FLATTEN_TO_24H = False

View File

@@ -6,19 +6,18 @@ import sys
import uuid
from datetime import datetime
# Ensure sys path includes root for imports if not already
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if project_root not in sys.path:
sys.path.append(project_root)
from DbKit.Db import Db
from Config.Config import DB_URL
from Config.Config import DB_URL, PRICE_FLATTEN_TO_24H_GLOBAL
from Model.StationProfile import StationProfile
from Model.StationStatus import StationStatus
from Model.StationPriceSchedule import StationPriceSchedule
from Apps.YeLiTe.Config.Setting import PRICE_FLATTEN_TO_24H
# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = logging.getLogger(__name__)
class YiLaiTeService:
@@ -90,73 +89,63 @@ class YiLaiTeService:
except Exception as e:
logger.error(f"更新任务结束日志失败: {e}")
async def process_price_detail_data(self, station_name, prices, address=None, distance=None) -> bool:
"""
保存场站数据:名称、地址、24小时价格计划、距离
"""
if not station_name or not prices:
async def process_price_detail_data(self, station_name, hourly_schedule, address=None, distance=None) -> bool:
if not station_name or not hourly_schedule:
return False
station_hash = self.get_hash(station_name)
now = datetime.now()
# 1. 预处理价格:生成 24 小时的价格映射
hourly_schedule = [None] * 24
for p in prices:
try:
start_str = p.get('start') or '00:00'
end_str = p.get('end') or '00:00'
# 解析小时
start_hour = int(start_str.split(':')[0])
end_hour = int(end_str.split(':')[0])
# 处理 00:00 作为结束时间
if end_hour == 0 and start_hour != 0:
end_hour = 24
elif end_hour == 0 and start_hour == 0 and len(prices) == 1:
end_hour = 24
# 如果跨天
if end_hour < start_hour:
end_hour += 24
price_val = self._to_float(p.get('price'))
curr = start_hour
while curr < end_hour:
hour_idx = curr % 24
hourly_schedule[hour_idx] = {
"start": f"{hour_idx:02d}:00",
"end": f"{(hour_idx + 1):02d}:00",
"price": price_val
}
curr += 1
except Exception as e:
logger.error(f"解析价格时段失败: {p}, error: {e}")
schedule_to_save = hourly_schedule
# 2. 兜底填充 (补全 24 小时)
last_valid = None
for item in hourly_schedule:
if item:
last_valid = item
break
if last_valid:
for i in range(24):
if hourly_schedule[i] is None:
new_item = last_valid.copy()
new_item["start"] = f"{i:02d}:00"
new_item["end"] = f"{(i+1):02d}:00"
hourly_schedule[i] = new_item
else:
last_valid = hourly_schedule[i]
else:
logger.warning(f"场站 {station_name} 未能生成有效的 24 小时价格表")
use_flatten = (PRICE_FLATTEN_TO_24H_GLOBAL or PRICE_FLATTEN_TO_24H) and isinstance(hourly_schedule, list)
if use_flatten:
tmp = [None] * 24
for p in hourly_schedule:
try:
start_str = p.get("start") or "00:00"
end_str = p.get("end") or "00:00"
start_hour = int(start_str.split(":")[0])
end_hour = int(end_str.split(":")[0])
if end_hour == 0 and start_hour != 0:
end_hour = 24
if end_hour < start_hour:
end_hour += 24
price_val = self._to_float(p.get("price"))
curr = start_hour
while curr < end_hour:
hour_idx = curr % 24
tmp[hour_idx] = {
"start": f"{hour_idx:02d}:00",
"end": f"{(hour_idx + 1):02d}:00",
"price": price_val,
}
curr += 1
except Exception as e:
logger.error(f"解析价格时段失败: {p}, error: {e}")
last_valid = None
for item in tmp:
if item:
last_valid = item
break
if last_valid:
for i in range(24):
if tmp[i] is None:
new_item = last_valid.copy()
new_item["start"] = f"{i:02d}:00"
new_item["end"] = f"{(i+1):02d}:00"
tmp[i] = new_item
else:
last_valid = tmp[i]
if any(tmp):
schedule_to_save = tmp
async with await self.db.get_session() as session:
# 1. 保存 Profile (如果不存在)
profile_id = self.generate_id()
await self.station_profile_model.save(
session=session,
@@ -165,23 +154,28 @@ class YiLaiTeService:
operator=self.operator,
station_name=station_name,
address=address,
valid_start_time=now
valid_start_time=now,
)
# 2. 保存价格
schedule_id = self.generate_id()
await self.station_price_schedule_model.save(
session=session,
id=schedule_id,
station_hash=station_hash,
schedule_json=hourly_schedule,
valid_start_time=now
schedule_json=schedule_to_save,
valid_start_time=now,
)
# 3. 保存状态 (记录距离和当前价格)
current_hour = now.hour
current_price_info = hourly_schedule[current_hour] or {}
current_price_info = {}
if isinstance(schedule_to_save, list) and 0 <= current_hour < len(schedule_to_save):
try:
current_price_info = schedule_to_save[current_hour] or {}
except:
current_price_info = {}
elif isinstance(schedule_to_save, dict):
current_price_info = schedule_to_save
status_id = self.generate_id()
await self.station_status_model.save(
session=session,
@@ -190,14 +184,15 @@ class YiLaiTeService:
total_piles=None,
free_piles=None,
piles_detail_json=None,
current_price=current_price_info.get('price'),
current_price=current_price_info.get("price"),
distance=distance,
valid_start_time=now
valid_start_time=now,
)
await session.commit()
logger.info(f"成功保存驿来特场站数据: {station_name} | 距离: {distance} | 价格条数: {len([x for x in hourly_schedule if x])}")
cnt = len(schedule_to_save) if isinstance(schedule_to_save, list) else 1
logger.info(f"成功保存驿来特场站数据: {station_name} | 距离: {distance} | 价格条数: {cnt}")
return True
async def process_station_list_vl(self, image_path, json_metadata, device_info=None, max_count=None) -> list: