diff --git a/discovery/disc_menu.py b/discovery/disc_menu.py index 1e8b4d735c5..bc5bf004dbb 100644 --- a/discovery/disc_menu.py +++ b/discovery/disc_menu.py @@ -17,6 +17,7 @@ def print_discovery(): print(" gainers show latest top gainers [Yahoo Finance]") print(" spac show other users spacs announcements [Reddit]") print(" spac_c show other users spacs announcements from subreddit SPACs [Reddit]") + print(" wsb show what WSB gang is up to in subreddit wallstreetbets [Reddit]") print(" watchlist show other users watchlist [Reddit]") print(" popular show popular tickers [Reddit]") print("") @@ -29,7 +30,7 @@ def disc_menu(): # Add list of arguments that the discovery parser accepts disc_parser = argparse.ArgumentParser(prog='discovery', add_help=False) disc_parser.add_argument('cmd', choices=['help', 'q', 'quit', - 'sectors', 'gainers', 'watchlist', 'spac', 'spac_c', 'popular']) + 'sectors', 'gainers', 'watchlist', 'spac', 'spac_c', 'wsb', 'popular']) print_discovery() @@ -75,6 +76,9 @@ def disc_menu(): elif ns_known_args.cmd == 'spac_c': reddit_api.spac_community(l_args) + elif ns_known_args.cmd == 'wsb': + reddit_api.wsb_community(l_args) + elif ns_known_args.cmd == 'popular': reddit_api.popular_tickers(l_args) diff --git a/discovery/reddit_api.py b/discovery/reddit_api.py index 17e29a4f078..8572f28ef5b 100644 --- a/discovery/reddit_api.py +++ b/discovery/reddit_api.py @@ -425,7 +425,7 @@ def spac_community(l_args): psaw_api = PushshiftAPI() if ns_parser.b_popular: - submissions = praw_api.subreddit('SPACs').top(limit=ns_parser.n_limit) + submissions = praw_api.subreddit('SPACs').hot(limit=ns_parser.n_limit) else: submissions = praw_api.subreddit('SPACs').new(limit=ns_parser.n_limit) @@ -512,5 +512,126 @@ def spac_community(l_args): if n_tickers: print("The following stock tickers have been mentioned across the previous SPACs:") print(s_watchlist_tickers[:-2]) + print("") + except: + print("") + + +# ------------------------------------------------------------------------------------------------------------------- +def wsb_community(l_args): + parser = argparse.ArgumentParser(prog='wsb', + description="""Show what WSB gang are up to in subreddit wallstreetbets [Reddit] """) + parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=10, help='Limit of posts') + parser.add_argument('-n', "--new", action="store_true", default=False, dest="b_new", help='If new, the posts retrieved are based on more recent rather than score') + + try: + (ns_parser, l_unknown_args) = parser.parse_known_args(l_args) + + if l_unknown_args: + print(f"The following args couldn't be interpreted: {l_unknown_args}\n") + return + + praw_api = praw.Reddit(client_id=cfg.API_REDDIT_CLIENT_ID, + client_secret=cfg.API_REDDIT_CLIENT_SECRET, + username=cfg.API_REDDIT_USERNAME, + user_agent=cfg.API_REDDIT_USER_AGENT, + password=cfg.API_REDDIT_PASSWORD) + + d_submission = {} + d_watchlist_tickers = {} + l_watchlist_links = list() + l_watchlist_author = list() + + psaw_api = PushshiftAPI() + + if ns_parser.b_new: + submissions = praw_api.subreddit('wallstreetbets').new(limit=ns_parser.n_limit) + else: + submissions = praw_api.subreddit('wallstreetbets').hot(limit=ns_parser.n_limit) + + while True: + submission = next(submissions, None) + if submission: + # Get more information about post using PRAW api + submission = praw_api.submission(id=submission.id) + + # Ensure that the post hasn't been removed by moderator in the meanwhile, + #that there is a description and it's not just an image, that the flair is + #meaningful, and that we aren't re-considering same author's watchlist + if not submission.removed_by_category and submission.selftext \ + and submission.author.name not in l_watchlist_author: # and submission.link_flair_text not in ['Yolo', 'Meme'] \ + + # 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 not l_tickers_found: + l_tickers_found = set(re.findall("[A-Z]{3,5}", submission.title)) + + if l_tickers_found: + # Add another author's name to the parsed watchlists + l_watchlist_author.append(submission.author.name) + + # Lookup stock tickers within a watchlist + for key in l_tickers_found: + if key in d_watchlist_tickers: + # Increment stock ticker found + d_watchlist_tickers[key] += 1 + else: + # Initialize stock ticker found + d_watchlist_tickers[key] = 1 + + l_watchlist_links.append(f"https://www.reddit.com{submission.permalink}") + # delte below, not necessary I reckon. Probably just link? + + # Refactor data + s_datetime = datetime.utcfromtimestamp(submission.created_utc).strftime("%d/%m/%Y %H:%M:%S") + s_link = f"https://www.reddit.com{submission.permalink}" + s_all_awards = "" + for award in submission.all_awardings: + s_all_awards += f"{award['count']} {award['name']}\n" + s_all_awards = s_all_awards[:-2] + + # Create dictionary with data to construct dataframe allows to save data + d_submission[submission.id] = { + 'created_utc': s_datetime, + 'subreddit': submission.subreddit, + 'link_flair_text': submission.link_flair_text, + 'title':submission.title, + 'score': submission.score, + 'link': s_link, + 'num_comments': submission.num_comments, + 'upvote_ratio': submission.upvote_ratio, + 'awards': s_all_awards + } + + # Print post data collected so far + print(f"{s_datetime} - {submission.title}") + print(f"{s_link}") + t_post = PrettyTable(['Subreddit', 'Flair', 'Score', '# Comments', 'Upvote %', "Awards"]) + t_post.add_row([submission.subreddit, submission.link_flair_text, submission.score, + submission.num_comments,f"{round(100*submission.upvote_ratio)}%", s_all_awards]) + print(t_post) + print("\n") + + # Check if search_submissions didn't get anymore posts + else: + break + + if d_watchlist_tickers: + lt_watchlist_sorted = sorted(d_watchlist_tickers.items(), key=lambda item: item[1], reverse=True) + s_watchlist_tickers = "" + n_tickers = 0 + for t_ticker in lt_watchlist_sorted: + 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]) + 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 SPACs:") + print(s_watchlist_tickers[:-2]) + print("") except: print("") \ No newline at end of file