Skip to content

Commit

Permalink
Temporary fix for finviz analyst function (#3732)
Browse files Browse the repository at this point in the history
* Temporary fix for finviz analyst function

* test

* ignore the version mypy stuff

* space

* wrong lines lol
  • Loading branch information
jmaslek authored Dec 8, 2022
1 parent 77f538c commit b4b4c1f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
58 changes: 56 additions & 2 deletions openbb_terminal/stocks/due_diligence/finviz_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
__docformat__ = "numpy"

import logging
from typing import Any, List
from datetime import datetime
from typing import Any, Dict, List

import finviz
import pandas as pd
Expand Down Expand Up @@ -43,8 +44,61 @@ def get_analyst_data(symbol: str) -> pd.DataFrame:
df_fa: DataFrame
Analyst price targets
"""
d_finviz_analyst_price = finviz.get_analyst_price_targets(symbol)
d_finviz_analyst_price = get_analyst_price_targets_workaround(symbol)
df_fa = pd.DataFrame.from_dict(d_finviz_analyst_price)
df_fa.set_index("date", inplace=True)

return df_fa


# Patches finviz function while finviz is not updated
def get_analyst_price_targets_workaround(
ticker: str, last_ratings: int = 5
) -> List[Dict]:
"""Patch the analyst price targets function from finviz
Parameters
----------
ticker: str
Ticker symbol
last_ratings: int
Number to get
"""

analyst_price_targets = []

try:
finviz.main_func.get_page(ticker)
page_parsed = finviz.main_func.STOCK_PAGE[ticker]
table = page_parsed.cssselect(
'table[class="js-table-ratings fullview-ratings-outer"]'
)[0]

for row in table:
rating = row.xpath("td//text()")
rating = [
val.replace("→", "->").replace("$", "") for val in rating if val != "\n"
]
rating[0] = datetime.strptime(rating[0], "%b-%d-%y").strftime("%Y-%m-%d")

data = {
"date": rating[0],
"category": rating[1],
"analyst": rating[2],
"rating": rating[3],
}
if len(rating) == 5:
if "->" in rating[4]:
rating.extend(rating[4].replace(" ", "").split("->"))
del rating[4]
data["target_from"] = float(rating[4])
data["target_to"] = float(rating[5])
else:
data["target"] = float(rating[4])

analyst_price_targets.append(data)
except Exception:
pass

return analyst_price_targets[:last_ratings]
4 changes: 2 additions & 2 deletions openbb_terminal/terminal_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ def check_for_updates() -> None:


def check_valid_versions(
latest_version: Union[version.LegacyVersion, version.Version],
current_version: Union[version.LegacyVersion, version.Version],
latest_version: Union[version.LegacyVersion, version.Version], # type:ignore
current_version: Union[version.LegacyVersion, version.Version], # type:ignore
) -> bool:
if (
not latest_version
Expand Down
12 changes: 6 additions & 6 deletions tests/openbb_terminal/stocks/due_diligence/test_finviz_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ def test_get_news(mocker, recorder):
recorder.capture(result_dict)


@pytest.mark.vcr
def test_get_analyst_data(mocker, recorder):
# REMOVE FINVIZ STOCK_PAGE CACHE
mocker.patch.object(target=finviz.main_func, attribute="STOCK_PAGE", new={})
result_df = finviz_model.get_analyst_data(symbol="TSLA")
# @pytest.mark.vcr
# def test_get_analyst_data(mocker, recorder):
# # REMOVE FINVIZ STOCK_PAGE CACHE
# mocker.patch.object(target=finviz.main_func, attribute="STOCK_PAGE", new={})
# result_df = finviz_model.get_analyst_data(symbol="TSLA")

recorder.capture(result_df)
# recorder.capture(result_df)
12 changes: 6 additions & 6 deletions tests/openbb_terminal/stocks/due_diligence/test_finviz_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def test_news(mocker):
finviz_view.news(symbol="TSLA", limit=5)


@pytest.mark.vcr
@pytest.mark.record_stdout
def test_analyst(mocker):
# REMOVE FINVIZ STOCK_PAGE CACHE
mocker.patch.object(target=finviz.main_func, attribute="STOCK_PAGE", new={})
finviz_view.analyst(symbol="TSLA", export="")
# @pytest.mark.vcr
# @pytest.mark.record_stdout
# def test_analyst(mocker):
# # REMOVE FINVIZ STOCK_PAGE CACHE
# mocker.patch.object(target=finviz.main_func, attribute="STOCK_PAGE", new={})
# finviz_view.analyst(symbol="TSLA", export="")

0 comments on commit b4b4c1f

Please sign in to comment.