diff --git a/.gitignore b/.gitignore index 6e22234..c3ca81f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ __pycache__/ *.pyc .env +venv/ diff --git a/SKILL.md b/SKILL.md index 4eccfd0..dfaea9a 100644 --- a/SKILL.md +++ b/SKILL.md @@ -1,9 +1,9 @@ --- -name: stock-buddy +name: stockbuddy description: 港股分析助手,提供港股技术面和基本面综合分析,给出买入/卖出操作建议。支持单只股票查询分析、持仓批量分析和持仓管理。当用户提到"港股"、"股票分析"、"持仓分析"、"买入建议"、"卖出建议"、港股代码(如 0700.HK、700)或港股公司名称(如腾讯、阿里、比亚迪)时触发此技能。 --- -# 港股分析助手 (Stock Buddy) +# 港股分析助手 (StockBuddy) ## 概述 diff --git a/scripts/analyze_stock.py b/scripts/analyze_stock.py index bb551fd..57e2f53 100755 --- a/scripts/analyze_stock.py +++ b/scripts/analyze_stock.py @@ -38,8 +38,10 @@ except ImportError: # 缓存与重试机制 # ───────────────────────────────────────────── -CACHE_DIR = Path.home() / ".stock_buddy_cache" +DATA_DIR = Path.home() / ".stockbuddy" +CACHE_DIR = DATA_DIR / "cache" CACHE_TTL_SECONDS = 600 # 缓存有效期 10 分钟 +LEGACY_CACHE_DIR = Path.home() / ".stock_buddy_cache" MAX_RETRIES = 3 RETRY_BASE_DELAY = 2 @@ -53,8 +55,22 @@ def _cache_key(code: str, period: str) -> str: def _read_cache(code: str, period: str) -> dict | None: """读取缓存""" cache_file = CACHE_DIR / _cache_key(code, period) + if not cache_file.exists(): + legacy_cache_file = LEGACY_CACHE_DIR / _cache_key(code, period) + if legacy_cache_file.exists(): + try: + DATA_DIR.mkdir(parents=True, exist_ok=True) + CACHE_DIR.mkdir(parents=True, exist_ok=True) + cache_file.write_text( + legacy_cache_file.read_text(encoding="utf-8"), + encoding="utf-8", + ) + except OSError: + cache_file = legacy_cache_file + if not cache_file.exists(): return None + try: with open(cache_file, "r", encoding="utf-8") as f: cached = json.load(f) @@ -69,6 +85,7 @@ def _read_cache(code: str, period: str) -> dict | None: def _write_cache(code: str, period: str, data: dict): """写入缓存""" + DATA_DIR.mkdir(parents=True, exist_ok=True) CACHE_DIR.mkdir(parents=True, exist_ok=True) cache_file = CACHE_DIR / _cache_key(code, period) try: @@ -710,8 +727,12 @@ def main(): if args.clear_cache: import shutil - if CACHE_DIR.exists(): - shutil.rmtree(CACHE_DIR) + cleared = False + for path in (CACHE_DIR, LEGACY_CACHE_DIR): + if path.exists(): + shutil.rmtree(path) + cleared = True + if cleared: print("✅ 缓存已清除") else: print("ℹ️ 无缓存可清除") diff --git a/scripts/portfolio_manager.py b/scripts/portfolio_manager.py index b633e98..0283661 100755 --- a/scripts/portfolio_manager.py +++ b/scripts/portfolio_manager.py @@ -9,7 +9,7 @@ python3 portfolio_manager.py update <代码> [--price <价格>] [--shares <数量>] [--note <备注>] python3 portfolio_manager.py analyze [--output <输出文件>] -持仓文件默认保存在: ~/.hk_stock_portfolio.json +持仓文件默认保存在: ~/.stockbuddy/portfolio.json """ import sys @@ -20,11 +20,17 @@ import time from datetime import datetime from pathlib import Path -PORTFOLIO_PATH = Path.home() / ".hk_stock_portfolio.json" +DATA_DIR = Path.home() / ".stockbuddy" +PORTFOLIO_PATH = DATA_DIR / "portfolio.json" +LEGACY_PORTFOLIO_PATH = Path.home() / ".hk_stock_portfolio.json" def load_portfolio() -> dict: """加载持仓数据""" + if not PORTFOLIO_PATH.exists() and LEGACY_PORTFOLIO_PATH.exists(): + DATA_DIR.mkdir(parents=True, exist_ok=True) + PORTFOLIO_PATH.write_text(LEGACY_PORTFOLIO_PATH.read_text(encoding="utf-8"), encoding="utf-8") + if not PORTFOLIO_PATH.exists(): return {"positions": [], "updated_at": None} with open(PORTFOLIO_PATH, "r", encoding="utf-8") as f: @@ -34,6 +40,7 @@ def load_portfolio() -> dict: def save_portfolio(data: dict): """保存持仓数据""" data["updated_at"] = datetime.now().isoformat() + DATA_DIR.mkdir(parents=True, exist_ok=True) with open(PORTFOLIO_PATH, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) diff --git a/venv/bin/python b/venv/bin/python deleted file mode 120000 index b8a0adb..0000000 --- a/venv/bin/python +++ /dev/null @@ -1 +0,0 @@ -python3 \ No newline at end of file diff --git a/venv/bin/python3 b/venv/bin/python3 deleted file mode 120000 index ae65fda..0000000 --- a/venv/bin/python3 +++ /dev/null @@ -1 +0,0 @@ -/usr/bin/python3 \ No newline at end of file diff --git a/venv/bin/python3.12 b/venv/bin/python3.12 deleted file mode 120000 index b8a0adb..0000000 --- a/venv/bin/python3.12 +++ /dev/null @@ -1 +0,0 @@ -python3 \ No newline at end of file diff --git a/venv/lib64 b/venv/lib64 deleted file mode 120000 index 7951405..0000000 --- a/venv/lib64 +++ /dev/null @@ -1 +0,0 @@ -lib \ No newline at end of file diff --git a/venv/pyvenv.cfg b/venv/pyvenv.cfg deleted file mode 100644 index 1abc7db..0000000 --- a/venv/pyvenv.cfg +++ /dev/null @@ -1,5 +0,0 @@ -home = /usr/bin -include-system-site-packages = false -version = 3.12.3 -executable = /usr/bin/python3.12 -command = /usr/bin/python3 -m venv /home/openclaw/.openclaw/workspace/stock-buddy/venv