89 lines
3.5 KiB
Python
89 lines
3.5 KiB
Python
import asyncio
|
||
import os
|
||
import sys
|
||
import logging
|
||
|
||
from Util import Win32Patch
|
||
|
||
# 确保项目根目录在 sys.path 中
|
||
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 Util.RedisKit import RedisKit
|
||
from sqlalchemy.sql import text
|
||
from Config.Config import DB_URL
|
||
|
||
# 配置日志
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||
logger = logging.getLogger("ClearYeLiTe")
|
||
Win32Patch.patch()
|
||
async def main():
|
||
"""
|
||
根据注释完成代码:
|
||
1. 删除数据库中所有 operator='驿来特' 的记录
|
||
2. 删除 Redis 中所有关于驿来特的缓存信息
|
||
"""
|
||
operator = '驿来特'
|
||
|
||
# 1. 数据库清理
|
||
logger.info(f"开始清理数据库中 operator='{operator}' 的数据...")
|
||
db = Db(db_url=DB_URL)
|
||
await db.init_db()
|
||
|
||
try:
|
||
async with db.AsyncSessionLocal() as session:
|
||
async with session.begin():
|
||
# 先删除从表记录(通过 station_hash 关联)
|
||
# 1. t_station_status_scd
|
||
sql_status = """
|
||
DELETE FROM t_station_status_scd
|
||
WHERE station_hash IN (
|
||
SELECT station_hash FROM t_station_profile_scd WHERE operator = :operator
|
||
)
|
||
"""
|
||
logger.info("正在清理 t_station_status_scd...")
|
||
result_status = await session.execute(text(sql_status), {"operator": operator})
|
||
logger.info(f"t_station_status_scd 已删除 {result_status.rowcount} 行记录。")
|
||
|
||
# 2. t_station_price_schedule_scd
|
||
sql_price = """
|
||
DELETE FROM t_station_price_schedule_scd
|
||
WHERE station_hash IN (
|
||
SELECT station_hash FROM t_station_profile_scd WHERE operator = :operator
|
||
)
|
||
"""
|
||
logger.info("正在清理 t_station_price_schedule_scd...")
|
||
result_price = await session.execute(text(sql_price), {"operator": operator})
|
||
logger.info(f"t_station_price_schedule_scd 已删除 {result_price.rowcount} 行记录。")
|
||
|
||
# 3. 最后删除主表 t_station_profile_scd
|
||
sql_profile = "DELETE FROM t_station_profile_scd WHERE operator = :operator"
|
||
logger.info("正在清理 t_station_profile_scd...")
|
||
result_profile = await session.execute(text(sql_profile), {"operator": operator})
|
||
logger.info(f"t_station_profile_scd 已删除 {result_profile.rowcount} 行记录。")
|
||
|
||
logger.info("数据库记录清理完成。")
|
||
except Exception as e:
|
||
logger.error(f"数据库清理失败: {e}")
|
||
|
||
# 2. Redis 清理
|
||
logger.info("开始清理 Redis 中的缓存数据...")
|
||
redis_kit = RedisKit()
|
||
# 根据 Crawler.py 中的逻辑,key 的模式为 crawled:ylt:*
|
||
pattern = "crawled:ylt:*"
|
||
try:
|
||
keys = await redis_kit.keys(pattern)
|
||
if keys:
|
||
logger.info(f"匹配到 {len(keys)} 个键,正在删除...")
|
||
await redis_kit.delete(*keys)
|
||
logger.info("Redis 缓存清理完成。")
|
||
else:
|
||
logger.info(f"未匹配到模式为 '{pattern}' 的键。")
|
||
except Exception as e:
|
||
logger.error(f"Redis 清理失败: {e}")
|
||
|
||
if __name__ == "__main__":
|
||
asyncio.run(main())
|