From 35f7651359e9bba65f134ec287316286bd28ff51 Mon Sep 17 00:00:00 2001 From: Bruno Xavier Date: Thu, 11 Mar 2021 03:57:38 -0800 Subject: [PATCH 1/2] Wrapping the reddit loop logic in try catch, preventing exceptions from aborting the run --- gamestonk_terminal/sentiment/reddit_api.py | 378 +++++++++++---------- 1 file changed, 199 insertions(+), 179 deletions(-) diff --git a/gamestonk_terminal/sentiment/reddit_api.py b/gamestonk_terminal/sentiment/reddit_api.py index b59db635b32..460ac9703c2 100644 --- a/gamestonk_terminal/sentiment/reddit_api.py +++ b/gamestonk_terminal/sentiment/reddit_api.py @@ -74,51 +74,55 @@ def watchlist(l_args): n_flair_posts_found = 0 while True: - submission = next(submissions, None) - - # Check if search_submissions didn't get anymore posts - if not submission: - break - - # 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.link_flair_text not in ["Yolo", "Meme"] - and submission.author.name not in l_watchlist_author - ): - l_tickers_found = find_tickers(submission) - - 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://old.reddit.com{submission.permalink}" - ) - - print_and_record_reddit_post(d_submission, submission) - - # Increment count of valid posts found - n_flair_posts_found += 1 - - # Check if number of wanted posts found has been reached - if n_flair_posts_found > ns_parser.n_limit - 1: - break + try: + submission = next(submissions, None) + + # Check if search_submissions didn't get anymore posts + if not submission: + break + + # 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.link_flair_text not in ["Yolo", "Meme"] + and submission.author.name not in l_watchlist_author + ): + l_tickers_found = find_tickers(submission) + + 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://old.reddit.com{submission.permalink}" + ) + + print_and_record_reddit_post(d_submission, submission) + + # Increment count of valid posts found + n_flair_posts_found += 1 + + # Check if number of wanted posts found has been reached + if n_flair_posts_found > ns_parser.n_limit - 1: + break + except Exception as e: + print(e) + print("") if n_flair_posts_found: lt_watchlist_sorted = sorted( @@ -248,39 +252,43 @@ def popular_tickers(l_args): n_tickers = 0 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 content - if ( - not submission.removed_by_category - and (submission.selftext or submission.title) - and submission.author.name not in l_watchlist_author - ): - l_tickers_found = find_tickers(submission) - - if l_tickers_found: - n_tickers += len(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 - - # Check if search_submissions didn't get anymore posts - else: - break + try: + 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 content + if ( + not submission.removed_by_category + and (submission.selftext or submission.title) + and submission.author.name not in l_watchlist_author + ): + l_tickers_found = find_tickers(submission) + + if l_tickers_found: + n_tickers += len(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 + + # Check if search_submissions didn't get anymore posts + else: + break + except Exception as e: + print(e) + print("") print(f" {n_tickers} potential tickers found.") @@ -405,44 +413,48 @@ def spac_community(l_args): submissions = praw_api.subreddit("SPACs").new(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.link_flair_text not in ["Yolo", "Meme"] - and submission.author.name not in l_watchlist_author - ): - l_tickers_found = find_tickers(submission) - - 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://old.reddit.com{submission.permalink}" - ) - - print_and_record_reddit_post(d_submission, submission) - - # Check if search_submissions didn't get anymore posts - else: - break + try: + 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.link_flair_text not in ["Yolo", "Meme"] + and submission.author.name not in l_watchlist_author + ): + l_tickers_found = find_tickers(submission) + + 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://old.reddit.com{submission.permalink}" + ) + + print_and_record_reddit_post(d_submission, submission) + + # Check if search_submissions didn't get anymore posts + else: + break + except Exception as e: + print(e) + print("") if d_watchlist_tickers: lt_watchlist_sorted = sorted( @@ -539,51 +551,55 @@ def spac(l_args): ) n_flair_posts_found = 0 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.link_flair_text not in ["Yolo", "Meme"] - and submission.author.name not in l_watchlist_author - ): - l_tickers_found = find_tickers(submission) - - 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://old.reddit.com{submission.permalink}" - ) - - print_and_record_reddit_post(d_submission, submission) - - # Increment count of valid posts found - n_flair_posts_found += 1 - - # Check if number of wanted posts found has been reached - if n_flair_posts_found > ns_parser.n_limit - 1: + try: + 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.link_flair_text not in ["Yolo", "Meme"] + and submission.author.name not in l_watchlist_author + ): + l_tickers_found = find_tickers(submission) + + 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://old.reddit.com{submission.permalink}" + ) + + print_and_record_reddit_post(d_submission, submission) + + # Increment count of valid posts found + n_flair_posts_found += 1 + + # Check if number of wanted posts found has been reached + if n_flair_posts_found > ns_parser.n_limit - 1: + break + + # Check if search_submissions didn't get anymore posts + else: break - - # Check if search_submissions didn't get anymore posts - else: - break + except Exception as e: + print(e) + print("") if n_flair_posts_found: lt_watchlist_sorted = sorted( @@ -666,25 +682,29 @@ def wsb_community(l_args): ) 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: - - l_watchlist_links.append( - f"https://old.reddit.com{submission.permalink}" - ) - - print_and_record_reddit_post(d_submission, submission) - - # Check if search_submissions didn't get anymore posts - else: - break + try: + 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: + + l_watchlist_links.append( + f"https://old.reddit.com{submission.permalink}" + ) + + print_and_record_reddit_post(d_submission, submission) + + # Check if search_submissions didn't get anymore posts + else: + break + except Exception as e: + print(e) + print("") print("") except Exception as e: print(e) From 5a765d4e7617c1b40307089f40e91223a84329de Mon Sep 17 00:00:00 2001 From: Bruno Xavier Date: Thu, 11 Mar 2021 04:05:24 -0800 Subject: [PATCH 2/2] Linting code --- gamestonk_terminal/sentiment/reddit_api.py | 64 +++++++++++----------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/gamestonk_terminal/sentiment/reddit_api.py b/gamestonk_terminal/sentiment/reddit_api.py index 460ac9703c2..606cf2a637d 100644 --- a/gamestonk_terminal/sentiment/reddit_api.py +++ b/gamestonk_terminal/sentiment/reddit_api.py @@ -76,14 +76,14 @@ def watchlist(l_args): while True: try: submission = next(submissions, None) - + # Check if search_submissions didn't get anymore posts if not submission: break - + # 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 @@ -94,11 +94,11 @@ def watchlist(l_args): and submission.author.name not in l_watchlist_author ): l_tickers_found = find_tickers(submission) - + 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: @@ -107,16 +107,16 @@ def watchlist(l_args): else: # Initialize stock ticker found d_watchlist_tickers[key] = 1 - + l_watchlist_links.append( f"https://old.reddit.com{submission.permalink}" ) - + print_and_record_reddit_post(d_submission, submission) - + # Increment count of valid posts found n_flair_posts_found += 1 - + # Check if number of wanted posts found has been reached if n_flair_posts_found > ns_parser.n_limit - 1: break @@ -257,7 +257,7 @@ def popular_tickers(l_args): 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 content @@ -267,13 +267,13 @@ def popular_tickers(l_args): and submission.author.name not in l_watchlist_author ): l_tickers_found = find_tickers(submission) - + if l_tickers_found: n_tickers += len(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: @@ -282,7 +282,7 @@ def popular_tickers(l_args): else: # Initialize stock ticker found d_watchlist_tickers[key] = 1 - + # Check if search_submissions didn't get anymore posts else: break @@ -418,7 +418,7 @@ def spac_community(l_args): 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 @@ -429,11 +429,11 @@ def spac_community(l_args): and submission.author.name not in l_watchlist_author ): l_tickers_found = find_tickers(submission) - + 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: @@ -442,13 +442,13 @@ def spac_community(l_args): else: # Initialize stock ticker found d_watchlist_tickers[key] = 1 - + l_watchlist_links.append( f"https://old.reddit.com{submission.permalink}" ) - + print_and_record_reddit_post(d_submission, submission) - + # Check if search_submissions didn't get anymore posts else: break @@ -556,7 +556,7 @@ def spac(l_args): 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 @@ -567,11 +567,11 @@ def spac(l_args): and submission.author.name not in l_watchlist_author ): l_tickers_found = find_tickers(submission) - + 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: @@ -580,20 +580,20 @@ def spac(l_args): else: # Initialize stock ticker found d_watchlist_tickers[key] = 1 - + l_watchlist_links.append( f"https://old.reddit.com{submission.permalink}" ) - + print_and_record_reddit_post(d_submission, submission) - + # Increment count of valid posts found n_flair_posts_found += 1 - + # Check if number of wanted posts found has been reached if n_flair_posts_found > ns_parser.n_limit - 1: break - + # Check if search_submissions didn't get anymore posts else: break @@ -687,18 +687,18 @@ def wsb_community(l_args): 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: - + l_watchlist_links.append( f"https://old.reddit.com{submission.permalink}" ) - + print_and_record_reddit_post(d_submission, submission) - + # Check if search_submissions didn't get anymore posts else: break