Files
RaptDramaDump/server/services/huangdou/coins.py
T

110 lines
3.1 KiB
Python

PAID_TYPES = {"coin", "coins", "point", "points", "pay", "money"}
VIP_TYPES = {"vip"}
FREE_TYPES = {"", "free", "0", "none"}
def episode_kind(item: dict) -> str:
return str(item.get("type") or "free").strip().lower()
def already_bought(item: dict) -> bool:
value = item.get("is_buy")
if value is None or value is True:
return True
return str(value).strip().lower() in {"1", "y", "yes", "true"}
def price_of(item: dict) -> int:
for key in ("price", "points", "point", "integral"):
number = _int(item.get(key))
if number:
return number
return 0
def paid_cost(item: dict) -> int:
if episode_kind(item) in FREE_TYPES or already_bought(item):
return 0
return price_of(item)
def episode_note(item: dict, *, account_vip: bool) -> tuple[bool, str, int]:
cost = paid_cost(item)
if cost > 0:
return False, f"{cost} 积分", cost
if episode_kind(item) in VIP_TYPES and not already_bought(item) and not account_vip:
return False, "需要 VIP", 0
return True, "", 0
def public_lock_reason(item: dict, message: str) -> str:
price = price_of(item) or _int(item.get("points"))
text = (message or "").strip()
if not already_bought(item) and price > 0:
return f"{price} 积分"
if any(word in text for word in ("不含", "权益", "解锁")):
return "这一集暂时不能播放"
return text or "这一集暂时不能播放"
def summarize(rows) -> dict:
coins = 0
coin_count = 0
free_count = 0
vip_count = 0
if isinstance(rows, list):
for item in rows:
if not isinstance(item, dict):
continue
cost = paid_cost(item)
if cost > 0:
coins += cost
coin_count += 1
continue
kind = episode_kind(item)
if kind in VIP_TYPES:
vip_count += 1
else:
free_count += 1
return {
"coins": coins,
"coin_count": coin_count,
"free_count": free_count,
"vip_count": vip_count,
}
def allow_payment(pay: bool, has_account: bool, balance: int, coins: int) -> bool:
return bool(pay and has_account and coins > 0 and balance >= coins)
def keep_episode(item: dict, *, allow_paid: bool, account_vip: bool) -> bool:
if paid_cost(item) > 0:
return allow_paid
if episode_kind(item) in VIP_TYPES and not already_bought(item):
return account_vip
return True
def buy_method(item: dict) -> str:
methods = item.get("methods")
if not isinstance(methods, list):
return ""
for method in methods:
if isinstance(method, str) and method.strip():
return method.strip()
if isinstance(method, dict):
for key in ("code", "method", "type", "id", "name"):
text = str(method.get(key) or "").strip()
if text:
return text
return ""
def _int(value) -> int:
try:
number = int(float(str(value)))
except (TypeError, ValueError):
return 0
return number if number > 0 else 0