-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.py
57 lines (45 loc) · 1.65 KB
/
request.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
import json
import os
import pathlib
import time
from typing import List, Optional
import requests
from tqdm import tqdm
path = os.path.dirname(os.path.abspath(__file__))
_STOCK_LIST_URL = 'https://financialmodelingprep.com/api/v3/company/stock/list'
_API_URL_TEMPLATE = 'https://financialmodelingprep.com/api/v3/' \
'historical-price-full/{}?serietype=line'
def _fetch(url: str, **kwargs: dict) -> Optional[requests.Response]:
response = requests.get(url, **kwargs)
if response.ok:
return response
raise RuntimeError(f'[{response.status_code}] for [{url}]')
def _get_stock_list() -> List[str]:
stock_list_response = _fetch(_STOCK_LIST_URL)
symbols_json = stock_list_response.json().get('symbolsList', None)
symbols_list = []
if not symbols_json:
return symbols_list
symbols_list = list(
filter(None, (entry.get('symbol', None) for entry in symbols_json))
)
return sorted(symbols_list)
def run():
data_dir = os.path.join(path, 'data')
pathlib.Path(data_dir).mkdir(parents=True, exist_ok=True)
stock_list = _get_stock_list()
print('Downloading data')
for i in tqdm(range(0, len(stock_list), 3)):
symbols = stock_list[i:i+3]
url = _API_URL_TEMPLATE.format(','.join(symbols))
file_name = '_'.join(symbols) + '.json'
file_path = os.path.join(data_dir, file_name)
if os.path.exists(file_path):
continue
print(f'Fetching {url}')
response = _fetch(url)
with open(file_path, 'w') as file:
json.dump(response.json(), file, indent=4)
time.sleep(0.25)
if __name__ == '__main__':
run()