Skip to content

Commit

Permalink
Added api key to secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
wleong1 committed Apr 8, 2024
1 parent 57425d6 commit 912cd23
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
70 changes: 35 additions & 35 deletions live_price_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pandas as pd
# import psycopg2

from src.parameters import ALPHA_VANTAGE_API_KEY # type: ignore[attr-defined]
# from src.parameters import ALPHA_VANTAGE_API_KEY # type: ignore[attr-defined]

ALPHA_VANTAGE_ENDPOINT = "https://www.alphavantage.co/query"

Expand All @@ -16,43 +16,43 @@ class LivePriceDisplay:
Returns the most recent price of the selected company.
"""

@staticmethod
def display_final_price_av(company_name: str) -> Union[str, dict, Any]:
"""
Returns a the price using Alpha Vantage.
# @staticmethod
# def display_final_price_av(company_name: str) -> Union[str, dict, Any]:
# """
# Returns a the price using Alpha Vantage.

Args:
company_name: The ticker symbol of the company.
# Args:
# company_name: The ticker symbol of the company.

Returns:
The most recent price.
"""
try:
# Gets last available price by default
price_params: dict = {
"apikey": ALPHA_VANTAGE_API_KEY,
"function": "TIME_SERIES_DAILY",
"symbol": company_name,
}
price_response: requests.models.Response = requests.get(
ALPHA_VANTAGE_ENDPOINT, params=price_params, timeout=20
)
if price_response.ok:
response_data: dict = price_response.json()
if "Time Series (Daily)" in response_data:
price_list: dict = response_data["Time Series (Daily)"]
most_recent_day: str = next(iter(price_list))
return price_list[most_recent_day]["4. close"]
return response_data
return price_response
# Returns:
# The most recent price.
# """
# try:
# # Gets last available price by default
# price_params: dict = {
# "apikey": ALPHA_VANTAGE_API_KEY,
# "function": "TIME_SERIES_DAILY",
# "symbol": company_name,
# }
# price_response: requests.models.Response = requests.get(
# ALPHA_VANTAGE_ENDPOINT, params=price_params, timeout=20
# )
# if price_response.ok:
# response_data: dict = price_response.json()
# if "Time Series (Daily)" in response_data:
# price_list: dict = response_data["Time Series (Daily)"]
# most_recent_day: str = next(iter(price_list))
# return price_list[most_recent_day]["4. close"]
# return response_data
# return price_response

except (
requests.exceptions.MissingSchema,
requests.RequestException,
KeyError,
IndexError,
):
return "Error fetching price"
# except (
# requests.exceptions.MissingSchema,
# requests.RequestException,
# KeyError,
# IndexError,
# ):
# return "Error fetching price"

@staticmethod
def display_final_price_yf(company_name: str) -> Union[float, str]:
Expand Down
4 changes: 2 additions & 2 deletions news_display.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""This module displays the most recent news of the selected company if available"""

import requests
import streamlit as st

from src.parameters import NEWS_API_KEY # type: ignore[attr-defined]

NEWS_ENDPOINT = "https://newsapi.org/v2/everything"

Expand All @@ -22,7 +22,7 @@ def _collect_news(company_name: str) -> list:
Returns:
five_article: The most recent five articles
"""
news_params: dict = {"apiKey": NEWS_API_KEY, "qInTitle": company_name}
news_params: dict = {"apiKey": st.secrets.connections.newsapi["apiKey"], "qInTitle": company_name}

news_response: requests.models.Response = requests.get(
NEWS_ENDPOINT, params=news_params, timeout=20
Expand Down

0 comments on commit 912cd23

Please sign in to comment.