Spaces:
Running
Running
File size: 23,734 Bytes
b163e21 865ae4b ccc0d44 865ae4b b163e21 ccc0d44 b163e21 ccc0d44 b163e21 3c5bd91 b163e21 ccc0d44 3c5bd91 b163e21 ccc0d44 b163e21 3c5bd91 b163e21 8c122be b163e21 31a340a b163e21 8c122be b163e21 cd8f75a b163e21 31a340a b163e21 31a340a b163e21 8c122be b163e21 31a340a 8c122be b163e21 8c122be b163e21 31a340a b163e21 31a340a b163e21 31a340a b163e21 31a340a b163e21 8c122be b163e21 31a340a b163e21 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | """
Data Manager: News and price data ingestion.
Handles:
- NewsAPI fetching (if API key provided)
- RSS feed fallback (Google News)
- Fuzzy deduplication for RSS noise
- Multi-symbol yfinance price ingestion
- Language filtering for FinBERT compatibility
Usage:
python -m app.data_manager --fetch
python -m app.data_manager --fetch --news-only
python -m app.data_manager --fetch --prices-only
"""
import argparse
import logging
from datetime import datetime, timedelta, timezone
from typing import Optional
import requests
import yfinance as yf
from rapidfuzz import fuzz
from langdetect import detect, LangDetectException
from sqlalchemy.dialects.sqlite import insert as sqlite_insert
from sqlalchemy.dialects.postgresql import insert as pg_insert
from sqlalchemy.orm import Session
from app.db import SessionLocal, init_db, get_db_type
from app.models import NewsArticle, PriceBar
from app.settings import get_settings
from app.rss_ingest import fetch_google_news
from app.utils import (
clean_text,
canonical_title,
normalize_url,
generate_dedup_key,
truncate_text,
)
from app.lock import pipeline_lock
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
def get_upsert_stmt(table, values: dict, index_elements: list, update_set: dict = None):
"""Create database-agnostic upsert statement."""
db_type = get_db_type()
if db_type == "postgresql":
stmt = pg_insert(table).values(**values)
if update_set:
stmt = stmt.on_conflict_do_update(index_elements=index_elements, set_=update_set)
else:
stmt = stmt.on_conflict_do_nothing(index_elements=index_elements)
else:
# SQLite
stmt = sqlite_insert(table).values(**values)
if update_set:
stmt = stmt.on_conflict_do_update(index_elements=index_elements, set_=update_set)
else:
stmt = stmt.on_conflict_do_nothing(index_elements=index_elements)
return stmt
# =============================================================================
# NewsAPI Fetching
# =============================================================================
def fetch_newsapi_articles(
api_key: str,
query: str,
language: str = "en",
lookback_days: int = 30,
page_size: int = 100
) -> list[dict]:
"""
Fetch articles from NewsAPI.
Note: Free plan limits to ~1 month of history.
"""
logger.info(f"Fetching from NewsAPI: query='{query}', language={language}")
# Calculate date range
to_date = datetime.now(timezone.utc)
from_date = to_date - timedelta(days=min(lookback_days, 30)) # API limit
url = "https://newsapi.org/v2/everything"
params = {
"apiKey": api_key,
"q": query,
"language": language,
"from": from_date.strftime("%Y-%m-%d"),
"to": to_date.strftime("%Y-%m-%d"),
"sortBy": "publishedAt",
"pageSize": page_size,
}
try:
response = requests.get(url, params=params, timeout=30)
response.raise_for_status()
data = response.json()
if data.get("status") != "ok":
logger.error(f"NewsAPI error: {data.get('message', 'Unknown error')}")
return []
articles = []
for item in data.get("articles", []):
try:
published_str = item.get("publishedAt", "")
published_at = datetime.fromisoformat(published_str.replace("Z", "+00:00")) if published_str else datetime.now(timezone.utc)
articles.append({
"title": item.get("title", ""),
"description": item.get("description", ""),
"content": item.get("content", ""),
"url": item.get("url", ""),
"source": item.get("source", {}).get("name", ""),
"author": item.get("author", ""),
"published_at": published_at,
})
except Exception as e:
logger.debug(f"Error parsing NewsAPI article: {e}")
continue
logger.info(f"Fetched {len(articles)} articles from NewsAPI")
return articles
except requests.RequestException as e:
logger.error(f"NewsAPI request failed: {e}")
return []
# =============================================================================
# Language Detection
# =============================================================================
def detect_language(text: str) -> Optional[str]:
"""Detect language of text. Returns None if detection fails."""
if not text or len(text) < 20:
return None
try:
return detect(text)
except LangDetectException:
return None
def filter_by_language(
articles: list[dict],
target_language: str = "en"
) -> tuple[list[dict], int]:
"""
Filter articles by language.
Returns:
Tuple of (filtered_articles, num_filtered_out)
"""
filtered = []
filtered_out = 0
for article in articles:
# Try to detect from title + description
text = f"{article.get('title', '')} {article.get('description', '')}"
lang = detect_language(text)
if lang is None or lang == target_language:
filtered.append(article)
else:
filtered_out += 1
logger.debug(f"Filtered out ({lang}): {article.get('title', '')[:50]}")
if filtered_out > 0:
logger.info(f"Language filter: kept {len(filtered)}, filtered out {filtered_out}")
return filtered, filtered_out
# =============================================================================
# Fuzzy Deduplication
# =============================================================================
def get_recent_titles(
session: Session,
window_hours: int = 48
) -> list[str]:
"""Get canonical titles from recent articles for fuzzy dedup."""
cutoff = datetime.now(timezone.utc) - timedelta(hours=window_hours)
articles = session.query(NewsArticle.canonical_title).filter(
NewsArticle.published_at >= cutoff,
NewsArticle.canonical_title.isnot(None)
).all()
return [a[0] for a in articles if a[0]]
def is_fuzzy_duplicate(
title: str,
existing_titles: list[str],
threshold: int = 85
) -> bool:
"""
Check if title is too similar to existing titles.
Uses token_set_ratio for robust matching.
"""
if not title or not existing_titles:
return False
canon = canonical_title(title)
for existing in existing_titles:
similarity = fuzz.token_set_ratio(canon, existing)
if similarity >= threshold:
logger.debug(f"Fuzzy duplicate ({similarity}%): '{title[:50]}...'")
return True
return False
# =============================================================================
# News Ingestion
# =============================================================================
def ingest_news(session: Session) -> dict:
"""
Ingest news from all configured sources.
Returns:
Dict with stats: imported, duplicates, language_filtered, fuzzy_filtered
"""
settings = get_settings()
# Strategic queries based on S&P Global 2026 copper market report
# Each pipeline run focuses on different strategic topics for diversity
STRATEGIC_QUERIES = [
# Supply Crisis / Deficit Focus
"copper supply deficit 2026",
"copper shortage AI data center",
"copper inventory LME warehouse",
# Key Players (Majors & Producers)
"Freeport-McMoRan copper outlook",
"BHP copper production news",
"Rio Tinto copper investment",
"Southern Copper SCCO forecast",
# China & Emerging Markets
"Zijin Mining copper investment",
"China copper demand stimulus",
"copper demand EV battery",
# M&A & Strategic Moves
"copper mining acquisition merger",
"Ivanhoe Mines copper grade",
"Lundin Mining copper deal",
# Price & Macro Analysis
"copper price forecast Goldman Sachs",
"copper futures CME analysis",
"grade decline copper mining",
]
logger.info(f"🕵️ Strategic News Agent: Investigating {len(STRATEGIC_QUERIES)} topics...")
stats = {
"imported": 0,
"duplicates": 0,
"language_filtered": 0,
"fuzzy_filtered": 0,
"source": "unknown",
"queries_used": len(STRATEGIC_QUERIES),
}
# Collect articles from ALL strategic queries
all_articles = []
seen_urls = set() # Track URLs to avoid duplicates across queries
for i, strategic_query in enumerate(STRATEGIC_QUERIES, 1):
logger.info(f" [{i}/{len(STRATEGIC_QUERIES)}] Searching: '{strategic_query}'")
query_articles = []
# Try NewsAPI first if key is available
if settings.newsapi_key:
articles = fetch_newsapi_articles(
api_key=settings.newsapi_key,
query=strategic_query,
language=settings.news_language,
lookback_days=settings.lookback_days,
)
if articles:
query_articles.extend(articles)
# RSS fallback/supplement
if not query_articles or not settings.newsapi_key:
rss_articles = fetch_google_news(
query=strategic_query,
language=settings.news_language,
)
query_articles.extend(rss_articles)
# Deduplicate within this batch (by URL)
new_articles = 0
for article in query_articles:
url = article.get('url', '')
if url and url not in seen_urls:
seen_urls.add(url)
all_articles.append(article)
new_articles += 1
if new_articles > 0:
logger.info(f" → Found {new_articles} new articles ({len(query_articles) - new_articles} duplicates skipped)")
stats["source"] = "newsapi+rss" if settings.newsapi_key else "rss"
if not all_articles:
logger.warning("No articles fetched from any source")
return stats
logger.info(f"Total unique articles fetched: {len(all_articles)}")
# Language filter
all_articles, lang_filtered = filter_by_language(
all_articles,
target_language=settings.news_language
)
stats["language_filtered"] = lang_filtered
# Get recent titles for fuzzy dedup
recent_titles = get_recent_titles(
session,
window_hours=settings.fuzzy_dedup_window_hours
)
# Process articles
for article in all_articles:
try:
title = clean_text(article.get("title", ""))
if not title:
continue
# Fuzzy dedup check
if is_fuzzy_duplicate(
title,
recent_titles,
threshold=settings.fuzzy_dedup_threshold
):
stats["fuzzy_filtered"] += 1
continue
# Prepare fields
description = clean_text(article.get("description", ""))
content = clean_text(article.get("content", ""))
url = normalize_url(article.get("url", ""))
source = article.get("source", "Unknown")
author = article.get("author", "")
published_at = article.get("published_at", datetime.now(timezone.utc))
# Generate keys
dedup_key = generate_dedup_key(
url=url,
title=title,
published_at=published_at,
source=source
)
canon_title = canonical_title(title)
# Upsert
stmt = get_upsert_stmt(
NewsArticle,
values={
"dedup_key": dedup_key,
"title": truncate_text(title, 500),
"canonical_title": truncate_text(canon_title, 500),
"description": truncate_text(description, 2000) if description else None,
"content": truncate_text(content, 10000) if content else None,
"url": url or None,
"source": source,
"author": author or None,
"language": settings.news_language,
"published_at": published_at,
"fetched_at": datetime.now(timezone.utc),
},
index_elements=["dedup_key"]
)
result = session.execute(stmt)
if result.rowcount > 0:
stats["imported"] += 1
# Add to recent titles for this batch
recent_titles.append(canon_title)
else:
stats["duplicates"] += 1
except Exception as e:
logger.warning(f"Error processing article: {e}")
continue
session.commit()
logger.info(
f"News ingestion complete: "
f"{stats['imported']} imported, "
f"{stats['duplicates']} duplicates, "
f"{stats['fuzzy_filtered']} fuzzy filtered, "
f"{stats['language_filtered']} language filtered"
)
return stats
# =============================================================================
# Price Ingestion
# =============================================================================
def fetch_symbol_with_retry(symbol: str, start_date, end_date, max_retries: int = 3, retry_delay: int = 30):
"""
Fetch price data for a symbol with retry on rate limit.
Args:
symbol: Ticker symbol
start_date: Start date
end_date: End date
max_retries: Maximum retry attempts
retry_delay: Seconds to wait between retries
Returns:
DataFrame or None if all retries failed
"""
import time
for attempt in range(max_retries):
try:
ticker = yf.Ticker(symbol)
df = ticker.history(
start=start_date.strftime("%Y-%m-%d"),
end=end_date.strftime("%Y-%m-%d"),
interval="1d"
)
return df
except Exception as e:
error_msg = str(e).lower()
if "rate limit" in error_msg or "too many requests" in error_msg:
if attempt < max_retries - 1:
logger.warning(f"{symbol}: Rate limited, waiting {retry_delay}s before retry {attempt + 2}/{max_retries}")
time.sleep(retry_delay)
else:
logger.error(f"{symbol}: Rate limit exceeded after {max_retries} retries")
raise
else:
raise
return None
def ingest_prices(session: Session) -> dict:
"""
Ingest price data for all configured symbols.
Uses INCREMENTAL fetching: checks latest bar date per symbol in DB
and only fetches from that point forward (plus 3-day overlap for corrections).
Falls back to full lookback if no existing data found for a symbol.
Returns:
Dict with stats per symbol
"""
import time
settings = get_settings()
# Fetch union of dashboard and training symbols (training may have different symbols)
dashboard_symbols = set(settings.symbols_list)
training_symbols = set(settings.training_symbols)
symbols = list(dashboard_symbols | training_symbols)
logger.info(f"Ingesting prices for {len(symbols)} symbols (dashboard={len(dashboard_symbols)}, training={len(training_symbols)})")
stats = {}
# Full lookback range (used only for first-time fetches)
end_date = datetime.now(timezone.utc)
full_start_date = end_date - timedelta(days=settings.lookback_days)
# Overlap buffer: re-fetch last 3 days to catch any corrections/adjustments
OVERLAP_DAYS = 3
for i, symbol in enumerate(symbols):
try:
# Check latest bar in DB for incremental fetch
latest_bar = session.query(PriceBar.date).filter(
PriceBar.symbol == symbol
).order_by(PriceBar.date.desc()).first()
if latest_bar and latest_bar.date:
# Incremental: fetch from (latest - overlap) to now
latest_date = latest_bar.date
if latest_date.tzinfo is None:
latest_date = latest_date.replace(tzinfo=timezone.utc)
start_date = latest_date - timedelta(days=OVERLAP_DAYS)
mode = "incremental"
else:
# First time: full lookback
start_date = full_start_date
mode = "full"
logger.info(f"Fetching prices for {symbol} ({mode})...")
# Fetch with retry mechanism
df = fetch_symbol_with_retry(symbol, start_date, end_date)
if df is None or df.empty:
logger.warning(f"No data returned for {symbol}")
stats[symbol] = {"imported": 0, "updated": 0, "error": "no_data"}
continue
imported = 0
updated = 0
for date_idx, row in df.iterrows():
try:
# Convert index to datetime
if hasattr(date_idx, 'to_pydatetime'):
bar_date = date_idx.to_pydatetime()
else:
bar_date = date_idx
# Ensure timezone
if bar_date.tzinfo is None:
bar_date = bar_date.replace(tzinfo=timezone.utc)
# Upsert
stmt = get_upsert_stmt(
PriceBar,
values={
"symbol": symbol,
"date": bar_date,
"open": float(row.get("Open", 0)) if row.get("Open") else None,
"high": float(row.get("High", 0)) if row.get("High") else None,
"low": float(row.get("Low", 0)) if row.get("Low") else None,
"close": float(row["Close"]),
"volume": float(row.get("Volume", 0)) if row.get("Volume") else None,
"adj_close": float(row.get("Adj Close", row["Close"])),
"fetched_at": datetime.now(timezone.utc),
},
index_elements=["symbol", "date"],
update_set={
"close": float(row["Close"]),
"adj_close": float(row.get("Adj Close", row["Close"])),
"fetched_at": datetime.now(timezone.utc),
}
)
result = session.execute(stmt)
if result.rowcount > 0:
imported += 1
else:
updated += 1
except Exception as e:
logger.debug(f"Error processing price bar: {e}")
continue
session.commit()
stats[symbol] = {"imported": imported, "updated": updated, "mode": mode}
logger.info(f"{symbol}: {imported} bars imported, {updated} unchanged ({mode}, {len(df)} fetched)")
# Add delay between symbols to avoid rate limiting
if i < len(symbols) - 1:
time.sleep(2) # 2 second delay between symbols
except Exception as e:
logger.error(f"Failed to fetch {symbol}: {e}")
stats[symbol] = {"imported": 0, "updated": 0, "error": str(e)}
return stats
# =============================================================================
# Main Entry Point
# =============================================================================
def fetch_all(
news: bool = True,
prices: bool = True
) -> dict:
"""
Run full data ingestion pipeline.
Args:
news: Whether to fetch news
prices: Whether to fetch prices
Returns:
Combined stats dict
"""
logger.info("Starting data ingestion pipeline...")
results = {
"news": None,
"prices": None,
"timestamp": datetime.now(timezone.utc).isoformat(),
}
with SessionLocal() as session:
if news:
results["news"] = ingest_news(session)
if prices:
results["prices"] = ingest_prices(session)
logger.info("Data ingestion complete")
return results
def main():
parser = argparse.ArgumentParser(
description="Fetch news and price data"
)
parser.add_argument(
"--fetch",
action="store_true",
help="Run data fetch"
)
parser.add_argument(
"--news-only",
action="store_true",
help="Fetch only news"
)
parser.add_argument(
"--prices-only",
action="store_true",
help="Fetch only prices"
)
parser.add_argument(
"--no-lock",
action="store_true",
help="Skip pipeline lock (for testing)"
)
parser.add_argument(
"--verbose", "-v",
action="store_true",
help="Verbose logging"
)
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
if not args.fetch:
parser.print_help()
return
# Initialize database
logger.info("Initializing database...")
init_db()
# Determine what to fetch
fetch_news = not args.prices_only
fetch_prices = not args.news_only
# Run with or without lock
if args.no_lock:
results = fetch_all(news=fetch_news, prices=fetch_prices)
else:
try:
with pipeline_lock():
results = fetch_all(news=fetch_news, prices=fetch_prices)
except RuntimeError as e:
logger.error(f"Could not acquire lock: {e}")
logger.info("Another pipeline process may be running. Use --no-lock to bypass.")
return
# Print summary
print("\n" + "=" * 50)
print("DATA INGESTION SUMMARY")
print("=" * 50)
if results.get("news"):
news = results["news"]
print(f"\nNews ({news.get('source', 'unknown')}):")
print(f" - Imported: {news.get('imported', 0)}")
print(f" - Duplicates: {news.get('duplicates', 0)}")
print(f" - Fuzzy filtered: {news.get('fuzzy_filtered', 0)}")
print(f" - Language filtered: {news.get('language_filtered', 0)}")
if results.get("prices"):
print("\nPrices:")
for symbol, stats in results["prices"].items():
status = f"{stats.get('imported', 0)} imported"
if stats.get("error"):
status = f"ERROR: {stats['error']}"
print(f" - {symbol}: {status}")
print(f"\nTimestamp: {results.get('timestamp', 'N/A')}")
if __name__ == "__main__":
main()
|