hub-gif a43e1f9b1d feat(dataset): 搜索/宽表支持按解析后销量与评价量排序
新增 sales_sort_value、comment_count_sort_value 与 volume_parse;入库与迁移回填;sort=total_sales|comment_count;前端排序下拉文案。

Made-with: Cursor
2026-04-16 10:15:24 +08:00

51 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""从爬虫导出文案解析销量、评价量等非负整数(与 reporting.charts._cn_volume_int 同源)。"""
from __future__ import annotations
import re
def cn_volume_int(s: str | None) -> int:
"""
支持「亿」「万」及纯数字;如 ``已售50万+`` → 500000。
无法解析时返回 0。
"""
t = (s or "").strip().replace(",", "").replace("", "")
if not t:
return 0
m = re.search(r"(\d+(?:\.\d+)?)\s*亿", t)
if m:
return int(round(float(m.group(1)) * 100_000_000))
m = re.search(r"(\d+(?:\.\d+)?)\s*万", t)
if m:
return int(round(float(m.group(1)) * 10_000))
m2 = re.search(r"(\d+)", t)
if m2:
return int(m2.group(1))
return 0
def sales_sort_value_from_search_cells(total_sales: str, comment_sales_floor: str) -> int | None:
"""搜索行:优先 ``total_sales``,否则销量楼层;两列皆空则 None。"""
ts = (total_sales or "").strip()
fl = (comment_sales_floor or "").strip()
if not ts and not fl:
return None
v = cn_volume_int(ts)
if v > 0:
return v
v2 = cn_volume_int(fl)
if v2 > 0:
return v2
return 0
def comment_count_sort_value_from_cell(comment_count: str) -> int | None:
if not (comment_count or "").strip():
return None
return cn_volume_int(comment_count)
def comment_count_sort_value_from_merged(pipeline_comment_count: str) -> int | None:
"""宽表评价量列(与搜索侧口径类似的文案)。"""
return comment_count_sort_value_from_cell(pipeline_comment_count)