From ed449abc15d7312894077cf50c19e34546597b2a Mon Sep 17 00:00:00 2001 From: Colin Delahunty <72827203+colin99d@users.noreply.github.com> Date: Fri, 7 Oct 2022 13:42:59 +0200 Subject: [PATCH] Added error handling (#2721) * Added error handling * Added error handling * Added fixes * Added Jeroen fixes --- .../financedatabase_model.py | 149 ++++++++++-------- openbb_terminal/stocks/stocks_helper.py | 11 +- 2 files changed, 91 insertions(+), 69 deletions(-) diff --git a/openbb_terminal/stocks/sector_industry_analysis/financedatabase_model.py b/openbb_terminal/stocks/sector_industry_analysis/financedatabase_model.py index 7173288df08..6b1f2b2cf67 100644 --- a/openbb_terminal/stocks/sector_industry_analysis/financedatabase_model.py +++ b/openbb_terminal/stocks/sector_industry_analysis/financedatabase_model.py @@ -7,14 +7,16 @@ import logging import financedatabase as fd import yfinance as yf from tqdm import tqdm +from requests.exceptions import ReadTimeout from openbb_terminal.decorators import log_start_end +from openbb_terminal.rich_config import console logger = logging.getLogger(__name__) @log_start_end(log=logger) -def get_countries(industry: str = "", sector: str = ""): +def get_countries(industry: str = "", sector: str = "") -> list: """Get all countries in Yahoo Finance data based on sector or industry. [Source: Finance Database] Parameters @@ -39,7 +41,7 @@ def get_countries(industry: str = "", sector: str = ""): @log_start_end(log=logger) -def get_sectors(industry: str = "", country: str = ""): +def get_sectors(industry: str = "", country: str = "") -> list: """Get all sectors in Yahoo Finance data based on country or industry. [Source: Finance Database] Parameters @@ -64,7 +66,7 @@ def get_sectors(industry: str = "", country: str = ""): @log_start_end(log=logger) -def get_industries(country: str = "", sector: str = ""): +def get_industries(country: str = "", sector: str = "") -> list: """Get all industries in Yahoo Finance data based on country or sector. [Source: Finance Database] Parameters @@ -92,7 +94,7 @@ def get_industries(country: str = "", sector: str = ""): @log_start_end(log=logger) -def get_marketcap(): +def get_marketcap() -> list: """Get all market cap division in Yahoo Finance data. [Source: Finance Database] Returns @@ -100,7 +102,7 @@ def get_marketcap(): list List of possible market caps """ - return list(["Small Cap", "Mid Cap", "Large Cap"]) + return ["Small Cap", "Mid Cap", "Large Cap"] @log_start_end(log=logger) @@ -110,8 +112,9 @@ def filter_stocks( industry: str = None, marketcap: str = "", exclude_exchanges: bool = True, -): - """Filter stocks based on country, sector, industry, market cap and exclude exchanges. [Source: Finance Database] +) -> list: + """Filter stocks based on country, sector, industry, market cap and exclude exchanges. + [Source: Finance Database] Parameters ---------- @@ -131,58 +134,15 @@ def filter_stocks( list List of filtered stocks """ + kwargs = {} + if country: + kwargs["country"] = country + if sector: + kwargs["sector"] = sector + if industry: + kwargs["industry"] = industry try: - if country: - if sector: - if industry: - data = fd.select_equities( - country=country, - sector=sector, - industry=industry, - exclude_exchanges=exclude_exchanges, - ) - else: # no industry - data = fd.select_equities( - country=country, - sector=sector, - exclude_exchanges=exclude_exchanges, - ) - else: # no sector - if industry: - data = fd.select_equities( - country=country, - industry=industry, - exclude_exchanges=exclude_exchanges, - ) - else: # no industry - data = fd.select_equities( - country=country, - exclude_exchanges=exclude_exchanges, - ) - - else: # no country - if sector: - if industry: - data = fd.select_equities( - sector=sector, - industry=industry, - exclude_exchanges=exclude_exchanges, - ) - else: # no industry - data = fd.select_equities( - sector=sector, - exclude_exchanges=exclude_exchanges, - ) - else: # no sector - if industry: - data = fd.select_equities( - industry=industry, - exclude_exchanges=exclude_exchanges, - ) - else: # no industry - data = fd.select_equities( - exclude_exchanges=exclude_exchanges, - ) + data = fd.select_equities(exclude_exchanges=exclude_exchanges, **kwargs) if marketcap: data = fd.search_products(data, query=marketcap, search="market_cap") @@ -191,7 +151,15 @@ def filter_stocks( except ValueError as e: logger.exception(str(e)) - return list() + return [] + + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + return [] @log_start_end(log=logger) @@ -202,7 +170,8 @@ def get_stocks_data( marketcap: str = "Mega Cap", exclude_exchanges: bool = True, ) -> dict: - """Get stocks data based on country, sector, industry, market cap and exclude exchanges. [Source: Finance Database] + """Get stocks data based on country, sector, industry, market cap and exclude exchanges. + [Source: Finance Database] Parameters ---------- @@ -237,7 +206,7 @@ def get_companies_per_sector_in_country( country: str = "United States", mktcap: str = "Large", exclude_exchanges: bool = True, -): +) -> dict: """Get number of companies per sector in a specific country (and specific market cap). [Source: Finance Database] Parameters @@ -271,6 +240,14 @@ def get_companies_per_sector_in_country( except ValueError as e: logger.exception(str(e)) + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + return {} + return companies_per_sector @@ -280,7 +257,8 @@ def get_companies_per_industry_in_country( mktcap: str = "Large", exclude_exchanges: bool = True, ): - """Get number of companies per industry in a specific country (and specific market cap). [Source: Finance Database] + """Get number of companies per industry in a specific country (and specific market cap). + [Source: Finance Database] Parameters ---------- @@ -316,14 +294,23 @@ def get_companies_per_industry_in_country( except ValueError as e: logger.exception(str(e)) + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + return {} + return companies_per_industry @log_start_end(log=logger) def get_companies_per_industry_in_sector( sector: str = "Technology", mktcap: str = "Large", exclude_exchanges: bool = True -): - """Get number of companies per industry in a specific sector (and specific market cap). [Source: Finance Database] +) -> dict: + """Get number of companies per industry in a specific sector (and specific market cap). + [Source: Finance Database] Parameters ---------- @@ -358,14 +345,23 @@ def get_companies_per_industry_in_sector( except ValueError as e: logger.exception(str(e)) + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + return {} + return companies_per_industry @log_start_end(log=logger) def get_companies_per_country_in_sector( sector: str = "Technology", mktcap: str = "Large", exclude_exchanges: bool = True -): - """Get number of companies per country in a specific sector (and specific market cap). [Source: Finance Database] +) -> dict: + """Get number of companies per country in a specific sector (and specific market cap). + [Source: Finance Database] Parameters ---------- @@ -400,6 +396,14 @@ def get_companies_per_country_in_sector( except ValueError as e: logger.exception(str(e)) + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + return {} + return companies_per_country @@ -408,8 +412,9 @@ def get_companies_per_country_in_industry( industry: str = "Internet Content & Information", mktcap: str = "Large", exclude_exchanges: bool = True, -): - """Get number of companies per country in a specific industry (and specific market cap). [Source: Finance Database] +) -> dict: + """Get number of companies per country in a specific industry (and specific market cap). + [Source: Finance Database] Parameters ---------- @@ -444,4 +449,12 @@ def get_companies_per_country_in_industry( except ValueError as e: logger.exception(str(e)) + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + return {} + return companies_per_country diff --git a/openbb_terminal/stocks/stocks_helper.py b/openbb_terminal/stocks/stocks_helper.py index f6fc44a32ef..57adb0f979e 100644 --- a/openbb_terminal/stocks/stocks_helper.py +++ b/openbb_terminal/stocks/stocks_helper.py @@ -19,6 +19,7 @@ import pandas as pd import plotly.graph_objects as go import pytz import requests +from requests.exceptions import ReadTimeout import yfinance as yf from numpy.core.fromnumeric import transpose @@ -95,7 +96,15 @@ def search( if industry: kwargs["industry"] = industry - data = fd.select_equities(**kwargs) + try: + data = fd.select_equities(**kwargs) + except ReadTimeout: + console.print( + "[red]Unable to retrieve company data from GitHub which limits the search" + " capabilities. This tends to be due to access restrictions for GitHub.com," + " please check if you can access this website without a VPN.[/red]\n" + ) + data = {} if not data: console.print("No companies found.\n") return