Skip to content

Commit

Permalink
Convert to requests since Ruff complained
Browse files Browse the repository at this point in the history
  • Loading branch information
JerBouma committed Sep 19, 2023
1 parent 4cc1409 commit c434ae3
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions financetoolkit/base/toolkit_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c434ae3

Please sign in to comment.