|
1 | 1 | import random |
2 | 2 | import logging |
3 | 3 | import isthisstockgood.RuleOneInvestingCalculations as RuleOne |
| 4 | +from requests.adapters import HTTPAdapter, Retry |
| 5 | +from requests.exceptions import RetryError |
4 | 6 | from requests_futures.sessions import FuturesSession |
5 | 7 | from isthisstockgood.Active.MSNMoney import MSNMoney |
6 | 8 | from isthisstockgood.Active.YahooFinance import YahooFinanceAnalysis |
|
10 | 12 | logger = logging.getLogger("IsThisStockGood") |
11 | 13 |
|
12 | 14 |
|
13 | | -def fetchDataForTickerSymbol(ticker): |
| 15 | +def fetchDataForTickerSymbol(ticker, growth_estimate=None): |
14 | 16 | """Fetches and parses all of the financial data for the `ticker`. |
15 | 17 |
|
16 | 18 | Args: |
@@ -48,16 +50,22 @@ def fetchDataForTickerSymbol(ticker): |
48 | 50 |
|
49 | 51 | # Wait for each RPC result before proceeding. |
50 | 52 | for rpc in data_fetcher.rpcs: |
51 | | - rpc.result() |
| 53 | + try: |
| 54 | + rpc.result() |
| 55 | + except RetryError as e: |
| 56 | + continue |
52 | 57 |
|
53 | 58 | msn_money = data_fetcher.msn_money |
54 | 59 | yahoo_finance_analysis = data_fetcher.yahoo_finance_analysis |
55 | 60 | zacks_analysis = data_fetcher.zacks_analysis |
56 | 61 | # NOTE: Some stocks won't have analyst growth rates, such as newly listed stocks or some foreign stocks. |
57 | | - five_year_growth_rate = \ |
58 | | - yahoo_finance_analysis.five_year_growth_rate if yahoo_finance_analysis \ |
59 | | - else zacks_analysis.five_year_growth_rate if zacks_analysis \ |
60 | | - else 0 |
| 62 | + if (yahoo_finance_analysis |
| 63 | + and yahoo_finance_analysis.five_year_growth_rate is not None): |
| 64 | + five_year_growth_rate = yahoo_finance_analysis.five_year_growth_rate |
| 65 | + elif (zacks_analysis and zacks_analysis.five_year_growth_rate is not None): |
| 66 | + five_year_growth_rate = zacks_analysis.five_year_growth_rate |
| 67 | + else: |
| 68 | + five_year_growth_rate = growth_estimate |
61 | 69 | margin_of_safety_price, sticker_price = _calculateMarginOfSafetyPrice( |
62 | 70 | msn_money.equity_growth_rates[-1], |
63 | 71 | msn_money.pe_low, |
@@ -143,6 +151,16 @@ def _create_session(self): |
143 | 151 | session.headers.update({ |
144 | 152 | 'User-Agent' : random.choice(DataFetcher.USER_AGENT_LIST) |
145 | 153 | }) |
| 154 | + |
| 155 | + retries = Retry( |
| 156 | + total=3, |
| 157 | + backoff_factor=0.1, |
| 158 | + status_forcelist=[301] |
| 159 | + ) |
| 160 | + adapter = HTTPAdapter(max_retries=retries) |
| 161 | + session.mount('http://', adapter) |
| 162 | + session.mount('https://', adapter) |
| 163 | + |
146 | 164 | return session |
147 | 165 |
|
148 | 166 | def fetch_msn_money_data(self): |
|
0 commit comments