From f9b8921ba8883d089dd64cc9668278c8c7f90bda Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Fri, 21 May 2021 16:50:29 -0700 Subject: [PATCH] Close #66 --- IEX_Symbol.py | 47 ++++++++++++++++++++++++++++++++++++----------- cg_Crypto.py | 35 ++++++++++++++++++++++++----------- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/IEX_Symbol.py b/IEX_Symbol.py index 4acf549..1faa913 100644 --- a/IEX_Symbol.py +++ b/IEX_Symbol.py @@ -55,10 +55,12 @@ class IEX_Symbol: ) -> Optional[Tuple[pd.DataFrame, datetime]]: reg_symbols = r.get( - f"https://cloud.iexapis.com/stable/ref-data/symbols?token={self.IEX_TOKEN}" + f"https://cloud.iexapis.com/stable/ref-data/symbols?token={self.IEX_TOKEN}", + timeout=5, ).json() otc_symbols = r.get( - f"https://cloud.iexapis.com/stable/ref-data/otc/symbols?token={self.IEX_TOKEN}" + f"https://cloud.iexapis.com/stable/ref-data/otc/symbols?token={self.IEX_TOKEN}", + timeout=5, ).json() reg = pd.DataFrame(data=reg_symbols) @@ -83,7 +85,10 @@ class IEX_Symbol: str Human readable text on status of IEX API """ - resp = r.get("https://pjmps0c34hp7.statuspage.io/api/v2/status.json") + resp = r.get( + "https://pjmps0c34hp7.statuspage.io/api/v2/status.json", + timeout=5, + ) if resp.status_code == 200: status = resp.json()["status"] @@ -155,7 +160,10 @@ class IEX_Symbol: IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol.id}/quote?token={self.IEX_TOKEN}" - response = r.get(IEXurl) + response = r.get( + IEXurl, + timeout=5, + ) if response.status_code == 200: IEXData = response.json() @@ -226,7 +234,10 @@ class IEX_Symbol: return "OTC stocks do not currently support any commands." IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/dividends/next?token={self.IEX_TOKEN}" - response = r.get(IEXurl) + response = r.get( + IEXurl, + timeout=5, + ) if response.status_code == 200 and response.json(): IEXData = response.json()[0] keys = ( @@ -288,7 +299,10 @@ class IEX_Symbol: return "OTC stocks do not currently support any commands." IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/news/last/15?token={self.IEX_TOKEN}" - response = r.get(IEXurl) + response = r.get( + IEXurl, + timeout=5, + ) if response.status_code == 200: data = response.json() if data: @@ -324,7 +338,10 @@ class IEX_Symbol: return "OTC stocks do not currently support any commands." IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/company?token={self.IEX_TOKEN}" - response = r.get(IEXurl) + response = r.get( + IEXurl, + timeout=5, + ) if response.status_code == 200: data = response.json() @@ -352,7 +369,10 @@ class IEX_Symbol: return "OTC stocks do not currently support any commands." IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/stats?token={self.IEX_TOKEN}" - response = r.get(IEXurl) + response = r.get( + IEXurl, + timeout=5, + ) if response.status_code == 200: data = response.json() @@ -399,7 +419,10 @@ class IEX_Symbol: return pd.DataFrame() IEXurl = f"https://cloud.iexapis.com/stable/stock/{symbol}/intraday-prices?token={self.IEX_TOKEN}" - response = r.get(IEXurl) + response = r.get( + IEXurl, + timeout=5, + ) if response.status_code == 200: df = pd.DataFrame(response.json()) df.dropna(inplace=True, subset=["date", "minute", "high", "low", "volume"]) @@ -437,7 +460,8 @@ class IEX_Symbol: pass response = r.get( - f"https://cloud.iexapis.com/stable/stock/{symbol}/chart/1mm?token={self.IEX_TOKEN}&chartInterval=3&includeToday=false" + f"https://cloud.iexapis.com/stable/stock/{symbol}/chart/1mm?token={self.IEX_TOKEN}&chartInterval=3&includeToday=false", + timeout=5, ) if response.status_code == 200: @@ -460,7 +484,8 @@ class IEX_Symbol: """ stocks = r.get( - f"https://cloud.iexapis.com/stable/stock/market/list/mostactive?token={self.IEX_TOKEN}" + f"https://cloud.iexapis.com/stable/stock/market/list/mostactive?token={self.IEX_TOKEN}", + timeout=5, ).json() return [f"${s['symbol']}: {s['companyName']}" for s in stocks] diff --git a/cg_Crypto.py b/cg_Crypto.py index e1f9ad8..1b6d4c6 100644 --- a/cg_Crypto.py +++ b/cg_Crypto.py @@ -44,7 +44,10 @@ class cg_Crypto: self, return_df=False ) -> Optional[Tuple[pd.DataFrame, datetime]]: - raw_symbols = r.get("https://api.coingecko.com/api/v3/coins/list").json() + raw_symbols = r.get( + "https://api.coingecko.com/api/v3/coins/list", + timeout=5, + ).json() symbols = pd.DataFrame(data=raw_symbols) symbols["description"] = "$$" + symbols["symbol"] + ": " + symbols["name"] @@ -62,7 +65,10 @@ class cg_Crypto: str Human readable text on status of CoinGecko API """ - status = r.get("https://api.coingecko.com/api/v3/ping") + status = r.get( + "https://api.coingecko.com/api/v3/ping", + timeout=5, + ) if status.status_code == 200: return f"CoinGecko API responded that it was OK in {status.elapsed.total_seconds()} Seconds." @@ -124,7 +130,8 @@ class cg_Crypto: """ response = r.get( - f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false" + f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false", + timeout=5, ) if response.status_code == 200: data = response.json() @@ -167,7 +174,8 @@ class cg_Crypto: Returns a timeseries dataframe with high, low, and volume data if its available. Otherwise returns empty pd.DataFrame. """ response = r.get( - f"https://api.coingecko.com/api/v3/coins/{symbol.id}/ohlc?vs_currency=usd&days=1" + f"https://api.coingecko.com/api/v3/coins/{symbol.id}/ohlc?vs_currency=usd&days=1", + timeout=5, ) if response.status_code == 200: df = pd.DataFrame( @@ -194,7 +202,8 @@ class cg_Crypto: Returns a timeseries dataframe with high, low, and volume data if its available. Otherwise returns empty pd.DataFrame. """ response = r.get( - f"https://api.coingecko.com/api/v3/coins/{symbol.id}/ohlc?vs_currency=usd&days=30" + f"https://api.coingecko.com/api/v3/coins/{symbol.id}/ohlc?vs_currency=usd&days=30", + timeout=5, ) if response.status_code == 200: @@ -221,7 +230,8 @@ class cg_Crypto: Each symbol passed in is a key with its value being a human readable formatted string of the symbols statistics. """ response = r.get( - f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false" + f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false", + timeout=5, ) if response.status_code == 200: data = response.json() @@ -253,7 +263,8 @@ class cg_Crypto: """ response = r.get( - f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false" + f"https://api.coingecko.com/api/v3/coins/{symbol.id}?localization=false", + timeout=5, ) if response.status_code == 200: data = response.json() @@ -273,9 +284,10 @@ class cg_Crypto: list of $$ID: NAME """ - coins = r.get("https://api.coingecko.com/api/v3/search/trending").json()[ - "coins" - ] + coins = r.get( + "https://api.coingecko.com/api/v3/search/trending", + timeout=5, + ).json()["coins"] return [f"$${c['item']['symbol'].upper()}: {c['item']['name']}" for c in coins] @@ -283,7 +295,8 @@ class cg_Crypto: query = ",".join([c.id for c in coins]) prices = r.get( - f"https://api.coingecko.com/api/v3/simple/price?ids={query}&vs_currencies=usd&include_24hr_change=true" + f"https://api.coingecko.com/api/v3/simple/price?ids={query}&vs_currencies=usd&include_24hr_change=true", + timeout=5, ).json() replies = []