From 4aee22fe09fb3ba20f6ae5b47ff35fd099e1f60e Mon Sep 17 00:00:00 2001 From: Artem Veremey Date: Fri, 5 Mar 2021 12:52:57 -0800 Subject: [PATCH] Improvents for sentiment/popular --- .github/workflows/test.yml | 2 +- gamestonk_terminal/sentiment/reddit_api.py | 71 +++++++++++++++++++--- 2 files changed, 64 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f4c83f46d01..3c206e17976 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,7 +23,7 @@ jobs: - run: pip install bandit black codespell flake8 mypy pyupgrade safety pylint - run: bandit -r . || true - run: black --check . - - run: codespell --ignore-words-list="mape" --quiet-level=2 # --skip="" + - run: codespell --ignore-words-list="mape" --quiet-level=2 --skip=".git" # --skip="" - run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - run: mypy --ignore-missing-imports . || true - run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true diff --git a/gamestonk_terminal/sentiment/reddit_api.py b/gamestonk_terminal/sentiment/reddit_api.py index 2c477d8efb2..cec57770e1b 100644 --- a/gamestonk_terminal/sentiment/reddit_api.py +++ b/gamestonk_terminal/sentiment/reddit_api.py @@ -2,6 +2,9 @@ import argparse import re from datetime import datetime, timedelta import warnings +import pandas as pd +from prawcore.exceptions import ResponseException +from requests import HTTPError from prettytable import PrettyTable from psaw import PushshiftAPI import praw @@ -226,6 +229,15 @@ def popular_tickers(l_args): prog="popular", description="""Print latest popular tickers. [Source: Reddit] """, ) + parser.add_argument( + "-n", + "--number", + action="store", + dest="n_top", + type=check_positive, + default=10, + help="display top N tickers", + ) parser.add_argument( "-l", "--limit", @@ -261,6 +273,7 @@ def popular_tickers(l_args): if l_unknown_args: print(f"The following args couldn't be interpreted: {l_unknown_args}\n") + print("popular -s -l -d ") return n_ts_after = int( @@ -356,32 +369,74 @@ def popular_tickers(l_args): else: break - print(f" {n_tickers} tickers found.") + print(f" {n_tickers} potential tickers found.") lt_watchlist_sorted = sorted( d_watchlist_tickers.items(), key=lambda item: item[1], reverse=True ) + if lt_watchlist_sorted: - print( - f"\nThe following TOP10 tickers have been mentioned in the last {ns_parser.n_days} days:" - ) n_top_stocks = 0 + # pylint: disable=redefined-outer-name + popular_tickers = [] for t_ticker in lt_watchlist_sorted: - if n_top_stocks > 9: + if n_top_stocks > ns_parser.n_top: break try: # If try doesn't trigger exception, it means that this stock exists on finviz # thus we can print it. - finviz.get_stock(t_ticker[0]) - print(f"{t_ticker[1]} {t_ticker[0]}") + stock_info = finviz.get_stock(t_ticker[0]) + popular_tickers.append( + ( + t_ticker[1], + t_ticker[0], + stock_info["Company"], + stock_info["Sector"], + stock_info["Price"], + stock_info["Change"], + stock_info["Perf Month"], + f"https://finviz.com/quote.ashx?t={t_ticker[0]}", + ) + ) n_top_stocks += 1 + except HTTPError as e: + if e.response.status_code != 404: + print(f"Unexpected exception from Fiviz: {e}") except Exception as e: print(e) - # pass + + popular_tickers_df = pd.DataFrame( + popular_tickers, + columns=[ + "Mentions", + "Ticker", + "Company", + "Sector", + "Price", + "Change", + "Perf Month", + "URL", + ], + ) + + print( + f"\nThe following TOP {ns_parser.n_top} tickers have been mentioned in the last {ns_parser.n_days} days:" + ) + + print(popular_tickers_df) + print("") else: print("No tickers found") + print("") + except ResponseException as e: + if e.response.status_code == 401: + print( + "Received a response from Reddit with an authorization error. Check your token." + ) + print("") + except Exception as e: print(e) print("")