import requests import json from datetime import datetime class SECUtils: def __init__(self): self.cik_lookup_url = "https://www.sec.gov/files/company_tickers.json" self.edgar_search_url = "https://data.sec.gov/submissions/CIK{cik}.json" self.headers = { "User-Agent": "StockResearchMVP/1.0 (contact@example.com)" } def get_cik(self, ticker): """Get CIK number for a ticker symbol""" try: response = requests.get(self.cik_lookup_url, headers=self.headers, timeout=10) response.raise_for_status() data = response.json() for k, v in data.items(): if v.get('ticker', '').upper() == ticker.upper(): return str(v['cik_str']).zfill(10) return None except Exception as e: return None def get_recent_filings(self, ticker): """Get recent SEC filings for a ticker""" try: cik = self.get_cik(ticker) if not cik: return f"❌ CIK not found for ticker: {ticker}" url = self.edgar_search_url.format(cik=cik) response = requests.get(url, headers=self.headers, timeout=10) response.raise_for_status() data = response.json() filings = data.get('filings', {}).get('recent', {}) if not filings: return f"❌ No filings found for {ticker}" result = f"📄 **SEC Filings for {ticker}**\n\n" # Get last 5 filings for i in range(min(5, len(filings.get('form', [])))): form = filings['form'][i] filing_date = filings['filingDate'][i] accession_num = filings['accessionNumber'][i].replace('-', '') filing_url = f"https://www.sec.gov/Archives/edgar/data/{int(cik)}/{accession_num}/" result += f"• **{form}** - {filing_date}\n" result += f" 📎 [View Filing]({filing_url})\n\n" return result except Exception as e: return f"❌ Error fetching SEC filings: {str(e)}" def get_company_info(self, ticker): """Get basic company information""" try: cik = self.get_cik(ticker) if not cik: return f"❌ Company info not found for ticker: {ticker}" url = self.edgar_search_url.format(cik=cik) response = requests.get(url, headers=self.headers, timeout=10) response.raise_for_status() data = response.json() name = data.get('name', 'N/A') sic = data.get('sic', 'N/A') sic_description = data.get('sicDescription', 'N/A') result = f"🏢 **Company Information for {ticker}**\n\n" result += f"• **Name**: {name}\n" result += f"• **CIK**: {cik}\n" result += f"• **SIC Code**: {sic}\n" result += f"• **Industry**: {sic_description}\n\n" return result except Exception as e: return f"❌ Error fetching company info: {str(e)}"