Skip to content

Commit

Permalink
updated utilities files
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkhamLee committed Feb 22, 2024
1 parent 0c932bf commit 896773a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
45 changes: 45 additions & 0 deletions etl_pipelines/alpha_vantage_library/alpha_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
# productivity-music-stocks-weather-IoT-dashboard
# https://github.com/MarkhamLee/productivity-music-stocks-weather-IoT-dashboard
# Utility scripts for retrieving data from the Alpha Advantage finance API
import os
import sys
import pandas as pd

parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(parent_dir)

from etl_library.logging_util import logger # noqa: E402
from etl_library.general_utilities import EtlUtilities # noqa: E402


class AlphaUtilities():
Expand All @@ -12,6 +21,7 @@ def __init__(self):
self.base_tbill_url = 'https://www.alphavantage.co/query?function=TREASURY_YIELD&interval=daily' # noqa: E501
self.bond_base = '&maturity='
self.api_key_base = '&apikey='
self.utilities = EtlUtilities()

def build_bond_url(self, maturity: str, key: str) -> str:

Expand All @@ -33,3 +43,38 @@ def bond_data_parser(response: dict) -> dict:
}

return payload

# this one will retrieve n number of bond data entries
def bond_data_parser_entries(self, data: dict, count: int):

try:

# split off just the treasury bill data
subsection = data['data']
logger.info('Subsection action completed')

# convert json to pandas data frame and keep the first N rows
rates = pd.DataFrame(subsection).head(count)
logger.info('Successfully converted JSON to pandas dataframe')

# rename columns
rates.rename(columns={"value": "rate"}, inplace=True)

# filter out only the rows with a number for rate
rates = rates[pd.to_numeric(rates['rate'],
errors='coerce').notnull()]
logger.info('Filtered out rows with valid rates')

# filter out only the rows with a valid date
rates = rates[pd.to_datetime(rates['date'],
errors='coerce').notnull()]

logger.info(f'data parsing complete, returning {len(rates)} rows of data') # noqa: E501

return rates

except Exception as e:
WEBHOOK_URL = os.environ['ALERT_WEBHOOK']
message = (f'ETL pipeline failure Alpha Vantage multi-day bond prices: {e}') # noqa: E501
logger.debug(message)
self.utilities.send_slack_webhook(WEBHOOK_URL, message)
2 changes: 1 addition & 1 deletion etl_pipelines/finnhub/finnhub_utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) Markham Lee
# (C) Markham Lee 2023 - 2024
# https://github.com/MarkhamLee/productivity-music-stocks-weather-IoT-dashboard
# Utilities for the Finnhub Finance API. Unnecessary at this point because
# the finnhub python library makes things simple, but splitting out some
Expand Down
5 changes: 5 additions & 0 deletions etl_pipelines/finnhub/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# (C) Markham Lee 2023 - 2024
# https://github.com/MarkhamLee/productivity-music-stocks-weather-IoT-dashboard
# Retrieves current stock price for a given security and then writes the
# data to InfluxDB.

import os
import sys
from finnhub_utilities import FinnHubUtilities
Expand Down

0 comments on commit 896773a

Please sign in to comment.