No1r97 commited on
Commit
3f898f8
1 Parent(s): b067069

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -69,9 +69,8 @@ def n_weeks_before(date_string, n):
69
 
70
  def get_stock_data(stock_symbol, steps):
71
 
72
- try:
73
- stock_data = yf.download(stock_symbol, steps[0], steps[-1])
74
- except Exception:
75
  raise gr.Error(f"Failed to download stock price data for symbol {stock_symbol} from yfinance!")
76
 
77
  # print(stock_data)
@@ -104,10 +103,9 @@ def get_news(symbol, data):
104
  end_date = row['End Date'].strftime('%Y-%m-%d')
105
  # print(symbol, ': ', start_date, ' - ', end_date)
106
  time.sleep(1) # control qpm
107
- try:
108
- weekly_news = finnhub_client.company_news(symbol, _from=start_date, to=end_date)
109
- except Exception:
110
- raise gr.Error(f"Failed to download company news for symbol {stock_symbol} from finnhub!")
111
  weekly_news = [
112
  {
113
  "date": datetime.fromtimestamp(n['datetime']).strftime('%Y%m%d%H%M%S'),
@@ -125,10 +123,9 @@ def get_news(symbol, data):
125
 
126
  def get_company_prompt(symbol):
127
 
128
- try:
129
- profile = finnhub_client.company_profile2(symbol=symbol)
130
- except Exception:
131
- raise gr.Error(f"Failed to download company profile for symbol {stock_symbol} from finnhub!")
132
 
133
  company_template = "[Company Introduction]:\n\n{name} is a leading entity in the {finnhubIndustry} sector. Incorporated and publicly traded since {ipo}, the company has established its reputation as one of the key players in the market. As of today, {name} has a market capitalization of {marketCapitalization:.2f} in {currency}, with {shareOutstanding:.2f} shares outstanding." \
134
  "\n\n{name} operates primarily in the {country}, trading under the ticker {ticker} on the {exchange}. As a dominant force in the {finnhubIndustry} space, the company continues to innovate and drive progress within the industry."
@@ -168,10 +165,9 @@ def sample_news(news, k=5):
168
 
169
  def get_current_basics(symbol, curday):
170
 
171
- try:
172
- basic_financials = finnhub_client.company_basic_financials(symbol, 'all')
173
- except Exception:
174
- raise gr.Error(f"Failed to download basic financials for symbol {stock_symbol} from finnhub!")
175
 
176
  final_basics, basic_list, basic_dict = [], [], defaultdict(dict)
177
 
 
69
 
70
  def get_stock_data(stock_symbol, steps):
71
 
72
+ stock_data = yf.download(stock_symbol, steps[0], steps[-1])
73
+ if len(stock_data) == 0:
 
74
  raise gr.Error(f"Failed to download stock price data for symbol {stock_symbol} from yfinance!")
75
 
76
  # print(stock_data)
 
103
  end_date = row['End Date'].strftime('%Y-%m-%d')
104
  # print(symbol, ': ', start_date, ' - ', end_date)
105
  time.sleep(1) # control qpm
106
+ weekly_news = finnhub_client.company_news(symbol, _from=start_date, to=end_date)
107
+ if len(weekly_news) == 0:
108
+ raise gr.Error(f"No company news found for symbol {stock_symbol} from finnhub!")
 
109
  weekly_news = [
110
  {
111
  "date": datetime.fromtimestamp(n['datetime']).strftime('%Y%m%d%H%M%S'),
 
123
 
124
  def get_company_prompt(symbol):
125
 
126
+ profile = finnhub_client.company_profile2(symbol=symbol)
127
+ if not profile:
128
+ raise gr.Error(f"Failed to find company profile for symbol {stock_symbol} from finnhub!")
 
129
 
130
  company_template = "[Company Introduction]:\n\n{name} is a leading entity in the {finnhubIndustry} sector. Incorporated and publicly traded since {ipo}, the company has established its reputation as one of the key players in the market. As of today, {name} has a market capitalization of {marketCapitalization:.2f} in {currency}, with {shareOutstanding:.2f} shares outstanding." \
131
  "\n\n{name} operates primarily in the {country}, trading under the ticker {ticker} on the {exchange}. As a dominant force in the {finnhubIndustry} space, the company continues to innovate and drive progress within the industry."
 
165
 
166
  def get_current_basics(symbol, curday):
167
 
168
+ basic_financials = finnhub_client.company_basic_financials(symbol, 'all')
169
+ if not basic_financials['series']:
170
+ raise gr.Error(f"Failed to find basic financials for symbol {stock_symbol} from finnhub!")
 
171
 
172
  final_basics, basic_list, basic_dict = [], [], defaultdict(dict)
173