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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""News Data Integration - Real-Time Sentiment Pipeline
|
| 2 |
+
|
| 3 |
+
Connects to real news APIs and RSS feeds for live sentiment signals.
|
| 4 |
+
Replaces synthetic news with actual financial headlines.
|
| 5 |
+
|
| 6 |
+
Supports:
|
| 7 |
+
- NewsAPI (https://newsapi.org/) - Free tier available
|
| 8 |
+
- RSS feeds (Yahoo Finance, Seeking Alpha, etc.)
|
| 9 |
+
- GDELT Project (global news database)
|
| 10 |
+
- Reddit/StockTwits social feeds
|
| 11 |
+
"""
|
| 12 |
+
import numpy as np
|
| 13 |
+
import pandas as pd
|
| 14 |
+
from datetime import datetime, timedelta
|
| 15 |
+
from typing import Dict, List, Optional, Tuple
|
| 16 |
+
import time
|
| 17 |
+
import warnings
|
| 18 |
+
warnings.filterwarnings('ignore')
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
import feedparser
|
| 22 |
+
FEEDPARSER_AVAILABLE = True
|
| 23 |
+
except ImportError:
|
| 24 |
+
FEEDPARSER_AVAILABLE = False
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class NewsAPIClient:
|
| 28 |
+
"""
|
| 29 |
+
NewsAPI.org client for financial news retrieval.
|
| 30 |
+
|
| 31 |
+
Free tier: 100 requests/day
|
| 32 |
+
Paid tier: $449/month for 1M requests
|
| 33 |
+
|
| 34 |
+
Use free tier for prototyping, upgrade for production.
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
def __init__(self, api_key: Optional[str] = None):
|
| 38 |
+
self.api_key = api_key
|
| 39 |
+
self.base_url = "https://newsapi.org/v2"
|
| 40 |
+
self.last_request_time = 0
|
| 41 |
+
self.min_interval = 1.2 # Free tier: ~80 requests/minute max
|
| 42 |
+
|
| 43 |
+
def _rate_limit(self):
|
| 44 |
+
"""Enforce rate limiting"""
|
| 45 |
+
elapsed = time.time() - self.last_request_time
|
| 46 |
+
if elapsed < self.min_interval:
|
| 47 |
+
time.sleep(self.min_interval - elapsed)
|
| 48 |
+
self.last_request_time = time.time()
|
| 49 |
+
|
| 50 |
+
def fetch_everything(self,
|
| 51 |
+
query: str,
|
| 52 |
+
from_date: Optional[str] = None,
|
| 53 |
+
to_date: Optional[str] = None,
|
| 54 |
+
language: str = 'en',
|
| 55 |
+
sort_by: str = 'publishedAt',
|
| 56 |
+
page_size: int = 100,
|
| 57 |
+
page: int = 1) -> List[Dict]:
|
| 58 |
+
"""
|
| 59 |
+
Fetch news articles matching query.
|
| 60 |
+
|
| 61 |
+
Args:
|
| 62 |
+
query: Search query (e.g., "AAPL Apple stock earnings")
|
| 63 |
+
from_date: Start date (YYYY-MM-DD)
|
| 64 |
+
to_date: End date (YYYY-MM-DD)
|
| 65 |
+
language: 'en', 'es', 'fr', etc.
|
| 66 |
+
sort_by: 'relevancy', 'popularity', 'publishedAt'
|
| 67 |
+
page_size: Max 100
|
| 68 |
+
page: Page number
|
| 69 |
+
"""
|
| 70 |
+
if self.api_key is None:
|
| 71 |
+
print("WARNING: No API key provided. Using mock data.")
|
| 72 |
+
return self._mock_news(query)
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
import requests
|
| 76 |
+
except ImportError:
|
| 77 |
+
print("WARNING: requests library not available. Using mock data.")
|
| 78 |
+
return self._mock_news(query)
|
| 79 |
+
|
| 80 |
+
self._rate_limit()
|
| 81 |
+
|
| 82 |
+
params = {
|
| 83 |
+
'q': query,
|
| 84 |
+
'apiKey': self.api_key,
|
| 85 |
+
'language': language,
|
| 86 |
+
'sortBy': sort_by,
|
| 87 |
+
'pageSize': min(page_size, 100),
|
| 88 |
+
'page': page
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
if from_date:
|
| 92 |
+
params['from'] = from_date
|
| 93 |
+
if to_date:
|
| 94 |
+
params['to'] = to_date
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
response = requests.get(
|
| 98 |
+
f"{self.base_url}/everything",
|
| 99 |
+
params=params,
|
| 100 |
+
timeout=30
|
| 101 |
+
)
|
| 102 |
+
response.raise_for_status()
|
| 103 |
+
data = response.json()
|
| 104 |
+
|
| 105 |
+
if data.get('status') != 'ok':
|
| 106 |
+
print(f"API Error: {data.get('message', 'Unknown error')}")
|
| 107 |
+
return self._mock_news(query)
|
| 108 |
+
|
| 109 |
+
articles = data.get('articles', [])
|
| 110 |
+
|
| 111 |
+
return [{
|
| 112 |
+
'title': a.get('title', ''),
|
| 113 |
+
'description': a.get('description', ''),
|
| 114 |
+
'content': a.get('content', ''),
|
| 115 |
+
'published_at': a.get('publishedAt', ''),
|
| 116 |
+
'source': a.get('source', {}).get('name', 'Unknown'),
|
| 117 |
+
'url': a.get('url', ''),
|
| 118 |
+
'author': a.get('author', '')
|
| 119 |
+
} for a in articles]
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Error fetching news: {e}")
|
| 123 |
+
return self._mock_news(query)
|
| 124 |
+
|
| 125 |
+
def fetch_for_ticker(self,
|
| 126 |
+
ticker: str,
|
| 127 |
+
company_name: str,
|
| 128 |
+
from_date: Optional[str] = None,
|
| 129 |
+
to_date: Optional[str] = None,
|
| 130 |
+
page_size: int = 100) -> pd.DataFrame:
|
| 131 |
+
"""
|
| 132 |
+
Fetch news for a specific ticker and return formatted DataFrame.
|
| 133 |
+
"""
|
| 134 |
+
query = f"{ticker} {company_name} stock"
|
| 135 |
+
articles = self.fetch_everything(
|
| 136 |
+
query=query,
|
| 137 |
+
from_date=from_date,
|
| 138 |
+
to_date=to_date,
|
| 139 |
+
page_size=page_size
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
df = pd.DataFrame(articles)
|
| 143 |
+
df['ticker'] = ticker
|
| 144 |
+
df['query'] = query
|
| 145 |
+
|
| 146 |
+
# Combine title and content for analysis
|
| 147 |
+
df['text'] = df['title'].fillna('') + '. ' + df['description'].fillna('') + ' ' + df['content'].fillna('')
|
| 148 |
+
df['text'] = df['text'].str.strip()
|
| 149 |
+
|
| 150 |
+
# Parse dates
|
| 151 |
+
df['date'] = pd.to_datetime(df['published_at'], errors='coerce')
|
| 152 |
+
|
| 153 |
+
return df
|
| 154 |
+
|
| 155 |
+
def fetch_multiple_tickers(self,
|
| 156 |
+
ticker_map: Dict[str, str],
|
| 157 |
+
from_date: Optional[str] = None,
|
| 158 |
+
to_date: Optional[str] = None) -> pd.DataFrame:
|
| 159 |
+
"""
|
| 160 |
+
Fetch news for multiple tickers.
|
| 161 |
+
|
| 162 |
+
Args:
|
| 163 |
+
ticker_map: {ticker: company_name}
|
| 164 |
+
"""
|
| 165 |
+
all_news = []
|
| 166 |
+
|
| 167 |
+
for ticker, company in ticker_map.items():
|
| 168 |
+
print(f"Fetching news for {ticker} ({company})...")
|
| 169 |
+
try:
|
| 170 |
+
df = self.fetch_for_ticker(
|
| 171 |
+
ticker, company, from_date, to_date
|
| 172 |
+
)
|
| 173 |
+
all_news.append(df)
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f" Error for {ticker}: {e}")
|
| 176 |
+
|
| 177 |
+
if all_news:
|
| 178 |
+
return pd.concat(all_news, ignore_index=True)
|
| 179 |
+
return pd.DataFrame()
|
| 180 |
+
|
| 181 |
+
def _mock_news(self, query: str) -> List[Dict]:
|
| 182 |
+
"""Generate mock news for testing without API key"""
|
| 183 |
+
import random
|
| 184 |
+
|
| 185 |
+
templates = [
|
| 186 |
+
{"title": "{company} reports strong quarterly earnings, beating expectations",
|
| 187 |
+
"sentiment": "positive"},
|
| 188 |
+
{"title": "{company} faces regulatory scrutiny over data practices",
|
| 189 |
+
"sentiment": "negative"},
|
| 190 |
+
{"title": "Analysts upgrade {company} to buy rating",
|
| 191 |
+
"sentiment": "positive"},
|
| 192 |
+
{"title": "{company} announces major product launch",
|
| 193 |
+
"sentiment": "positive"},
|
| 194 |
+
{"title": "{company} stock falls amid market volatility",
|
| 195 |
+
"sentiment": "negative"},
|
| 196 |
+
{"title": "Market awaits {company} earnings report next week",
|
| 197 |
+
"sentiment": "neutral"},
|
| 198 |
+
]
|
| 199 |
+
|
| 200 |
+
company = query.split()[0] if query else "Company"
|
| 201 |
+
|
| 202 |
+
articles = []
|
| 203 |
+
for i, template in enumerate(random.sample(templates, min(3, len(templates)))):
|
| 204 |
+
articles.append({
|
| 205 |
+
'title': template['title'].format(company=company),
|
| 206 |
+
'description': f"Analysis of {company} stock performance.",
|
| 207 |
+
'content': f"Detailed article about {company} and market conditions.",
|
| 208 |
+
'published_at': (datetime.now() - timedelta(hours=i*6)).isoformat(),
|
| 209 |
+
'source': f'MockSource{i}',
|
| 210 |
+
'url': f'https://example.com/article{i}',
|
| 211 |
+
'author': 'MockAuthor'
|
| 212 |
+
})
|
| 213 |
+
|
| 214 |
+
return articles
|
| 215 |
+
|
| 216 |
+
|
| 217 |
+
class RSSFeedClient:
|
| 218 |
+
"""
|
| 219 |
+
RSS Feed client for real-time financial news.
|
| 220 |
+
|
| 221 |
+
No API key needed! Just RSS feeds from financial websites.
|
| 222 |
+
"""
|
| 223 |
+
|
| 224 |
+
FINANCIAL_FEEDS = {
|
| 225 |
+
'yahoo_finance': 'https://finance.yahoo.com/news/rssindex',
|
| 226 |
+
'marketwatch': 'https://www.marketwatch.com/rss/topstories',
|
| 227 |
+
'seeking_alpha': 'https://seekingalpha.com/market_currents.xml',
|
| 228 |
+
'investing_com': 'https://www.investing.com/rss/news.rss',
|
| 229 |
+
'barrons': 'https://www.barrons.com/articles/rss',
|
| 230 |
+
'wall_street_journal': 'https://feeds.a.dj.com/rss/WSJcomUSBusiness.xml',
|
| 231 |
+
'reuters_business': 'https://www.reutersagency.com/feed/?taxonomy=markets&post_type=reuters-best',
|
| 232 |
+
'benzinga': 'https://www.benzinga.com/feed',
|
| 233 |
+
'the_street': 'https://www.thestreet.com/.rss/full/',
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
def __init__(self):
|
| 237 |
+
self.feeds = {}
|
| 238 |
+
|
| 239 |
+
def fetch_feed(self, feed_url: str, max_entries: int = 50) -> List[Dict]:
|
| 240 |
+
"""Fetch and parse an RSS feed"""
|
| 241 |
+
if not FEEDPARSER_AVAILABLE:
|
| 242 |
+
print("WARNING: feedparser not available. Install with: pip install feedparser")
|
| 243 |
+
return []
|
| 244 |
+
|
| 245 |
+
try:
|
| 246 |
+
feed = feedparser.parse(feed_url)
|
| 247 |
+
|
| 248 |
+
articles = []
|
| 249 |
+
for entry in feed.entries[:max_entries]:
|
| 250 |
+
articles.append({
|
| 251 |
+
'title': entry.get('title', ''),
|
| 252 |
+
'description': entry.get('summary', entry.get('description', '')),
|
| 253 |
+
'content': entry.get('summary', ''),
|
| 254 |
+
'published_at': entry.get('published', entry.get('updated', '')),
|
| 255 |
+
'source': feed.feed.get('title', 'Unknown'),
|
| 256 |
+
'url': entry.get('link', ''),
|
| 257 |
+
'author': entry.get('author', '')
|
| 258 |
+
})
|
| 259 |
+
|
| 260 |
+
return articles
|
| 261 |
+
|
| 262 |
+
except Exception as e:
|
| 263 |
+
print(f"Error fetching RSS feed {feed_url}: {e}")
|
| 264 |
+
return []
|
| 265 |
+
|
| 266 |
+
def fetch_all_feeds(self, max_entries_per_feed: int = 20) -> pd.DataFrame:
|
| 267 |
+
"""Fetch all configured financial feeds"""
|
| 268 |
+
all_articles = []
|
| 269 |
+
|
| 270 |
+
for name, url in self.FINANCIAL_FEEDS.items():
|
| 271 |
+
print(f"Fetching {name}...")
|
| 272 |
+
articles = self.fetch_feed(url, max_entries_per_feed)
|
| 273 |
+
for a in articles:
|
| 274 |
+
a['feed_source'] = name
|
| 275 |
+
all_articles.extend(articles)
|
| 276 |
+
|
| 277 |
+
if not all_articles:
|
| 278 |
+
return pd.DataFrame()
|
| 279 |
+
|
| 280 |
+
df = pd.DataFrame(all_articles)
|
| 281 |
+
df['text'] = df['title'].fillna('') + '. ' + df['description'].fillna('')
|
| 282 |
+
df['date'] = pd.to_datetime(df['published_at'], errors='coerce')
|
| 283 |
+
|
| 284 |
+
return df
|
| 285 |
+
|
| 286 |
+
def add_custom_feed(self, name: str, url: str):
|
| 287 |
+
"""Add a custom RSS feed"""
|
| 288 |
+
self.FINANCIAL_FEEDS[name] = url
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
class GDELTClient:
|
| 292 |
+
"""
|
| 293 |
+
GDELT Project (Global Database of Events, Language, and Tone) client.
|
| 294 |
+
|
| 295 |
+
Free, massive global news database.
|
| 296 |
+
https://www.gdeltproject.org/
|
| 297 |
+
|
| 298 |
+
GDELT provides:
|
| 299 |
+
- Every news article worldwide (updated every 15 minutes)
|
| 300 |
+
- Sentiment scoring (tone)
|
| 301 |
+
- Event coding
|
| 302 |
+
- Geographic tagging
|
| 303 |
+
"""
|
| 304 |
+
|
| 305 |
+
GDELT_URL = "http://data.gdeltproject.org/gdeltv2/lastupdate.txt"
|
| 306 |
+
|
| 307 |
+
def __init__(self):
|
| 308 |
+
pass
|
| 309 |
+
|
| 310 |
+
def fetch_latest_updates(self) -> pd.DataFrame:
|
| 311 |
+
"""Fetch latest GDELT update URLs"""
|
| 312 |
+
try:
|
| 313 |
+
import requests
|
| 314 |
+
response = requests.get(self.GDELT_URL, timeout=30)
|
| 315 |
+
response.raise_for_status()
|
| 316 |
+
|
| 317 |
+
lines = response.text.strip().split('\n')
|
| 318 |
+
|
| 319 |
+
updates = []
|
| 320 |
+
for line in lines:
|
| 321 |
+
parts = line.split()
|
| 322 |
+
if len(parts) >= 3:
|
| 323 |
+
updates.append({
|
| 324 |
+
'timestamp': parts[0],
|
| 325 |
+
'url': parts[2],
|
| 326 |
+
'type': 'events' if 'export' in parts[2] else 'mentions' if 'mentions' in parts[2] else 'gkg'
|
| 327 |
+
})
|
| 328 |
+
|
| 329 |
+
return pd.DataFrame(updates)
|
| 330 |
+
|
| 331 |
+
except Exception as e:
|
| 332 |
+
print(f"Error fetching GDELT updates: {e}")
|
| 333 |
+
return pd.DataFrame()
|
| 334 |
+
|
| 335 |
+
def fetch_gdelt_csv(self, url: str) -> pd.DataFrame:
|
| 336 |
+
"""Fetch and parse a GDELT CSV file"""
|
| 337 |
+
try:
|
| 338 |
+
import requests
|
| 339 |
+
import zipfile
|
| 340 |
+
import io
|
| 341 |
+
|
| 342 |
+
response = requests.get(url, timeout=60)
|
| 343 |
+
response.raise_for_status()
|
| 344 |
+
|
| 345 |
+
# GDELT files are ZIP archives
|
| 346 |
+
with zipfile.ZipFile(io.BytesIO(response.content)) as z:
|
| 347 |
+
csv_name = z.namelist()[0]
|
| 348 |
+
with z.open(csv_name) as f:
|
| 349 |
+
df = pd.read_csv(f, sep='\t', header=None, low_memory=False)
|
| 350 |
+
|
| 351 |
+
# Add column names based on type
|
| 352 |
+
if 'export' in url:
|
| 353 |
+
# CAMEO event data
|
| 354 |
+
columns = ['GlobalEventID', 'Day', 'MonthYear', 'Year', 'FractionDate',
|
| 355 |
+
'Actor1Code', 'Actor1Name', 'Actor1CountryCode', 'Actor1KnownGroupCode',
|
| 356 |
+
'Actor1EthnicCode', 'Actor1Religion1Code', 'Actor1Religion2Code',
|
| 357 |
+
'Actor1Type1Code', 'Actor1Type2Code', 'Actor1Type3Code',
|
| 358 |
+
'Actor2Code', 'Actor2Name', 'Actor2CountryCode', 'Actor2KnownGroupCode',
|
| 359 |
+
'Actor2EthnicCode', 'Actor2Religion1Code', 'Actor2Religion2Code',
|
| 360 |
+
'Actor2Type1Code', 'Actor2Type2Code', 'Actor2Type3Code',
|
| 361 |
+
'IsRootEvent', 'EventCode', 'EventBaseCode', 'EventRootCode',
|
| 362 |
+
'QuadClass', 'GoldsteinScale', 'NumMentions', 'NumSources',
|
| 363 |
+
'NumArticles', 'AvgTone', 'Actor1Geo_Type', 'Actor1Geo_FullName',
|
| 364 |
+
'Actor1Geo_CountryCode', 'Actor1Geo_ADM1Code', 'Actor1Geo_Lat',
|
| 365 |
+
'Actor1Geo_Long', 'Actor1Geo_FeatureID', 'Actor2Geo_Type',
|
| 366 |
+
'Actor2Geo_FullName', 'Actor2Geo_CountryCode', 'Actor2Geo_ADM1Code',
|
| 367 |
+
'Actor2Geo_Lat', 'Actor2Geo_Long', 'Actor2Geo_FeatureID',
|
| 368 |
+
'ActionGeo_Type', 'ActionGeo_FullName', 'ActionGeo_CountryCode',
|
| 369 |
+
'ActionGeo_ADM1Code', 'ActionGeo_Lat', 'ActionGeo_Long',
|
| 370 |
+
'ActionGeo_FeatureID', 'DATEADDED', 'SOURCEURL']
|
| 371 |
+
df.columns = columns[:len(df.columns)]
|
| 372 |
+
|
| 373 |
+
return df
|
| 374 |
+
|
| 375 |
+
except Exception as e:
|
| 376 |
+
print(f"Error fetching GDELT data: {e}")
|
| 377 |
+
return pd.DataFrame()
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
class SocialMediaScraper:
|
| 381 |
+
"""
|
| 382 |
+
Social media sentiment scraper (Reddit, StockTwits, Twitter/X).
|
| 383 |
+
|
| 384 |
+
Note: Twitter API now requires paid access ($100/month basic tier).
|
| 385 |
+
Reddit API has rate limits but free tier available.
|
| 386 |
+
StockTwits has free API for basic usage.
|
| 387 |
+
"""
|
| 388 |
+
|
| 389 |
+
REDDIT_SUBREDDITS = [
|
| 390 |
+
'wallstreetbets', 'stocks', 'investing', 'StockMarket',
|
| 391 |
+
'options', 'pennystocks', 'SecurityAnalysis', 'algotrading'
|
| 392 |
+
]
|
| 393 |
+
|
| 394 |
+
def __init__(self):
|
| 395 |
+
pass
|
| 396 |
+
|
| 397 |
+
def fetch_reddit_posts(self,
|
| 398 |
+
subreddit: str,
|
| 399 |
+
limit: int = 100,
|
| 400 |
+
time_filter: str = 'day') -> pd.DataFrame:
|
| 401 |
+
"""
|
| 402 |
+
Fetch Reddit posts from a subreddit.
|
| 403 |
+
|
| 404 |
+
Requires: pip install praw
|
| 405 |
+
You need Reddit API credentials (free at reddit.com/prefs/apps)
|
| 406 |
+
"""
|
| 407 |
+
try:
|
| 408 |
+
import praw
|
| 409 |
+
except ImportError:
|
| 410 |
+
print("WARNING: praw not available. Install with: pip install praw")
|
| 411 |
+
return pd.DataFrame()
|
| 412 |
+
|
| 413 |
+
# Note: User must provide their own credentials
|
| 414 |
+
# This is a placeholder showing the pattern
|
| 415 |
+
print("REDDIT INTEGRATION PATTERN:")
|
| 416 |
+
print(" 1. Create app at https://www.reddit.com/prefs/apps")
|
| 417 |
+
print(" 2. Get client_id and client_secret")
|
| 418 |
+
print(" 3. Initialize: praw.Reddit(client_id='...', client_secret='...', user_agent='...')")
|
| 419 |
+
print(" 4. Fetch: reddit.subreddit('wallstreetbets').hot(limit=100)")
|
| 420 |
+
|
| 421 |
+
return pd.DataFrame()
|
| 422 |
+
|
| 423 |
+
def fetch_stocktwits_feed(self,
|
| 424 |
+
ticker: str,
|
| 425 |
+
limit: int = 30) -> pd.DataFrame:
|
| 426 |
+
"""
|
| 427 |
+
Fetch StockTwits messages for a ticker.
|
| 428 |
+
|
| 429 |
+
StockTwits API: https://api.stocktwits.com/developers/docs
|
| 430 |
+
Free tier available for basic usage.
|
| 431 |
+
"""
|
| 432 |
+
try:
|
| 433 |
+
import requests
|
| 434 |
+
except ImportError:
|
| 435 |
+
print("WARNING: requests not available")
|
| 436 |
+
return pd.DataFrame()
|
| 437 |
+
|
| 438 |
+
url = f"https://api.stocktwits.com/api/2/streams/symbol/{ticker}.json"
|
| 439 |
+
|
| 440 |
+
try:
|
| 441 |
+
response = requests.get(url, timeout=30)
|
| 442 |
+
response.raise_for_status()
|
| 443 |
+
data = response.json()
|
| 444 |
+
|
| 445 |
+
messages = data.get('messages', [])
|
| 446 |
+
|
| 447 |
+
return pd.DataFrame([{
|
| 448 |
+
'text': m.get('body', ''),
|
| 449 |
+
'created_at': m.get('created_at', ''),
|
| 450 |
+
'username': m.get('user', {}).get('username', ''),
|
| 451 |
+
'sentiment': m.get('entities', {}).get('sentiment', {}).get('basic', 'neutral'),
|
| 452 |
+
'likes': m.get('likes', {}).get('total', 0),
|
| 453 |
+
'ticker': ticker
|
| 454 |
+
} for m in messages])
|
| 455 |
+
|
| 456 |
+
except Exception as e:
|
| 457 |
+
print(f"Error fetching StockTwits: {e}")
|
| 458 |
+
return pd.DataFrame()
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
class NewsPipeline:
|
| 462 |
+
"""
|
| 463 |
+
Complete news pipeline: fetch -> preprocess -> sentiment -> aggregate.
|
| 464 |
+
|
| 465 |
+
Connects NewsAPI + RSS feeds + Social media into one unified feed.
|
| 466 |
+
"""
|
| 467 |
+
|
| 468 |
+
def __init__(self,
|
| 469 |
+
news_api_key: Optional[str] = None,
|
| 470 |
+
use_rss: bool = True,
|
| 471 |
+
use_gdelt: bool = False,
|
| 472 |
+
use_social: bool = False):
|
| 473 |
+
self.news_api = NewsAPIClient(news_api_key)
|
| 474 |
+
self.rss_client = RSSFeedClient()
|
| 475 |
+
self.gdelt_client = GDELTClient()
|
| 476 |
+
self.social_scraper = SocialMediaScraper()
|
| 477 |
+
|
| 478 |
+
self.use_rss = use_rss
|
| 479 |
+
self.use_gdelt = use_gdelt
|
| 480 |
+
self.use_social = use_social
|
| 481 |
+
|
| 482 |
+
def fetch_all(self,
|
| 483 |
+
tickers: List[str],
|
| 484 |
+
company_names: Optional[Dict[str, str]] = None,
|
| 485 |
+
from_date: Optional[str] = None,
|
| 486 |
+
to_date: Optional[str] = None) -> pd.DataFrame:
|
| 487 |
+
"""
|
| 488 |
+
Fetch news from ALL sources for given tickers.
|
| 489 |
+
|
| 490 |
+
Returns unified DataFrame with all articles.
|
| 491 |
+
"""
|
| 492 |
+
all_news = []
|
| 493 |
+
|
| 494 |
+
# NewsAPI
|
| 495 |
+
if company_names:
|
| 496 |
+
ticker_map = {t: company_names.get(t, t) for t in tickers}
|
| 497 |
+
else:
|
| 498 |
+
ticker_map = {t: t for t in tickers}
|
| 499 |
+
|
| 500 |
+
print("[NewsAPI] Fetching financial news...")
|
| 501 |
+
try:
|
| 502 |
+
news_api_df = self.news_api.fetch_multiple_tickers(
|
| 503 |
+
ticker_map, from_date, to_date
|
| 504 |
+
)
|
| 505 |
+
if not news_api_df.empty:
|
| 506 |
+
news_api_df['source_type'] = 'newsapi'
|
| 507 |
+
all_news.append(news_api_df)
|
| 508 |
+
except Exception as e:
|
| 509 |
+
print(f" NewsAPI error: {e}")
|
| 510 |
+
|
| 511 |
+
# RSS Feeds
|
| 512 |
+
if self.use_rss:
|
| 513 |
+
print("[RSS] Fetching financial feeds...")
|
| 514 |
+
try:
|
| 515 |
+
rss_df = self.rss_client.fetch_all_feeds(max_entries_per_feed=10)
|
| 516 |
+
if not rss_df.empty:
|
| 517 |
+
rss_df['source_type'] = 'rss'
|
| 518 |
+
# Tag with tickers using simple keyword matching
|
| 519 |
+
rss_df['ticker'] = rss_df['text'].apply(
|
| 520 |
+
lambda x: self._extract_tickers(str(x), tickers)
|
| 521 |
+
)
|
| 522 |
+
rss_df = rss_df[rss_df['ticker'].notna()]
|
| 523 |
+
if not rss_df.empty:
|
| 524 |
+
all_news.append(rss_df)
|
| 525 |
+
except Exception as e:
|
| 526 |
+
print(f" RSS error: {e}")
|
| 527 |
+
|
| 528 |
+
# Combine
|
| 529 |
+
if all_news:
|
| 530 |
+
combined = pd.concat(all_news, ignore_index=True)
|
| 531 |
+
combined['text'] = combined['text'].fillna('')
|
| 532 |
+
combined['date'] = pd.to_datetime(combined['date'], errors='coerce')
|
| 533 |
+
return combined.sort_values('date', ascending=False)
|
| 534 |
+
|
| 535 |
+
return pd.DataFrame()
|
| 536 |
+
|
| 537 |
+
def _extract_tickers(self, text: str, tickers: List[str]) -> Optional[str]:
|
| 538 |
+
"""Simple keyword matching to tag articles with tickers"""
|
| 539 |
+
text_upper = text.upper()
|
| 540 |
+
for ticker in tickers:
|
| 541 |
+
if f' {ticker} ' in text_upper or f'${ticker}' in text_upper:
|
| 542 |
+
return ticker
|
| 543 |
+
return None
|
| 544 |
+
|
| 545 |
+
def aggregate_daily_sentiment(self,
|
| 546 |
+
news_df: pd.DataFrame,
|
| 547 |
+
sentiment_fn: Optional[Callable] = None) -> pd.DataFrame:
|
| 548 |
+
"""
|
| 549 |
+
Aggregate news into daily sentiment scores per ticker.
|
| 550 |
+
|
| 551 |
+
Requires sentiment_fn that takes text -> dict with 'sentiment_score'.
|
| 552 |
+
If not provided, returns raw counts only.
|
| 553 |
+
"""
|
| 554 |
+
if news_df.empty:
|
| 555 |
+
return pd.DataFrame()
|
| 556 |
+
|
| 557 |
+
# Ensure date is datetime
|
| 558 |
+
news_df['date'] = pd.to_datetime(news_df['date'], errors='coerce')
|
| 559 |
+
news_df['date'] = news_df['date'].dt.date
|
| 560 |
+
|
| 561 |
+
if sentiment_fn is not None:
|
| 562 |
+
print("Computing sentiment scores...")
|
| 563 |
+
sentiments = []
|
| 564 |
+
for text in news_df['text']:
|
| 565 |
+
try:
|
| 566 |
+
result = sentiment_fn(str(text))
|
| 567 |
+
sentiments.append(result.get('sentiment_score', 0))
|
| 568 |
+
except:
|
| 569 |
+
sentiments.append(0)
|
| 570 |
+
news_df['sentiment_score'] = sentiments
|
| 571 |
+
else:
|
| 572 |
+
news_df['sentiment_score'] = 0
|
| 573 |
+
|
| 574 |
+
# Aggregate by date and ticker
|
| 575 |
+
daily = news_df.groupby(['date', 'ticker']).agg({
|
| 576 |
+
'sentiment_score': ['mean', 'std', 'count'],
|
| 577 |
+
'text': 'first'
|
| 578 |
+
}).reset_index()
|
| 579 |
+
|
| 580 |
+
# Flatten multi-index columns
|
| 581 |
+
daily.columns = ['date', 'ticker', 'sentiment_mean', 'sentiment_std',
|
| 582 |
+
'article_count', 'sample_text']
|
| 583 |
+
|
| 584 |
+
# Confidence weighting: more articles = more confident
|
| 585 |
+
daily['confidence'] = np.minimum(daily['article_count'] / 5, 1.0)
|
| 586 |
+
daily['sentiment_alpha'] = daily['sentiment_mean'] * daily['confidence']
|
| 587 |
+
|
| 588 |
+
return daily
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
if __name__ == '__main__':
|
| 592 |
+
# Test news pipeline
|
| 593 |
+
pipeline = NewsPipeline()
|
| 594 |
+
|
| 595 |
+
# Fetch mock news (no API key)
|
| 596 |
+
news = pipeline.news_api.fetch_for_ticker('AAPL', 'Apple', page_size=5)
|
| 597 |
+
print(f"Fetched {len(news)} articles for AAPL")
|
| 598 |
+
print(news[['title', 'source', 'date']].head())
|