From a0acdfb13f806c35c82a7c4dc81ea98de52814e0 Mon Sep 17 00:00:00 2001 From: DidierRLopes Date: Sun, 1 Aug 2021 01:58:05 +0100 Subject: [PATCH] Fix MarketBeat disc/ratings scraping. (#584) * Add line to mention saved generated report on terminal * Fix MarketBeat scrap ratings --- .../discovery/marketbeat_model.py | 18 ++-- .../discovery/marketbeat_view.py | 94 +++++++++++-------- .../papermill/crypto_market_view.py | 8 ++ .../papermill/dark_pool_view.py | 8 ++ .../papermill/due_diligence_view.py | 8 ++ .../papermill/econ_data_view.py | 8 ++ 6 files changed, 96 insertions(+), 48 deletions(-) diff --git a/gamestonk_terminal/discovery/marketbeat_model.py b/gamestonk_terminal/discovery/marketbeat_model.py index b8bd10241e4..28faeca41fa 100644 --- a/gamestonk_terminal/discovery/marketbeat_model.py +++ b/gamestonk_terminal/discovery/marketbeat_model.py @@ -71,23 +71,23 @@ def get_ratings() -> List[dict]: td = tds[3] if td.find("span"): td.span.extract() - current_price = td.text - rating["current_price"] = current_price + analyst = td.text + rating["analyst"] = analyst # target price td = tds[4] - target_price = td.text - rating["target_price"] = target_price + current_price = td.text + rating["current_price"] = current_price # new rating td = tds[5] - new_rating = td.text - rating["rating"] = new_rating + target_price = td.text + rating["target_price"] = target_price # impact on price td = tds[6] if td.find("a"): - impact = td.find("a").text + rate = td.find("a").text else: - impact = td.text - rating["impact"] = impact + rate = td.text + rating["rate"] = rate ratings.append(rating) return ratings diff --git a/gamestonk_terminal/discovery/marketbeat_view.py b/gamestonk_terminal/discovery/marketbeat_view.py index 017338c6d28..57a87412b58 100644 --- a/gamestonk_terminal/discovery/marketbeat_view.py +++ b/gamestonk_terminal/discovery/marketbeat_view.py @@ -14,7 +14,10 @@ from gamestonk_terminal.discovery import marketbeat_model def ratings_view(other_args: List[str]): - """Prints top ratings updates + """Prints top ratings updates [Source: MarketBeat] + + MarketBeat has changed the access to their data. Now, a user needs to have 'MarketBeat All Access' + to make the most out of this command. Parameters ---------- @@ -25,7 +28,9 @@ def ratings_view(other_args: List[str]): add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter, prog="ratings", - description="""Top ratings updates. [Source: MarketBeat]""", + description="""Top ratings updates. MarketBeat has changed the access to their data. + Now, a user needs to have 'MarketBeat All Access' to make the most out of this command. + [Source: MarketBeat]""", ) parser.add_argument( "-t", @@ -33,7 +38,7 @@ def ratings_view(other_args: List[str]): action="store", dest="n_threshold", type=check_positive, - default=100, + default=0, # Change to 0 because of the preview shown by MarketBeat help="Minimum threshold in percentage change between current and target price to show ratings", ) @@ -49,69 +54,80 @@ def ratings_view(other_args: List[str]): ratings = marketbeat_model.get_ratings() df_ratings = pd.DataFrame(ratings) + df_ratings = df_ratings[df_ratings["target_price"] != ""] - df_ratings["old_target"] = df_ratings["target_price"].apply( - lambda x: x.split(" ➝ ")[0] if "➝" in x else "" + df_ratings["old_target"] = ( + df_ratings["target_price"] + .apply(lambda x: x.split(" ➝ ")[0].replace("$", "") if "➝" in x else "") + .astype(float) ) - df_ratings["new_target"] = df_ratings["target_price"].apply( - lambda x: x.split(" ➝ ")[1] if "➝" in x else x + df_ratings["new_target"] = ( + df_ratings["target_price"] + .apply(lambda x: x.split(" ➝ ")[1].replace("$", "") if "➝" in x else x) + .astype(float) ) - df_ratings["clean_current_price"] = df_ratings["current_price"].apply( - lambda x: "".join(c if c.isdigit() or c == "." else "" for c in str(x)) - ) - df_ratings["clean_current_price"] = df_ratings["clean_current_price"].astype( - float - ) - df_ratings["clean_target_price"] = df_ratings["new_target"].apply( - lambda x: "".join(c if c.isdigit() or c == "." else "" for c in str(x)) - ) - df_ratings["clean_target_price"] = df_ratings["clean_target_price"].astype( - float + df_ratings["clean_current_price"] = ( + df_ratings["current_price"] + .apply(lambda x: x.replace("0.0%", "").replace("+", "").replace("$", "")) + .astype(float) ) - df_ratings["pct_increase"] = ( + df_ratings.drop( + columns=["analyst", "current_price", "target_price"], inplace=True + ) + + df_ratings["pct_increase"] = round( 100 - * (df_ratings["clean_target_price"] - df_ratings["clean_current_price"]) - / df_ratings["clean_current_price"] + * (df_ratings["new_target"] - df_ratings["clean_current_price"]) + / df_ratings["clean_current_price"], + 2, ) - df_ratings["pct_increase"] = df_ratings["pct_increase"].round(2) + + df_ratings = df_ratings.sort_values(by=["pct_increase"], ascending=False) df_ratings["pct_abs"] = np.abs(df_ratings["pct_increase"]) - df_ratings["pct_abs"] = df_ratings["pct_abs"].round(2) - - df_ratings = df_ratings.sort_values("pct_abs", ascending=False) df_ratings_top = df_ratings[df_ratings["pct_abs"] > ns_parser.n_threshold][ [ "ticker", + "action", "brokerage", - "rating", - "current_price", + "rate", "old_target", "new_target", + "clean_current_price", "pct_increase", ] ] + df_ratings_top["old_target"] = df_ratings_top["old_target"].apply( + lambda x: str(x) + " $" + ) + + df_ratings_top["new_target"] = df_ratings_top["new_target"].apply( + lambda x: str(x) + " $" + ) + + df_ratings_top["clean_current_price"] = df_ratings_top[ + "clean_current_price" + ].apply(lambda x: str(x) + " $") + df_ratings_top["pct_increase"] = df_ratings_top["pct_increase"].apply( lambda x: str(x) + " %" ) - print( - df_ratings_top.rename( - columns={ - "ticker": "Ticker", - "brokerage": "Brokerage", - "rating": "Rating", - "current_price": "Current Price", - "old_target": "Old Target", - "new_target": "New Target", - "pct_increase": "Percentage Increase", - } - ).to_string(index=False) + df_ratings_top = df_ratings_top.rename( + columns={ + "old_target": "Old Target", + "new_target": "New Target", + "clean_current_price": "Current Price", + "pct_increase": "Increase", + } ) + print(df_ratings_top.to_string(index=False)) print("") + except Exception as e: print(e, "\n") diff --git a/gamestonk_terminal/papermill/crypto_market_view.py b/gamestonk_terminal/papermill/crypto_market_view.py index cae6ac46c79..3cb82da5daa 100644 --- a/gamestonk_terminal/papermill/crypto_market_view.py +++ b/gamestonk_terminal/papermill/crypto_market_view.py @@ -72,7 +72,15 @@ def crypto_market_report(other_args: List[str]): analysis_notebook + "." + ns_parser.mode, ) ) + print("") + print( + "Exported: ", + os.path.join( + os.path.abspath(os.path.join(".")), analysis_notebook + ".html" + ), + "\n", + ) except Exception as e: print(e, "\n") diff --git a/gamestonk_terminal/papermill/dark_pool_view.py b/gamestonk_terminal/papermill/dark_pool_view.py index ee95dab396a..0da1283512d 100644 --- a/gamestonk_terminal/papermill/dark_pool_view.py +++ b/gamestonk_terminal/papermill/dark_pool_view.py @@ -87,7 +87,15 @@ def dark_pool_report(other_args: List[str]): analysis_notebook + "." + ns_parser.mode, ) ) + print("") + print( + "Exported: ", + os.path.join( + os.path.abspath(os.path.join(".")), analysis_notebook + ".html" + ), + "\n", + ) except Exception as e: print(e, "\n") diff --git a/gamestonk_terminal/papermill/due_diligence_view.py b/gamestonk_terminal/papermill/due_diligence_view.py index ab759e9f812..581b2b83b3a 100644 --- a/gamestonk_terminal/papermill/due_diligence_view.py +++ b/gamestonk_terminal/papermill/due_diligence_view.py @@ -88,7 +88,15 @@ def due_diligence_report(other_args: List[str]): analysis_notebook + "." + ns_parser.mode, ) ) + print("") + print( + "Exported: ", + os.path.join( + os.path.abspath(os.path.join(".")), analysis_notebook + ".html" + ), + "\n", + ) except Exception as e: print(e, "\n") diff --git a/gamestonk_terminal/papermill/econ_data_view.py b/gamestonk_terminal/papermill/econ_data_view.py index fe3074cf484..138d3625aec 100644 --- a/gamestonk_terminal/papermill/econ_data_view.py +++ b/gamestonk_terminal/papermill/econ_data_view.py @@ -72,7 +72,15 @@ def economy_data_report(other_args: List[str]): analysis_notebook + "." + ns_parser.mode, ) ) + print("") + print( + "Exported: ", + os.path.join( + os.path.abspath(os.path.join(".")), analysis_notebook + ".html" + ), + "\n", + ) except Exception as e: print(e, "\n")