From 40347ea41a23790225e1c4fc3f1b1a3720fe7893 Mon Sep 17 00:00:00 2001 From: Danglewood <85772166+deeleeramone@users.noreply.github.com> Date: Thu, 23 May 2024 10:55:20 -0700 Subject: [PATCH] prevent key error --- .../models/forward_eps_estimates.py | 50 +++++++++---------- .../models/forward_sales_estimates.py | 24 ++++----- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_eps_estimates.py b/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_eps_estimates.py index 9e52c312120a..58c7f306ca53 100644 --- a/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_eps_estimates.py +++ b/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_eps_estimates.py @@ -150,45 +150,41 @@ def transform_data( if period_type == "quarterly" else "FY" ) - eps_estimates["number_of_analysts"] = seek_object[ - "eps_normalized_num_of_estimates" - ][str(i)][0].get("dataitemvalue") + num_estimates = seek_object["revenue_num_of_estimates"].get(str(i)) + if not num_estimates: + continue + eps_estimates["number_of_analysts"] = num_estimates[0].get( + "dataitemvalue" + ) actual = seek_object["eps_normalized_actual"].get(str(i)) - gaap_actual = seek_object["eps_gaap_actual"].get(str(i)) if actual: eps_estimates["normalized_actual"] = actual[0].get("dataitemvalue") + gaap_actual = seek_object["eps_gaap_actual"].get(str(i)) if gaap_actual: eps_estimates["gaap_actual"] = gaap_actual[0].get("dataitemvalue") - low = seek_object["eps_normalized_consensus_low"][str(i)][0].get( - "dataitemvalue" - ) + low = seek_object["eps_normalized_consensus_low"].get(str(i)) if low: - eps_estimates["low_estimate"] = low - gaap_low = seek_object["eps_gaap_consensus_low"][str(i)][0].get( - "dataitemvalue" - ) + eps_estimates["low_estimate"] = low[0].get("dataitemvalue") + gaap_low = seek_object["eps_gaap_consensus_low"].get(str(i)) if gaap_low: - eps_estimates["low_estimate_gaap"] = gaap_low - high = seek_object["eps_normalized_consensus_high"][str(i)][0].get( - "dataitemvalue" - ) + eps_estimates["low_estimate_gaap"] = gaap_low[0].get( + "dataitemvalue" + ) + high = seek_object["eps_normalized_consensus_high"].get(str(i)) if high: - eps_estimates["high_estimate"] = high - gaap_high = seek_object["eps_gaap_consensus_high"][str(i)][0].get( - "dataitemvalue" - ) + eps_estimates["high_estimate"] = high[0].get("dataitemvalue") + gaap_high = seek_object["eps_gaap_consensus_high"].get(str(i)) if gaap_high: - eps_estimates["high_estimate_gaap"] = gaap_high - mean = seek_object["eps_normalized_consensus_mean"][str(i)][0].get( - "dataitemvalue" - ) + eps_estimates["high_estimate_gaap"] = gaap_high[0].get( + "dataitemvalue" + ) + mean = seek_object["eps_normalized_consensus_mean"].get(str(i)) if mean: + mean = mean[0].get("dataitemvalue") eps_estimates["mean"] = mean - gaap_mean = seek_object["eps_gaap_consensus_mean"][str(i)][0].get( - "dataitemvalue" - ) + gaap_mean = seek_object["eps_gaap_consensus_mean"].get(str(i)) if gaap_mean: - eps_estimates["mean_gaap"] = gaap_mean + eps_estimates["mean_gaap"] = gaap_mean[0].get("dataitemvalue") # Calculate the estimated growth percent. this = float(mean) if mean else None prev = None diff --git a/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_sales_estimates.py b/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_sales_estimates.py index c97f60d89c4a..1bd706ed167e 100644 --- a/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_sales_estimates.py +++ b/openbb_platform/providers/seeking_alpha/openbb_seeking_alpha/models/forward_sales_estimates.py @@ -80,7 +80,7 @@ async def aextract_data( "estimates_data_items": "revenue_actual,revenue_consensus_low,revenue_consensus_mean," "revenue_consensus_high,revenue_num_of_estimates", "period_type": fp, - "relative_periods": "-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11", + "relative_periods": "-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12", } ids = {ticker: await get_seekingalpha_id(ticker) for ticker in tickers} querystring["ticker_ids"] = (",").join(list(ids.values())) @@ -111,7 +111,7 @@ def transform_data( warn(f"Symbol Error: No data found for, {ticker}") seek_object = estimates.get(sa_id) items = len(seek_object["revenue_num_of_estimates"]) - for i in range(0, items - 3): + for i in range(0, items - 4): rev_estimates: Dict = {} rev_estimates["symbol"] = ticker period = seek_object["revenue_num_of_estimates"][str(i)][0].get( @@ -132,13 +132,15 @@ def transform_data( if period_type == "quarterly" else "FY" ) - rev_estimates["number_of_analysts"] = seek_object[ - "revenue_num_of_estimates" - ][str(i)][0].get("dataitemvalue") - mean = seek_object["revenue_consensus_mean"][str(i)][0].get( + num_estimates = seek_object["revenue_num_of_estimates"].get(str(i)) + if not num_estimates: + continue + rev_estimates["number_of_analysts"] = num_estimates[0].get( "dataitemvalue" ) + mean = seek_object["revenue_consensus_mean"].get(str(i)) if mean: + mean = mean[0].get("dataitemvalue") rev_estimates["mean"] = int(float(mean)) actual = ( seek_object["revenue_actual"][str(i)][0].get("dataitemvalue") @@ -147,15 +149,13 @@ def transform_data( ) if actual: rev_estimates["actual"] = int(float(actual)) - low = seek_object["revenue_consensus_low"][str(i)][0].get( - "dataitemvalue" - ) + low = seek_object["revenue_consensus_low"].get(str(i)) if low: + low = low[0].get("dataitemvalue") rev_estimates["low_estimate"] = int(float(low)) - high = seek_object["revenue_consensus_high"][str(i)][0].get( - "dataitemvalue" - ) + high = seek_object["revenue_consensus_high"].get(str(i)) if high: + high = high[0].get("dataitemvalue") rev_estimates["high_estimate"] = int(float(high)) # Calculate the estimated growth percent. this = float(mean) if mean else None