Files
live-hub-py/web/backend/migrations/versions/20260724_0008_douyu_activity_tasks.py
T
yml2213 62099e8d68 新增斗鱼活动任务模块:绑定、宝典、鱼翅、积分、兑换等功能
- 新增 activity_client.py:封装斗鱼活动/兑换/充值/送礼接口
- 新增 cookie_utils.py:Cookie 解析与规范化工具
- 新增 douyu_service/douyu_runner:斗鱼任务服务层与批量执行器
- 新增 douyu 路由:任务类型查询、账号列表、配置管理、商品管理、批量任务、WebSocket 日志
- 新增 models/schemas:DouyuTask/DouyuConfig/DouyuGoodsSnapshot 模型,Account 扩展点数/鱼翅/绑定状态等字段
- 新增数据库迁移:斗鱼活动相关表与 accounts 字段补充
- 新增前端 DouyuTasksPage 任务操作台页面
- 兑换商品请求添加 sec-ch-ua 反检测头
- 兑换商品支持最多 8 次重试 + csrf_token 自动刷新
- 注册 douyu:task / douyu:config 权限点
- 侧边栏新增斗鱼分组与任务操作台菜单入口
2026-07-24 16:11:39 +08:00

151 lines
6.5 KiB
Python

"""新增斗鱼活动任务表
Revision ID: 20260724_0008
Revises: 20260712_0007
Create Date: 2026-07-24
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "20260724_0008"
down_revision: Union[str, None] = "20260712_0007"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def _has_table(bind, table_name: str) -> bool:
return sa.inspect(bind).has_table(table_name)
def _columns(bind, table_name: str) -> set[str]:
if not _has_table(bind, table_name):
return set()
return {column["name"] for column in sa.inspect(bind).get_columns(table_name)}
def _indexes(bind, table_name: str) -> set[str]:
if not _has_table(bind, table_name):
return set()
return {index["name"] for index in sa.inspect(bind).get_indexes(table_name)}
def _add_column_if_missing(bind, table_name: str, column: sa.Column) -> None:
if column.name not in _columns(bind, table_name):
op.add_column(table_name, column)
def _create_index_if_missing(bind, name: str, table_name: str, columns: list[str], unique: bool = False) -> None:
if name not in _indexes(bind, table_name):
op.create_index(name, table_name, columns, unique=unique)
def upgrade() -> None:
bind = op.get_bind()
_add_column_if_missing(bind, "accounts", sa.Column("uid", sa.String(length=32), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("nickname", sa.String(length=128), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("points", sa.Integer(), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("game_name", sa.String(length=128), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("game_channel", sa.String(length=128), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("gold_balance", sa.Integer(), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("exchange_balance", sa.Integer(), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("bind_status", sa.String(length=32), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("change_role_wait_time", sa.Integer(), nullable=True))
_add_column_if_missing(bind, "accounts", sa.Column("updated_at", sa.DateTime(), nullable=True))
_create_index_if_missing(bind, "ix_accounts_uid", "accounts", ["uid"])
if not _has_table(bind, "douyu_tasks"):
op.create_table(
"douyu_tasks",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("batch_id", sa.String(length=64), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("task_type", sa.String(length=64), nullable=False),
sa.Column("status", sa.String(length=32), nullable=True),
sa.Column("message", sa.String(length=512), nullable=True),
sa.Column("result", sa.JSON(), nullable=True),
sa.Column("created_by", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
_create_index_if_missing(bind, "ix_douyu_tasks_batch_id", "douyu_tasks", ["batch_id"])
_create_index_if_missing(bind, "ix_douyu_tasks_task_type", "douyu_tasks", ["task_type"])
if not _has_table(bind, "douyu_config"):
op.create_table(
"douyu_config",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("manual_id", sa.String(length=64), nullable=True),
sa.Column("rid", sa.String(length=64), nullable=True),
sa.Column("bind_act_alias", sa.String(length=64), nullable=True),
sa.Column("confirm_act_alias", sa.String(length=64), nullable=True),
sa.Column("legacy_act_alias", sa.String(length=64), nullable=True),
sa.Column("room_id", sa.String(length=64), nullable=True),
sa.Column("elite_amount", sa.Integer(), nullable=True),
sa.Column("gold_pay_type", sa.Integer(), nullable=True),
sa.Column("gift_id", sa.String(length=64), nullable=True),
sa.Column("skin_id", sa.String(length=64), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
if not _has_table(bind, "douyu_goods_snapshot"):
op.create_table(
"douyu_goods_snapshot",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("commodity_id", sa.String(length=64), nullable=False),
sa.Column("name", sa.String(length=256), nullable=True),
sa.Column("score", sa.Integer(), nullable=True),
sa.Column("status", sa.String(length=32), nullable=True),
sa.Column("raw", sa.JSON(), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
_create_index_if_missing(
bind,
"ix_douyu_goods_snapshot_commodity_id",
"douyu_goods_snapshot",
["commodity_id"],
)
def downgrade() -> None:
bind = op.get_bind()
if _has_table(bind, "douyu_goods_snapshot"):
indexes = _indexes(bind, "douyu_goods_snapshot")
if "ix_douyu_goods_snapshot_commodity_id" in indexes:
op.drop_index("ix_douyu_goods_snapshot_commodity_id", table_name="douyu_goods_snapshot")
op.drop_table("douyu_goods_snapshot")
if _has_table(bind, "douyu_config"):
op.drop_table("douyu_config")
if _has_table(bind, "douyu_tasks"):
indexes = _indexes(bind, "douyu_tasks")
if "ix_douyu_tasks_task_type" in indexes:
op.drop_index("ix_douyu_tasks_task_type", table_name="douyu_tasks")
if "ix_douyu_tasks_batch_id" in indexes:
op.drop_index("ix_douyu_tasks_batch_id", table_name="douyu_tasks")
op.drop_table("douyu_tasks")
account_columns = _columns(bind, "accounts")
for column in [
"updated_at",
"change_role_wait_time",
"bind_status",
"exchange_balance",
"gold_balance",
"game_channel",
"game_name",
"points",
"nickname",
"uid",
]:
if column in account_columns:
op.drop_column("accounts", column)