Created
August 15, 2021 02:19
-
-
Save McKlayne/8c02125ef89fb938abf2ddbd7ecd8cd1 to your computer and use it in GitHub Desktop.
top_mover_database_lambda.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import pandas as pd | |
from datetime import datetime | |
import pytz | |
#!/usr/bin/env python | |
try: | |
# For Python 3.0 and later | |
from urllib.request import urlopen | |
except ImportError: | |
# Fall back to Python 2's urllib2 | |
from urllib2 import urlopen | |
import json | |
def get_jsonparsed_data(url): | |
""" | |
Receive the content of ``url``, parse it as JSON and return the object. | |
Parameters | |
---------- | |
url : str | |
Returns | |
------- | |
dict | |
""" | |
response = urlopen(url) | |
data = response.read().decode("utf-8") | |
return json.loads(data) | |
def lambda_handler(event, context): | |
top_movers_path = 's3://data/top_mover_and_loser/top_movers.csv' | |
#FMP API Call | |
url = ("https://financialmodelingprep.com/api/v3/gainers?apikey={YOUR_FMP_API_KEY}") | |
#Call the get_jsonparsed_data function from above and create a dataframe | |
top_movers = pd.DataFrame(get_jsonparsed_data(url)) | |
#change the percentages to a float | |
top_movers.changesPercentage = top_movers.changes.astype(float)/(top_movers.price.astype(float)-top_movers.changes.astype(float)) | |
#Add time to the dataframe | |
tz = pytz.timezone('US/Central') | |
top_movers['Date'] = datetime.now(tz).strftime("%m/%d/%Y %H:%M:%S") | |
#try, is to open the csv if it already has data in it and is in the cloud | |
try: | |
tmovers = pd.read_csv(top_movers_path) | |
#append new data to CSV | |
tmovers.append(top_movers).to_csv(top_movers_path, index=False) | |
#except, this would trigger if a csv is not in cloud already -- so, first time running the lambda it will add it with the data | |
except: | |
top_movers.to_csv(top_movers_path,index=False) | |
return "Added 30 more top movers to the top mover database" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment