Fix live ingest, email config, and dependencies for homelab deploy.
CI / security-scan (pull_request) Failing after 28s
CI / lint-and-test (pull_request) Failing after 31s
CI / dependency-scan (pull_request) Successful in 22s
CI / docker-build-test (pull_request) Failing after 34s
CI / workflow-summary (pull_request) Successful in 4s
CI / security-scan (pull_request) Failing after 28s
CI / lint-and-test (pull_request) Failing after 31s
CI / dependency-scan (pull_request) Successful in 22s
CI / docker-build-test (pull_request) Failing after 34s
CI / workflow-summary (pull_request) Successful in 4s
Switch HouseWatcherClient to public STOCK Act JSON (legacy housestockwatcher.com is down), add httpx/scikit-learn to pyproject, wire SMTP settings through Settings, fix get_session() as a context manager, and use savepoints in TradeLoader so one bad row does not roll back the batch.
This commit is contained in:
+6
-6
@@ -1,18 +1,18 @@
|
|||||||
# Email Setup for levkin.ca
|
# Email Setup for levkine.ca (Mailcow)
|
||||||
|
|
||||||
Your POTE system is configured to use `test@levkin.ca` for sending reports.
|
Homelab POTE sends via Mailcow **`mail.levkine.ca`** using the shared **`alerts@levkine.ca`** mailbox (same as Kuma/Beszel). See ansible `docs/guides/smtp-inventory.md`.
|
||||||
|
|
||||||
## ✅ Configuration Done
|
## ✅ Configuration Done
|
||||||
|
|
||||||
The `.env` file has been created with these settings:
|
The `.env` file has been created with these settings:
|
||||||
|
|
||||||
```env
|
```env
|
||||||
SMTP_HOST=mail.levkin.ca
|
SMTP_HOST=10.0.10.132
|
||||||
SMTP_PORT=587
|
SMTP_PORT=587
|
||||||
SMTP_USER=test@levkin.ca
|
SMTP_USER=alerts@levkine.ca
|
||||||
SMTP_PASSWORD=YOUR_MAILBOX_PASSWORD_HERE
|
SMTP_PASSWORD=YOUR_MAILBOX_PASSWORD_HERE
|
||||||
FROM_EMAIL=test@levkin.ca
|
FROM_EMAIL=alerts@levkine.ca
|
||||||
REPORT_RECIPIENTS=test@levkin.ca
|
REPORT_RECIPIENTS=idobkin@gmail.com
|
||||||
```
|
```
|
||||||
|
|
||||||
## 🔑 Next Steps
|
## 🔑 Next Steps
|
||||||
|
|||||||
@@ -18,8 +18,10 @@ dependencies = [
|
|||||||
"pydantic-settings>=2.0",
|
"pydantic-settings>=2.0",
|
||||||
"python-dotenv>=1.0",
|
"python-dotenv>=1.0",
|
||||||
"requests>=2.31",
|
"requests>=2.31",
|
||||||
|
"httpx>=0.27",
|
||||||
"pandas>=2.0",
|
"pandas>=2.0",
|
||||||
"numpy>=1.24",
|
"numpy>=1.24",
|
||||||
|
"scikit-learn>=1.3",
|
||||||
"yfinance>=0.2",
|
"yfinance>=0.2",
|
||||||
"psycopg2-binary>=2.9",
|
"psycopg2-binary>=2.9",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ def main():
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
logger.info("=== Fetching Congressional Trades from House Stock Watcher ===")
|
logger.info("=== Fetching Congressional Trades (public STOCK Act data) ===")
|
||||||
logger.info("Source: https://housestockwatcher.com (free, no API key)")
|
logger.info("Source: public JSON feeds (see HouseWatcherClient.data_urls)")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with HouseWatcherClient() as client:
|
with HouseWatcherClient() as client:
|
||||||
|
|||||||
@@ -30,6 +30,16 @@ class Settings(BaseSettings):
|
|||||||
# Logging
|
# Logging
|
||||||
log_level: str = Field(default="INFO", description="Log level (DEBUG, INFO, WARNING, ERROR)")
|
log_level: str = Field(default="INFO", description="Log level (DEBUG, INFO, WARNING, ERROR)")
|
||||||
|
|
||||||
|
# Email (Mailcow @ mail.levkine.ca — use LAN IP from app LXCs if DNS points public)
|
||||||
|
smtp_host: str = Field(default="mail.levkine.ca", description="SMTP server hostname")
|
||||||
|
smtp_port: int = Field(default=587, description="SMTP port (587 STARTTLS)")
|
||||||
|
smtp_user: str = Field(default="", description="SMTP auth username")
|
||||||
|
smtp_password: str = Field(default="", description="SMTP auth password")
|
||||||
|
from_email: str = Field(default="", description="From address for reports")
|
||||||
|
report_recipients: str = Field(
|
||||||
|
default="", description="Default report recipients (comma-separated)"
|
||||||
|
)
|
||||||
|
|
||||||
# Application
|
# Application
|
||||||
app_name: str = "POTE"
|
app_name: str = "POTE"
|
||||||
app_version: str = "0.1.0"
|
app_version: str = "0.1.0"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ Database layer: engine, session factory, and base model.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from collections.abc import Generator
|
from collections.abc import Generator
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
||||||
@@ -26,8 +27,9 @@ class Base(DeclarativeBase):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
def get_session() -> Generator[Session, None, None]:
|
def get_session() -> Generator[Session, None, None]:
|
||||||
"""Get a database session (use as a context manager or dependency)."""
|
"""Get a database session (context manager or FastAPI-style dependency)."""
|
||||||
session = SessionLocal()
|
session = SessionLocal()
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
"""
|
"""
|
||||||
House Stock Watcher client for fetching congressional trade data.
|
House Stock Watcher client for fetching congressional trade data.
|
||||||
Free, no API key required - scrapes from housestockwatcher.com
|
|
||||||
|
Uses public STOCK Act disclosure datasets (no API key). The legacy
|
||||||
|
housestockwatcher.com host is often unavailable; we try several mirrors.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
from datetime import date, datetime
|
from datetime import date, datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -11,16 +14,69 @@ import httpx
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
def _default_data_urls() -> tuple[str, ...]:
|
||||||
|
override = os.environ.get("POTE_HOUSE_DATA_URL", "").strip()
|
||||||
|
if override:
|
||||||
|
return (override,)
|
||||||
|
return (
|
||||||
|
"https://raw.githubusercontent.com/kadoa-org/congress-trading-monitor/main/public/data/trades.json",
|
||||||
|
"https://housestockwatcher.com/api/all_transactions",
|
||||||
|
"https://house-stock-watcher-data.s3-us-west-2.amazonaws.com/data/all_transactions.json",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
DEFAULT_DATA_URLS: tuple[str, ...] = _default_data_urls()
|
||||||
|
|
||||||
|
|
||||||
|
def _ascii_safe(text: str | None) -> str:
|
||||||
|
"""Normalize text for DB fields that may use ASCII-only client encoding."""
|
||||||
|
if not text:
|
||||||
|
return ""
|
||||||
|
return text.replace("\u00b7", "-").replace("·", "-").strip()
|
||||||
|
|
||||||
|
|
||||||
|
def _party_label(code: str | None) -> str:
|
||||||
|
if not code:
|
||||||
|
return ""
|
||||||
|
mapping = {"D": "Democrat", "R": "Republican", "I": "Independent"}
|
||||||
|
return mapping.get(code.strip().upper(), code.strip())
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_transaction_record(raw: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Map a raw disclosure record to the House Stock Watcher field names
|
||||||
|
expected by TradeLoader.
|
||||||
|
"""
|
||||||
|
if "representative" in raw and "disclosure_date" in raw:
|
||||||
|
return raw
|
||||||
|
|
||||||
|
# Kadoa congress-trading-monitor export
|
||||||
|
if "filer_name" in raw:
|
||||||
|
chamber = (raw.get("chamber") or "").strip().lower()
|
||||||
|
house = "Senate" if chamber == "senate" else "House"
|
||||||
|
return {
|
||||||
|
"representative": raw.get("filer_name", "").strip(),
|
||||||
|
"ticker": (raw.get("ticker") or "").strip(),
|
||||||
|
"transaction_date": raw.get("transaction_date", ""),
|
||||||
|
"disclosure_date": raw.get("filing_date", ""),
|
||||||
|
"transaction": raw.get("transaction_type", ""),
|
||||||
|
"amount": raw.get("amount_range_label", ""),
|
||||||
|
"house": house,
|
||||||
|
"district": _ascii_safe(raw.get("office")),
|
||||||
|
"party": _party_label(raw.get("party")),
|
||||||
|
}
|
||||||
|
|
||||||
|
return raw
|
||||||
|
|
||||||
|
|
||||||
class HouseWatcherClient:
|
class HouseWatcherClient:
|
||||||
"""
|
"""
|
||||||
Client for House Stock Watcher API (free, community-maintained).
|
Client for congressional trade JSON feeds (free, community-maintained).
|
||||||
|
|
||||||
Data source: https://housestockwatcher.com/
|
Primary source: congress-trading-monitor public dataset on GitHub.
|
||||||
No authentication required.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
BASE_URL = "https://housestockwatcher.com/api"
|
data_urls: tuple[str, ...] = DEFAULT_DATA_URLS
|
||||||
|
|
||||||
def __init__(self, timeout: float = 30.0):
|
def __init__(self, timeout: float = 30.0):
|
||||||
"""
|
"""
|
||||||
@@ -65,30 +121,31 @@ class HouseWatcherClient:
|
|||||||
Raises:
|
Raises:
|
||||||
httpx.HTTPError: If request fails
|
httpx.HTTPError: If request fails
|
||||||
"""
|
"""
|
||||||
url = f"{self.BASE_URL}/all_transactions"
|
last_error: Exception | None = None
|
||||||
|
for url in self.data_urls:
|
||||||
|
if not url:
|
||||||
|
continue
|
||||||
logger.info(f"Fetching transactions from {url}")
|
logger.info(f"Fetching transactions from {url}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = self._client.get(url)
|
response = self._client.get(url)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
data = response.json()
|
data = response.json()
|
||||||
|
|
||||||
if not isinstance(data, list):
|
if not isinstance(data, list):
|
||||||
raise ValueError(f"Expected list response, got {type(data)}")
|
raise ValueError(f"Expected list response, got {type(data)}")
|
||||||
|
data = [normalize_transaction_record(item) for item in data]
|
||||||
logger.info(f"Fetched {len(data)} transactions from House Stock Watcher")
|
logger.info(f"Fetched {len(data)} transactions from {url}")
|
||||||
|
|
||||||
if limit:
|
if limit:
|
||||||
data = data[:limit]
|
data = data[:limit]
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
except httpx.HTTPError as e:
|
|
||||||
logger.error(f"Failed to fetch from House Stock Watcher: {e}")
|
|
||||||
raise
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Unexpected error fetching transactions: {e}")
|
last_error = e
|
||||||
raise
|
logger.warning(f"Failed to fetch from {url}: {e}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
logger.error("Failed to fetch congressional trades from all configured URLs")
|
||||||
|
if last_error:
|
||||||
|
raise last_error
|
||||||
|
raise RuntimeError("No data URLs configured")
|
||||||
|
|
||||||
def fetch_recent_transactions(self, days: int = 30) -> list[dict[str, Any]]:
|
def fetch_recent_transactions(self, days: int = 30) -> list[dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -45,12 +45,11 @@ class TradeLoader:
|
|||||||
|
|
||||||
for txn in transactions:
|
for txn in transactions:
|
||||||
try:
|
try:
|
||||||
# Get or create official
|
with self.session.begin_nested():
|
||||||
official, is_new_official = self._get_or_create_official(txn)
|
official, is_new_official = self._get_or_create_official(txn)
|
||||||
if is_new_official:
|
if is_new_official:
|
||||||
officials_created += 1
|
officials_created += 1
|
||||||
|
|
||||||
# Get or create security
|
|
||||||
ticker = txn.get("ticker", "").strip().upper()
|
ticker = txn.get("ticker", "").strip().upper()
|
||||||
if not ticker or ticker in ("N/A", "--", ""):
|
if not ticker or ticker in ("N/A", "--", ""):
|
||||||
logger.debug(
|
logger.debug(
|
||||||
@@ -62,7 +61,6 @@ class TradeLoader:
|
|||||||
if is_new_security:
|
if is_new_security:
|
||||||
securities_created += 1
|
securities_created += 1
|
||||||
|
|
||||||
# Create trade (upsert)
|
|
||||||
trade_created = self._upsert_trade(txn, official.id, security.id, source)
|
trade_created = self._upsert_trade(txn, official.id, security.id, source)
|
||||||
if trade_created:
|
if trade_created:
|
||||||
trades_created += 1
|
trades_created += 1
|
||||||
|
|||||||
Reference in New Issue
Block a user