Skip to content

TrueHOOHA/shanxi-securities-tushare-skill / sxsc-tushare-analysis

sxsc-tushare-analysis

Verified

对用户提到的标的(沪深股票、指数、公募基金、期货)进行综合分析,输出带 ECharts 图表的 HTML 分析报告。 触发条件:用户说"分析""研究""评估""看看XX怎么样""XX基本面""XX技术面""XX估值""XX财务""深度研究""对比"等分析类请求,且标的属于 A 股/指数/公募基金/期货。 本 skill 自动调用 sxsc_tushare 取数并输出完整报告,接管数据获取与分析全流程。当本 skill 被触发时,数据获取类 skill 不应同时激活——分析已包含数据。

scripts/token_util.py

View on GitHub
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""token 文件读取工具:data_api 与 check_env 的唯一实现,避免两份逻辑漂移。

token 文件即 `sxsc_tushare.set_token()` 写入的位置,文件名随包构建而变
(读 `upass.TOKEN_F_P`,例如 sxsc_tk.csv / jsdata_tk.csv),内容为
首行表头 `token`、次行 token 值的 csv。
"""
import os
from typing import Optional


def token_file_path() -> str:
    """返回 token 文件路径。

    ⚠️ 文件名**不固定**:随 sxsc_tushare 构建来源而变,同为 1.2.12 也可能不同:
      - 新版构建(如 20261030):~/sxsc_tk.csv
      - 旧版构建(如 20231213):~/jsdata_tk.csv
    因此一律读 `upass.TOKEN_F_P`,不硬编码文件名。SDK 不可用时回退为 sxsc_tk.csv。
    """
    name = None
    try:
        import sxsc_tushare.upass as up
        name = getattr(up, "TOKEN_F_P", None)
    except Exception:
        name = None
    return os.path.join(os.path.expanduser("~"), name or "sxsc_tk.csv")


def _read_token_text(path: str) -> Optional[str]:
    """按文本解析 token 文件(容忍表头/注释/UTF-16/GBK)。

    只接受两种可明确判定的格式,避免把表头或注释当凭证:
      - 表头 `token`(或单行 `token,值`)+ 下一行值
      - 仅一行有效内容(无表头手写文件)
    多行且无 token 表头(如误写成 `tk\\n值`)返回 None,由调用方报"未找到 token"。
    """
    text = None
    for enc in ("utf-8-sig", "utf-16", "gbk"):
        try:
            with open(path, "r", encoding=enc) as f:
                text = f.read()
            break
        except (UnicodeDecodeError, UnicodeError):
            continue
        except OSError:
            return None
    if text is None:
        return None

    lines = [ln.strip() for ln in text.splitlines()]
    lines = [ln for ln in lines if ln and not ln.lstrip().startswith("#")]
    for i, ln in enumerate(lines):
        _low = ln.lower()
        if _low == "token":
            if i + 1 < len(lines):
                return lines[i + 1].split(",")[0].strip() or None
            return None
        if _low.startswith("token,"):
            return ln.split(",", 1)[1].strip() or None
    if len(lines) == 1:
        return lines[0].split(",")[0].strip() or None
    return None


def load_token() -> Optional[str]:
    """从 token 文件读取明文 token(不读环境变量)。

    该文件即 `sxsc_tushare.set_token()` 写入的位置:65 字节左右的 csv,
    首行表头 `token`,次行 token 值。手动创建即可,不需再经过环境变量。

    不直接调 `sx.get_token()` 的原因:它在失败时会向 stdout 打 `get token error.`,
    会把日志/报告输出弄脏;这里自己读并保持同上解析口径,另容忍“只有一行 token”的
    手写文件(无表头)与 UTF-16/GBK 编码(Windows PowerShell 常见)。

    Returns:
        token 字符串;文件不存在 / 为空 / 解析失败或不可读时返回 None。
    """
    path = token_file_path()
    if not os.path.isfile(path):
        return None

    try:
        import pandas as pd

        # dtype=str:纯数字 token 不会因类型推断丢掉前导零
        df = pd.read_csv(path, dtype=str)
        if not df.empty and "token" in df.columns:
            val = df.loc[0, "token"]
            if not pd.isna(val) and str(val).strip():
                return str(val).strip()
    except Exception:
        pass
    return _read_token_text(path)


def token_help() -> str:
    """token 缺失时的可操作提示。"""
    path = token_file_path()
    return (
        f"未找到可用 token。请把 token 写入:{path}\n"
        f"  方式一(推荐,SDK 自带):python -c \"import sxsc_tushare as sx; sx.set_token('你的token')\"\n"
        f"  方式二(手写文件):首行写 token,次行写 token 值,UTF-8 编码无 BOM\n"
        f"  若文件已存在仍报错:请检查文件读取权限与编码(支持 UTF-8/UTF-16/GBK)"
    )
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall