import requests


def fetch_intraday_quote(ticker, FINNHUB_API_KEY):
    """
    Get current market price and percentage diff from today's open for a single stock ticker
    """
    r = requests.get(f'https://finnhub.io/api/v1/quote?symbol={ticker}&token={FINNHUB_API_KEY}')
    """
    dict with keys
    o - Open price of the day
    h - High price of the day
    l - Low price of the day
    c - Current price
    pc - Previous close price
    """
    res = r.json()
    # print(r.json())
    try:
        current = res["c"]
        up_down_by = (res["c"] - res["o"]) * 100 / res["o"]
    except ZeroDivisionError:
        current = "N/A"
        up_down_by = "N/A"
    except Exception as e:
        print(f"exception in fetch_intraday_quote() with ticker {ticker}")
        print(e)
        current = "N/A"
        up_down_by = "N/A"
    return {'stock_name': ticker, "current": current, "intraday_perc_change": up_down_by}