feat(watchlist): add company identity catalog
Embed a pinned FinanceDatabase equity snapshot, add authenticated company search and identity-based follows, preserve legacy assets, and map selected Yahoo symbols for collection. Refs #19 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
11
backend/scripts/company-catalog-requirements.txt
Normal file
11
backend/scripts/company-catalog-requirements.txt
Normal file
@ -0,0 +1,11 @@
|
||||
certifi==2026.11.20
|
||||
charset-normalizer==3.5.2
|
||||
idna==3.20
|
||||
numpy==2.4.6
|
||||
pandas==3.0.3
|
||||
polars==2.1.0
|
||||
polars-runtime-32==2.1.0
|
||||
python-dateutil==4.0.0
|
||||
requests==2.34.2
|
||||
tzdata==2026.6
|
||||
urllib3==2.8.0
|
||||
45
backend/scripts/company-catalog-upstream.json
Normal file
45
backend/scripts/company-catalog-upstream.json
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"license_sha256": "dea317ce7193c52174ebb0def6df1a2f762f75b60e5bc026176fea6cf3ede4ff",
|
||||
"upstream_commit": "93b872f0e4b4e05e8d35b7f0a39efe9245bcc2c1",
|
||||
"upstream_equities_data_sha256": "decfcf03f1294dabe09a591a5015775dc49a5a54a3536e35b4869659a5123f7f",
|
||||
"upstream_source_sha256": {
|
||||
"financedatabase/__init__.py": "607e4c1f0f772d2f51f6004b06fe4a905441d29142fa1f06988f549ce5c8e794",
|
||||
"financedatabase/cache_model.py": "1372be4459874b7494c82a9ca8b869803bb843ef6ee5132df67f9f0a710a1e88",
|
||||
"financedatabase/categories_model.py": "f4f47fd5d90edc476f7ce2cf524977a0dc4115dfacfe2f914c6b3566900648f4",
|
||||
"financedatabase/cryptos/__init__.py": "896a0c2f55ea897a3f6c136c0e0e33118de0ff9274edccd9ae0bb29b26770d74",
|
||||
"financedatabase/cryptos/cryptos_controller.py": "953ff91ef8a8043987e2cf1d552332e6cb511f69aa85483186301fee0bb79fc2",
|
||||
"financedatabase/currencies/__init__.py": "c1884f7dbcc322ea64d210696fec6ed6508e05e63614f35523d8ea44fdc23adc",
|
||||
"financedatabase/currencies/currencies_controller.py": "a427110863e835e99ecd153d9b5d960520d6052e22aacff7ef0ad994c09b311d",
|
||||
"financedatabase/database_controller.py": "56b9db9cd3363a8993f13a19bb1530d3d77fef9c945acb75df4ca4c853d6e1d7",
|
||||
"financedatabase/equities/__init__.py": "55b8e55b48cf515b1ec3e3d1266550e3cf00f21fba584e4394cd2264dc4a84df",
|
||||
"financedatabase/equities/equities_controller.py": "0fd7d859fa2f43f7c3d43484a21a8d5ccfcdcb2276e2a4a1d1c94eb838b88bc5",
|
||||
"financedatabase/etfs/__init__.py": "5cb9a938a8130ed8a3af1c19095a5d896f809ca24c50f66586a67b31937304b9",
|
||||
"financedatabase/etfs/etfs_controller.py": "2944bb089470bafc149b94e6f1719081849be1aec62e702df59fe482a5fc51f0",
|
||||
"financedatabase/frame_model.py": "1e36264d193952a8ddd51ccd5ccc3b6ad1dea55b3c5041ad68112f2c29210386",
|
||||
"financedatabase/funds/__init__.py": "e67772c33f8130fa8867723f7b41efe7e9b776ea4107db81e399eaec136e369e",
|
||||
"financedatabase/funds/funds_controller.py": "9a282674b0ac1c5d925fd65955ac7d66899cd788c1dfc74379aacd2c12bf72a6",
|
||||
"financedatabase/helpers.py": "87946e2257254b6d6700716c27f6edc964bdb4aeba847cf958f0ec70a9f9bb49",
|
||||
"financedatabase/indices/__init__.py": "5f5915ef8e0d5e5ef4efe04139db3397ec2a59fc2b8c08e370685959aff51733",
|
||||
"financedatabase/indices/indices_controller.py": "d67f8b26b3d72de503fc62ae987021a6a9bed78a99bd30a6be203d742a7a514c",
|
||||
"financedatabase/listings_model.py": "44f9b99fda44019e09c37be241d2fd1cdcf52afd1a6b1d33eb7ac54b7132b48d",
|
||||
"financedatabase/mcp_server/__init__.py": "9df6de1448fd4d659e8463d6548c90b40b6c47f5492dffc5ceb2b585d3993c18",
|
||||
"financedatabase/mcp_server/__main__.py": "51796c03ba817474a84161ccbb3900838651acee63a47dec7e95a59d4815363a",
|
||||
"financedatabase/mcp_server/analytics_model.py": "426775e4d6c8d140b4cc009139ad5ce5e24b31bf32f8cb3dc1620676b8d418f5",
|
||||
"financedatabase/mcp_server/coercion_model.py": "c309112cd0e76c70e777613a9b9d0bd0c09f0f8522d24a72c1d2dcb0a4e9cdc7",
|
||||
"financedatabase/mcp_server/formatting_model.py": "2c3d32ec961352782894993b336c4ee58d47bf0ee25b762f5f5bd22688fde546",
|
||||
"financedatabase/mcp_server/mcp_controller.py": "fe1ebab3086b54855aeecffd4f43bd60257f5ae24fe0dbfdc0e3ea0242431dd7",
|
||||
"financedatabase/mcp_server/provider_model.py": "22bb45c6f192abbb29cf6aea8ee6b901a204d5960765ae7c10e575f002e9af55",
|
||||
"financedatabase/mcp_server/ranking_model.py": "f03e03de04fc719a0a4ec6beeeaadb261b403a1ad5b3abf057977af6cc924faa",
|
||||
"financedatabase/mcp_server/registry_model.py": "a4d4844268ef84fbcae1c82aa16e1ba4be34b0a295936661df13220dd3b4dd46",
|
||||
"financedatabase/mcp_server/setup_model.py": "69508873479ceacfbdb92a8bacfd988968c773b9adb67193dbcfee2d7acc92be",
|
||||
"financedatabase/mcp_server/site_model.py": "0cf95f37479086d6753653e382029ec8f2806e654e6d35e6dc611f75cb0e6a2f",
|
||||
"financedatabase/mcp_server/stats_page_model.py": "c53f4ee51851feb4050b8fb9724ea973c0b95305a205ee78456eb818dff6a9ca",
|
||||
"financedatabase/mcp_server/tools_model.py": "81cadc7360a3512503d0e126437fde168c7043e4578d7d7214bbba5c38d2cfd1",
|
||||
"financedatabase/moneymarkets/__init__.py": "d6e552766b18c4c577f730e23e622847f327d71cc0c8009140bdcd6384829cc8",
|
||||
"financedatabase/moneymarkets/moneymarkets_controller.py": "516f6db36f3cd5195cdbab70bda810575e0b875ce7b43e66087b5be6d336971f",
|
||||
"financedatabase/query_model.py": "a24ff8c88ffb4f5c4dcf7607e057e8b549b3d5f403539a821ca62935fd54e54b",
|
||||
"financedatabase/utilities/__init__.py": "8ae00bf057b5f6315fac4999e7a813d4b21696017b76246e44a08e20dc9dc282",
|
||||
"financedatabase/utilities/dataframe_model.py": "814cab4fca135b5c7b6f50818052c0a318f6bd8466338a8bead0c66270066aee",
|
||||
"financedatabase/utilities/logger_model.py": "9dcf03c9dca815a1d6a2d0b694f5937f98b4ede2a5e03abf36a19575b4387d92"
|
||||
}
|
||||
}
|
||||
114
backend/scripts/update-company-catalog.py
Normal file
114
backend/scripts/update-company-catalog.py
Normal file
@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Explicit maintenance only. Never imported/called by the application.
|
||||
Run with pinned dependencies in company-catalog-requirements.txt and -E.
|
||||
--source-dir uses an already downloaded upstream checkout completely offline.
|
||||
--download explicitly downloads only the pinned official source archive.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import socket
|
||||
import sys
|
||||
import tarfile
|
||||
import tempfile
|
||||
import urllib.request
|
||||
|
||||
COMMIT = "93b872f0e4b4e05e8d35b7f0a39efe9245bcc2c1"
|
||||
DATA_HASH = "decfcf03f1294dabe09a591a5015775dc49a5a54a3536e35b4869659a5123f7f"
|
||||
FIELDS = "symbol name exchange mic country isin figi composite_figi delisted listing_score primary_listing main_venue listings usual_currency home_known".split()
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
def digest(content):
|
||||
return hashlib.sha256(content).hexdigest()
|
||||
|
||||
def generate(source, destination):
|
||||
parquet = source / "compression/equities.parquet"
|
||||
if digest(parquet.read_bytes()) != DATA_HASH:
|
||||
raise SystemExit("Pinned equities parquet hash mismatch")
|
||||
lock = json.loads((ROOT / "scripts/company-catalog-upstream.json").read_text())
|
||||
if lock["upstream_commit"] != COMMIT or lock["upstream_equities_data_sha256"] != DATA_HASH:
|
||||
raise SystemExit("Upstream lock and generator constants disagree")
|
||||
for name, expected in lock["upstream_source_sha256"].items():
|
||||
if digest((source / name).read_bytes()) != expected:
|
||||
raise SystemExit("Pinned upstream source hash mismatch")
|
||||
if digest((source / "LICENSE").read_bytes()) != lock["license_sha256"]:
|
||||
raise SystemExit("Pinned upstream license hash mismatch")
|
||||
# The upstream selector is run over local data; refuse Python network access.
|
||||
def deny(*args, **kwargs):
|
||||
raise RuntimeError("Network forbidden while selecting the local snapshot")
|
||||
socket.socket.connect = deny
|
||||
socket.create_connection = deny
|
||||
os.environ.setdefault("POLARS_MAX_THREADS", "4")
|
||||
sys.path.insert(0, str(source))
|
||||
import financedatabase as fd
|
||||
import polars as pl
|
||||
equities = fd.Equities(use_local_location=True)
|
||||
selected = equities.select(only_primary_listing=True, exclude_delisted=True, as_pandas=False)
|
||||
rows = selected.join(equities.get_listing_ranks(), on="symbol", how="left").select(FIELDS).sort("symbol")
|
||||
if rows.height != 47735:
|
||||
raise SystemExit(f"Unexpected primary equity count {rows.height}, expected 47735")
|
||||
targets = {"NVDA": "NVIDIA Corporation", "AAPL": "Apple Inc.", "9888.HK": "Baidu, Inc.", "002594.SZ": "BYD Company Limited", "0285.HK": "BYD Electronic (International) Company Limited"}
|
||||
for symbol, name in targets.items():
|
||||
match = rows.filter(pl.col("symbol") == symbol)
|
||||
if match.height != 1 or match["name"][0] != name:
|
||||
raise SystemExit("Pinned primary target mismatch")
|
||||
content = rows.write_csv().encode("utf-8")
|
||||
identities = {}
|
||||
unidentified = 0
|
||||
for row in rows.iter_rows(named=True):
|
||||
identity = "isin:" + row["isin"] if row["isin"] else ("figi:" + row["figi"] if row["figi"] else None)
|
||||
if identity:
|
||||
identities[identity] = identities.get(identity, 0) + 1
|
||||
else:
|
||||
unidentified += 1
|
||||
manifest = {
|
||||
"runtime_resolvable_identities": len(identities),
|
||||
"rows_without_stable_identity": unidentified,
|
||||
"identities_with_multiple_primary_rows": sum(count > 1 for count in identities.values()),
|
||||
"runtime_identity_policy": "ISIN else FIGI; group identical identity; highest upstream listing_score; equal-score primary rows use symbol lexical tie-break for a deterministic representative; unidentified rows remain embedded but are not searchable/followable.",
|
||||
"upstream_repository": "https://github.com/JerBouma/FinanceDatabase",
|
||||
"upstream_commit": COMMIT,
|
||||
"upstream_commit_date_utc": "2026-11-17T13:27:37Z",
|
||||
"package_version": "2.5.2", "license": "MIT",
|
||||
"upstream_equities_data_file": "compression/equities.parquet",
|
||||
"upstream_equities_data_sha256": DATA_HASH,
|
||||
"selection": "Equities(use_local_location=True).select(only_primary_listing=True, exclude_delisted=True, as_pandas=False); join get_listing_ranks(); sort symbol",
|
||||
"primary_listing_evidence": "Unmodified commit-pinned upstream listings_model over the complete local Equities data; no ticker heuristic or provider API.",
|
||||
"records": rows.height, "snapshot": "equities_primary.csv", "sha256": digest(content),
|
||||
"fields": FIELDS,
|
||||
"upstream_source_sha256": {str(p.relative_to(source)): digest(p.read_bytes()) for p in sorted((source / "financedatabase").rglob("*.py"))},
|
||||
"license_sha256": digest((source / "LICENSE").read_bytes()),
|
||||
}
|
||||
destination.mkdir(parents=True, exist_ok=True)
|
||||
# Generate everything before replacing existing artifacts; no cache purge.
|
||||
with tempfile.TemporaryDirectory(dir=destination) as staging:
|
||||
stage = Path(staging)
|
||||
(stage / "equities_primary.csv").write_bytes(content)
|
||||
(stage / "manifest.json").write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
||||
shutil.copyfile(source / "LICENSE", stage / "FINANCEDATABASE_LICENSE")
|
||||
for name in ("equities_primary.csv", "manifest.json", "FINANCEDATABASE_LICENSE"):
|
||||
os.replace(stage / name, destination / name)
|
||||
print(f"Equity primary snapshot: {rows.height} records; sha256={digest(content)}")
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument("--source-dir", type=Path)
|
||||
group.add_argument("--download", action="store_true")
|
||||
parser.add_argument("--output", type=Path, default=ROOT / "internal/companycatalog/data")
|
||||
args = parser.parse_args()
|
||||
if args.source_dir:
|
||||
generate(args.source_dir.resolve(), args.output)
|
||||
else:
|
||||
with tempfile.TemporaryDirectory(prefix="company-catalog-", dir=os.environ.get("IDEA_MANAGED_TMP") or os.environ.get("TMPDIR")) as work:
|
||||
archive = urllib.request.urlopen(f"https://codeload.github.com/JerBouma/FinanceDatabase/tar.gz/{COMMIT}", timeout=120).read()
|
||||
with tarfile.open(fileobj=io.BytesIO(archive), mode="r:gz") as tar:
|
||||
tar.extractall(work, filter="data")
|
||||
generate(Path(work) / f"FinanceDatabase-{COMMIT}", args.output)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user