Files
Jyotisha/scripts/market_astrology_source_connector.py
T
ClaudeandClaude Opus 5.5 6a57faacdb feat(consult): run the astrologer's upstream skill in full (BUG-1696, BUG-1697, BUG-1698)
- root SKILL.md is her upstream file (body byte-identical to the 5fb8379c mirror; description quoted so YAML parses and Mastra can load the skill)
- chat binds the whole SKILL.md plus references/product_runtime_contract.md (safety-only) instead of a ~477-character excerpt
- every turn carries her full strict-workflow-router (synced to upstream) and the domain's event-judgment file; product lists follow as an extra layer
- import her 10 sub-skills, family router, interpretation status contract, technique ontology and the 54 files her method names
- tests/test_upstream_skill_parity.py (in the quick gate) fails whenever her skill and ours drift
- 18 frontend guards that locked the excerpt design updated with 原值/新值/原因 notes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-07 14:02:26 +08:00

89 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Source connector for market / financial astrology data."""
from __future__ import annotations
import csv
import io
import json
from pathlib import Path
from typing import Any
from urllib.request import urlopen
def _normalize_price_row(row: dict[str, Any]) -> dict[str, Any] | None:
date = str(row.get("date") or row.get("timestamp") or "").strip()
close = row.get("close")
if not date or close is None:
return None
return {
"date": date,
"open": row.get("open"),
"high": row.get("high"),
"low": row.get("low"),
"close": close,
"volume": row.get("volume"),
}
def parse_market_payload(raw_text: str, *, content_type: str | None = None) -> list[dict[str, Any]]:
text = raw_text.strip()
if not text:
return []
is_json = (content_type or "").lower().find("json") >= 0 or text.startswith("{") or text.startswith("[")
if is_json:
payload = json.loads(text)
if isinstance(payload, dict):
payload = payload.get("rows") or payload.get("price_series") or []
rows = payload if isinstance(payload, list) else []
return [row for row in (_normalize_price_row(item) for item in rows if isinstance(item, dict)) if row]
reader = csv.DictReader(io.StringIO(text))
return [row for row in (_normalize_price_row(item) for item in reader if isinstance(item, dict)) if row]
def _normalize_label_row(row: dict[str, Any]) -> dict[str, Any] | None:
date = str(row.get("date") or row.get("timestamp") or "").strip()
label = str(row.get("label") or row.get("outcome") or row.get("regime") or "").strip()
if not date or not label:
return None
return {
"date": date,
"label": label,
"source": str(row.get("source") or ""),
}
def parse_market_labels(raw_text: str, *, content_type: str | None = None) -> list[dict[str, Any]]:
text = raw_text.strip()
if not text:
return []
is_json = (content_type or "").lower().find("json") >= 0 or text.startswith("{") or text.startswith("[")
if is_json:
payload = json.loads(text)
if isinstance(payload, dict):
payload = payload.get("rows") or payload.get("labels") or []
rows = payload if isinstance(payload, list) else []
return [row for row in (_normalize_label_row(item) for item in rows if isinstance(item, dict)) if row]
reader = csv.DictReader(io.StringIO(text))
return [row for row in (_normalize_label_row(item) for item in reader if isinstance(item, dict)) if row]
def fetch_market_payload(source: str) -> list[dict[str, Any]]:
if source.startswith("http://") or source.startswith("https://"):
with urlopen(source) as response: # nosec: local research connector only
content_type = response.headers.get_content_type()
raw_text = response.read().decode("utf-8")
return parse_market_payload(raw_text, content_type=content_type)
path = Path(source)
return parse_market_payload(path.read_text(encoding="utf-8"), content_type=path.suffix)
def fetch_market_labels(source: str) -> list[dict[str, Any]]:
if source.startswith("http://") or source.startswith("https://"):
with urlopen(source) as response: # nosec: local research connector only
content_type = response.headers.get_content_type()
raw_text = response.read().decode("utf-8")
return parse_market_labels(raw_text, content_type=content_type)
path = Path(source)
return parse_market_labels(path.read_text(encoding="utf-8"), content_type=path.suffix)