feat(weibo): 增加微博微豆基础自动化
This commit is contained in:
@@ -0,0 +1,284 @@
|
||||
"""微博微豆账号导入、刷新与绑定 Scheme 服务。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.weibo import (
|
||||
WeiboAuthError,
|
||||
WeiboClient,
|
||||
WeiboError,
|
||||
WeiboExchangeResult,
|
||||
WeiboHomeSnapshot,
|
||||
WeiboPaymentStatus,
|
||||
WeiboRechargeOrder,
|
||||
normalize_weibo_cookie,
|
||||
)
|
||||
|
||||
from ..models import WeiboAccount
|
||||
|
||||
|
||||
@dataclass(frozen=True, slots=True)
|
||||
class WeiboImportIssue:
|
||||
line: int
|
||||
message: str
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
def _error_status(exc: Exception) -> str:
|
||||
return "invalid" if isinstance(exc, WeiboAuthError) else "error"
|
||||
|
||||
|
||||
def _apply_snapshot(
|
||||
account: WeiboAccount,
|
||||
snapshot: WeiboHomeSnapshot,
|
||||
cookie: str,
|
||||
) -> None:
|
||||
account.uid = snapshot.uid
|
||||
account.nickname = snapshot.nickname
|
||||
account.cookie = cookie
|
||||
account.cookie_updated_at = _now()
|
||||
account.is_bound = snapshot.is_bound
|
||||
account.game_role_name = snapshot.role_name
|
||||
account.can_rebind = snapshot.can_rebind
|
||||
account.rebind_remain_days = snapshot.rebind_remain_days
|
||||
account.weidou_balance = snapshot.weidou_balance
|
||||
if snapshot.activity:
|
||||
account.activity = snapshot.activity
|
||||
account.ck_check_status = "valid"
|
||||
account.ck_check_message = ""
|
||||
account.ck_checked_at = _now()
|
||||
|
||||
|
||||
def import_weibo_cookies(
|
||||
db: Session,
|
||||
text: str,
|
||||
tag: str = "",
|
||||
activity: str = "",
|
||||
) -> tuple[list[WeiboAccount], list[WeiboImportIssue], int]:
|
||||
"""逐行验证并导入微博 CK。
|
||||
|
||||
返回 ``(成功账号, 错误列表, 跳过数)``。单行 CK 失效或网络失败只记录该行,
|
||||
不阻断其余账号导入。
|
||||
"""
|
||||
|
||||
accounts: list[WeiboAccount] = []
|
||||
errors: list[WeiboImportIssue] = []
|
||||
skipped = 0
|
||||
seen_uids: set[str] = set()
|
||||
clean_tag = (tag or "").strip()
|
||||
|
||||
for line_no, raw_line in enumerate((text or "").splitlines(), 1):
|
||||
raw = raw_line.strip()
|
||||
if not raw:
|
||||
continue
|
||||
try:
|
||||
cookie = normalize_weibo_cookie(raw)
|
||||
snapshot = WeiboClient(cookie).fetch_home(activity)
|
||||
except (ValueError, WeiboError) as exc:
|
||||
errors.append(WeiboImportIssue(line=line_no, message=str(exc)))
|
||||
continue
|
||||
|
||||
if snapshot.uid in seen_uids:
|
||||
skipped += 1
|
||||
continue
|
||||
seen_uids.add(snapshot.uid)
|
||||
|
||||
account = (
|
||||
db.query(WeiboAccount).filter(WeiboAccount.uid == snapshot.uid).first()
|
||||
)
|
||||
if account is None:
|
||||
account = WeiboAccount(uid=snapshot.uid, cookie=cookie)
|
||||
db.add(account)
|
||||
_apply_snapshot(account, snapshot, cookie)
|
||||
if clean_tag:
|
||||
account.tag = clean_tag
|
||||
accounts.append(account)
|
||||
|
||||
if accounts:
|
||||
db.commit()
|
||||
for account in accounts:
|
||||
db.refresh(account)
|
||||
return accounts, errors, skipped
|
||||
|
||||
|
||||
def refresh_weibo_account(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
activity: str = "",
|
||||
) -> WeiboHomeSnapshot:
|
||||
"""刷新一个微博账号;失败时保留旧资料并记录本次检查状态。"""
|
||||
|
||||
try:
|
||||
snapshot = WeiboClient(account.cookie or "").fetch_home(activity)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
account.ck_check_status = _error_status(exc)
|
||||
account.ck_check_message = str(exc)
|
||||
account.ck_checked_at = _now()
|
||||
db.commit()
|
||||
raise
|
||||
|
||||
_apply_snapshot(account, snapshot, account.cookie or "")
|
||||
db.commit()
|
||||
db.refresh(account)
|
||||
return snapshot
|
||||
|
||||
|
||||
def generate_weibo_bind_schemes(
|
||||
account: WeiboAccount,
|
||||
bind_url: str,
|
||||
) -> list[dict[str, str]]:
|
||||
"""分别生成微信、QQ Scheme;单渠道失败不阻断另一渠道。"""
|
||||
|
||||
client = WeiboClient()
|
||||
result: list[dict[str, str]] = []
|
||||
for channel, label in (("wx", "微信"), ("qq", "QQ")):
|
||||
try:
|
||||
scheme = client.generate_bind_scheme(bind_url, channel)
|
||||
result.append(
|
||||
{"channel": channel, "label": label, "scheme": scheme, "error": ""}
|
||||
)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
result.append(
|
||||
{
|
||||
"channel": channel,
|
||||
"label": label,
|
||||
"scheme": "",
|
||||
"error": str(exc),
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def _effective_activity(account: WeiboAccount, fallback: str) -> str:
|
||||
return account.activity or fallback
|
||||
|
||||
|
||||
def _mark_manual_failure(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
exc: Exception,
|
||||
) -> None:
|
||||
account.ck_check_status = _error_status(exc)
|
||||
account.ck_check_message = str(exc)
|
||||
account.ck_checked_at = _now()
|
||||
db.commit()
|
||||
|
||||
|
||||
def query_weibo_mall(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
activity: str = "",
|
||||
) -> dict:
|
||||
try:
|
||||
data = WeiboClient(account.cookie or "").list_mall(
|
||||
_effective_activity(account, activity)
|
||||
)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
_mark_manual_failure(db, account, exc)
|
||||
raise
|
||||
if isinstance(data.get("weidou_balance"), int):
|
||||
account.weidou_balance = data["weidou_balance"]
|
||||
db.commit()
|
||||
return data
|
||||
|
||||
|
||||
def query_weibo_exchange_records(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
page: int,
|
||||
page_size: int,
|
||||
activity: str = "",
|
||||
) -> dict:
|
||||
try:
|
||||
return WeiboClient(account.cookie or "").list_exchange_records(
|
||||
_effective_activity(account, activity),
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
_mark_manual_failure(db, account, exc)
|
||||
raise
|
||||
|
||||
|
||||
def exchange_weibo_goods(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
goods_id: int,
|
||||
expected_price: int,
|
||||
activity: str = "",
|
||||
) -> WeiboExchangeResult:
|
||||
"""预检并执行一次微博微豆兑换,然后刷新账号余额。"""
|
||||
|
||||
try:
|
||||
result = WeiboClient(account.cookie or "").exchange_goods(
|
||||
goods_id,
|
||||
expected_price=expected_price,
|
||||
activity=_effective_activity(account, activity),
|
||||
)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
_mark_manual_failure(db, account, exc)
|
||||
raise
|
||||
if result.balance_after is not None:
|
||||
account.weidou_balance = result.balance_after
|
||||
db.commit()
|
||||
return result
|
||||
|
||||
|
||||
def query_weibo_point_history(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
page: int,
|
||||
page_size: int,
|
||||
activity: str = "",
|
||||
) -> dict:
|
||||
try:
|
||||
return WeiboClient(account.cookie or "").list_point_history(
|
||||
_effective_activity(account, activity),
|
||||
page=page,
|
||||
page_size=page_size,
|
||||
)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
_mark_manual_failure(db, account, exc)
|
||||
raise
|
||||
|
||||
|
||||
def create_weibo_recharge_order(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
amount_yuan: int,
|
||||
activity: str = "",
|
||||
) -> WeiboRechargeOrder:
|
||||
try:
|
||||
return WeiboClient(account.cookie or "").create_recharge_order(
|
||||
amount_yuan=amount_yuan,
|
||||
activity=_effective_activity(account, activity),
|
||||
)
|
||||
except (WeiboError, ValueError) as exc:
|
||||
_mark_manual_failure(db, account, exc)
|
||||
raise
|
||||
|
||||
|
||||
def query_weibo_payment_status(
|
||||
db: Session,
|
||||
account: WeiboAccount,
|
||||
*,
|
||||
pay_id: str,
|
||||
) -> WeiboPaymentStatus:
|
||||
try:
|
||||
return WeiboClient(account.cookie or "").get_payment_status(pay_id)
|
||||
except Exception as exc:
|
||||
_mark_manual_failure(db, account, exc)
|
||||
raise
|
||||
Reference in New Issue
Block a user