-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
74 lines (60 loc) · 2.01 KB
/
main.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
import json
import os
from datetime import datetime, timedelta
from typing import List, Any
import backoff
import boto3
import pytz
import requests
time_zone = pytz.timezone('Asia/Kolkata')
date_time_obj = datetime.now(time_zone)
DATE = (date_time_obj.today() + timedelta(days=-1)).date() # Get previous day
API_KEY = os.getenv("API_KEY")
def get_tickers() -> List:
with open("tickers.txt") as file:
data = file.readlines()
return data
def check_status(r):
try:
if r.status_code == 429:
return True
else:
return False
except AttributeError:
return False
def upload_file_to_s3(filepath, final_date, filename):
s3 = boto3.resource('s3')
bucket = os.getenv("S3_BUCKET")
print(f"Uploading file {filename} to S3...")
s3.Bucket(bucket).upload_file(filepath, f"{final_date}/{filename}")
@backoff.on_predicate(
backoff.runtime,
predicate=lambda r: check_status(r),
value=lambda r: 300,
jitter=None,
)
def process_ticker() -> Any:
tickers_list = get_tickers()
for ticker in tickers_list:
ticker = ticker.replace("\n", "")
file_name = f"{str(ticker).lower()}.json"
file_path = os.path.abspath(file_name)
file_exists = os.path.exists(file_path)
if file_exists is False:
base_url = f"https://api.polygon.io/v1/open-close/{ticker}/{DATE}?adjusted=true&apiKey={API_KEY}"
data = requests.get(base_url)
print(f"TICKER => {ticker} | STATUS => {data.status_code}")
if data.status_code == 429:
return data
else:
with open(file_name, "w") as file_obj:
file_obj.write(json.dumps(data.json()))
file_obj.close()
upload_file_to_s3(file_path, DATE, file_name)
def delete_files():
for item in os.listdir():
if item.endswith(".json"):
os.remove(os.path.join("", item))
if __name__ == '__main__':
process_ticker()
delete_files()