def get_upcoming_earnings(
    api_key: Dict[str, str],
    start_date: str,
    end_date: str,
    country: str,
    only_sp500: bool,
):
    """Returns stocks announcing there earnings in next 3 months."""
    CSV_URL = f"https://www.alphavantage.co/query?function=EARNINGS_CALENDAR&horizon=3month&apikey={api_key}"
    with requests.Session() as s:
        download = s.get(CSV_URL)
        decoded_content = download.content.decode("utf-8")
        cr = list(csv.reader(decoded_content.splitlines(), delimiter=","))
        df = pd.DataFrame(columns=cr[0], data=cr[1:])
        sd = pd.to_datetime(start_date, format="%Y-%m-%d")
        ed = pd.to_datetime(end_date, format="%Y-%m-%d")
        df["reportDate"] = pd.to_datetime(df["reportDate"])
        df = df[df["currency"] == country][df["reportDate"] > sd][df["reportDate"] < ed]
        if only_sp500:
            sp500 = pd.read_html(
                "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
            )[0]
            sp500_tickers = list(sp500["Symbol"])
            df = df[df["symbol"].isin(sp500_tickers)]
        return df[["symbol", "name", "reportDate"]]
Returns stocks announcing there earnings in next 3 months.
llama-index-integrations/tools/llama-index-tools-finance/llama_index/tools/finance/earnings.py