File size: 3,317 Bytes
904a3df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)}"