From c434ae3347879f2092f2e2075e1c844b1ba74410 Mon Sep 17 00:00:00 2001 From: jeroen Date: Tue, 19 Sep 2023 22:53:22 +0200 Subject: [PATCH] Convert to requests since Ruff complained --- financetoolkit/base/toolkit_controller.py | 31 ++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/financetoolkit/base/toolkit_controller.py b/financetoolkit/base/toolkit_controller.py index e42b8c8c..83149d95 100644 --- a/financetoolkit/base/toolkit_controller.py +++ b/financetoolkit/base/toolkit_controller.py @@ -2,12 +2,11 @@ __docformat__ = "google" -import json import re -import urllib from datetime import datetime, timedelta import pandas as pd +import requests from financetoolkit.base.fundamentals_model import ( get_analyst_estimates as _get_analyst_estimates, @@ -141,16 +140,24 @@ def __init__( self._tickers: list[str] = [] for ticker in tickers: if bool(re.match("^([A-Z]{2})([A-Z0-9]{9})([0-9])$", ticker)): - url = f"https://query2.finance.yahoo.com/v1/finance/search?q={ticker}" - - with urllib.request.urlopen(url) as response: - data = response.read().decode("utf-8") - data = json.loads(data) - - try: - print(f"Converted {ticker} to {data['quotes'][0]['symbol']}") - self._tickers.append(data["quotes"][0]["symbol"]) - except (KeyError, ValueError, IndexError): + response = requests.get( + f"https://query2.finance.yahoo.com/v1/finance/search?q={ticker}", + timeout=60, + headers={ + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit" + "/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36" + }, + ) + + if response.status_code == 200: # noqa + data = response.json() + + try: + print(f"Converted {ticker} to {data['quotes'][0]['symbol']}") + self._tickers.append(data["quotes"][0]["symbol"]) + except (KeyError, ValueError, IndexError): + print(f"Could not convert {ticker}") + else: print(f"Could not convert {ticker}") else: self._tickers.append(ticker)