From 1e2e0b163f0cbc92e4b39cb5de526bbcf1d78b3f Mon Sep 17 00:00:00 2001 From: didier Date: Tue, 23 Feb 2021 22:53:54 +0000 Subject: [PATCH] Improve watchlist command in reddit_api --- sentiment/reddit_api.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/sentiment/reddit_api.py b/sentiment/reddit_api.py index 296b740d07b..7cbb5b2c01d 100644 --- a/sentiment/reddit_api.py +++ b/sentiment/reddit_api.py @@ -48,6 +48,7 @@ def watchlist(l_args): d_watchlist_tickers = {} l_watchlist_links = list() l_watchlist_author = list() + ls_text = list() praw_api = praw.Reddit(client_id=cfg.API_REDDIT_CLIENT_ID, client_secret=cfg.API_REDDIT_CLIENT_SECRET, @@ -76,11 +77,20 @@ def watchlist(l_args): if not submission.removed_by_category and submission.selftext \ and submission.link_flair_text not in ['Yolo', 'Meme'] \ and submission.author.name not in l_watchlist_author: + ls_text = list() + ls_text.append(submission.selftext) + ls_text.append(submission.title) + + submission.comments.replace_more(limit=0) + for comment in submission.comments.list(): + ls_text.append(comment.body) + + l_tickers_found = list() + for s_text in ls_text: + for s_ticker in set(re.findall(r'([A-Z]{3,5} )', s_text)): + l_tickers_found.append(s_ticker.strip()) - # Make sure there is at least 1 ticker found on the post text - l_tickers_found = set(re.findall("[A-Z]{3,5}", submission.selftext)) if l_tickers_found: - # Add another author's name to the parsed watchlists l_watchlist_author.append(submission.author.name) @@ -146,12 +156,13 @@ def watchlist(l_args): # 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]) - s_watchlist_tickers += f"{t_ticker[1]} {t_ticker[0]}, " + if int(t_ticker[1]) > 1: + s_watchlist_tickers += f"{t_ticker[1]} {t_ticker[0]}, " n_tickers += 1 except: pass if n_tickers: - print("The following stock tickers have been mentioned across the previous watchlists:") + print("The following stock tickers have been mentioned more than once across the previous watchlists:") print(s_watchlist_tickers[:-2]+'\n') print("")