-
Notifications
You must be signed in to change notification settings - Fork 1
/
EDGAR_api.py
98 lines (77 loc) · 2.68 KB
/
EDGAR_api.py
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
# Get links from SEC api calls and then scrape teh links for relevant data
from sec_api import QueryApi
import requests
import prompts
import os
# API endpoint
url = "https://api.sec-api.io/extractor"
# Access the value of the environment variable
api_key=""
with open('EDGAR_KEY.txt', 'r') as file:
api_key = file.read()
# Assign the value to the 'auth' variable
# NOTE: if running locally better to just paste the api key variable
auth = api_key
ten_k_items = prompts.ten_k_items
ten_q_items = prompts.ten_q_items
eight_k_items = prompts.eight_k_items
def get_item_number(filing_type, item_name):
item_name = item_name
filing_type = filing_type
item_dict ={}
if filing_type == '10-K':
item_dict = ten_k_items
elif filing_type == '10-Q':
item_dict = ten_q_items
elif filing_type == '8-K':
item_dict = eight_k_items
else:
return 'Invalid filing type'
print(f"Item name {item_name}")
print(f"filing type {filing_type}")
print(item_dict.get(item_name))
if any(char.isdigit() for char in item_name): # if the item name is already a alphanum id
return item_name
return item_dict.get(item_name, 'Invalid item type')
def get_financial_filings_urls(name,auth):
queryApi = QueryApi(api_key=auth)
filings_dict = {}
# List of form types to query
form_types = ["8-K", "10-Q", "10-K"]
for form_type in form_types:
query = {
"query": {
"query_string": {
"query": f"(formType:\"{form_type}\") AND companyName:{name}",
}
},
"from": "0",
"size": "1", # Only fetch the latest document of the type
"sort": [{"filedAt": {"order": "desc"}}]
}
response = queryApi.get_filings(query)
if response["filings"]:
filing = response["filings"][0]
url = filing["linkToTxt"]
filings_dict[form_type] = url
return filings_dict
#get company info with query api, return dictionary with newest link for all forms which will be passed to retrieve section
def retrieveSection(url, itemType, auth) -> str:
# Request parameters
params = {
"url": url,
"item": itemType,
"type": "text",
"token": auth
}
# Send request
response = requests.get("https://api.sec-api.io/extractor", params=params)
data = response.text
return data
def run_sec_API(companyName, Form, Section):
# Parameters above are what PALM returns
urls = get_financial_filings_urls(companyName, auth)
print(urls)
data = retrieveSection(urls[Form],get_item_number(Form, Section), auth)
print(data)
return data