Skip to content

Commit dffbf53

Browse files
author
connorsanders
committed
Hardened data.py.
1 parent 86173f3 commit dffbf53

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

test/test_yahoofinancials.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def setUp(self):
4343
self.test_yf_currencies = yf(currencies)
4444
self.test_yf_concurrent = yf(stocks, concurrent=True)
4545
self.test_yf_stock_flat = yf('C', flat_format=True)
46+
self.test_yf_stock_analytic = yf('WFC')
4647

4748
# Fundamentals Test
4849
def test_yf_fundamentals(self):
@@ -120,15 +121,15 @@ def test_yf_fundamentals_flat(self):
120121
def test_yf_analytic_methods(self):
121122

122123
# Get Insights
123-
out = self.test_yf_stock_single.get_insights()
124-
if out.get("C").get("instrumentInfo").get("technicalEvents").get("sector") == "Financial Services":
124+
out = self.test_yf_stock_analytic.get_insights()
125+
if out.get("WFC").get("instrumentInfo").get("technicalEvents").get("sector") == "Financial Services":
125126
self.assertEqual(True, True)
126127
else:
127128
self.assertEqual(False, True)
128129

129130
# Get Recommendations
130-
out = self.test_yf_stock_single.get_recommendations()
131-
if isinstance(out.get("C"), list):
131+
out = self.test_yf_stock_analytic.get_recommendations()
132+
if isinstance(out.get("WFC"), list):
132133
self.assertEqual(True, True)
133134
else:
134135
self.assertEqual(False, True)

yahoofinancials/data.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,17 @@ def _create_dict_ent(self, up_ticker, statement_type, tech_type, report_name, hi
560560
dict_ent = {up_ticker: re_data}
561561
return dict_ent
562562

563+
def _retry_create_dict_ent(self, up_ticker, statement_type, tech_type, report_name, hist_obj):
564+
i = 0
565+
while i < 250:
566+
try:
567+
out = self._create_dict_ent(up_ticker, statement_type, tech_type, report_name, hist_obj)
568+
return out
569+
except:
570+
time.sleep(random.randint(2, 10))
571+
i += 1
572+
continue
573+
563574
# Private method to return the stmt_id for the reformat_process
564575
def _get_stmt_id(self, statement_type, raw_data):
565576
stmt_id = ''
@@ -611,17 +622,17 @@ def get_time_code(self, time_interval):
611622
# Public Method to get stock data
612623
def get_stock_data(self, statement_type='income', tech_type='', report_name='', hist_obj={}):
613624
data = {}
614-
if statement_type == 'income' and tech_type == '' and report_name == '': # temp, so this method doesn't return nulls
625+
if statement_type == 'income' and tech_type == '' and report_name == '': # temp, so this method doesn't return nulls
615626
statement_type = 'profile'
616627
tech_type = 'assetProfile'
617628
report_name = 'assetProfile'
618629
if isinstance(self.ticker, str):
619-
dict_ent = self._create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj)
630+
dict_ent = self._retry_create_dict_ent(self.ticker, statement_type, tech_type, report_name, hist_obj)
620631
data.update(dict_ent)
621632
else:
622633
if self.concurrent:
623634
with Pool(self._get_worker_count()) as pool:
624-
dict_ents = pool.map(partial(self._create_dict_ent,
635+
dict_ents = pool.map(partial(self._retry_create_dict_ent,
625636
statement_type=statement_type,
626637
tech_type=tech_type,
627638
report_name=report_name,

0 commit comments

Comments
 (0)