From 9f0bf8bfc502728d364c01abc3f9ec2bdbfea439 Mon Sep 17 00:00:00 2001 From: Bhupesh-V Date: Sun, 21 Jan 2024 19:28:58 +0530 Subject: [PATCH] ignore deleted questions from the summarizer --- ama-summarizer/.gitignore | 1 + ama-summarizer/main.py | 49 ++++++++++++++++++--------------- ama-summarizer/readme.md | 8 +++--- ama-summarizer/requirements.txt | 1 + 4 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 ama-summarizer/.gitignore create mode 100644 ama-summarizer/requirements.txt diff --git a/ama-summarizer/.gitignore b/ama-summarizer/.gitignore new file mode 100644 index 0000000..597e274 --- /dev/null +++ b/ama-summarizer/.gitignore @@ -0,0 +1 @@ +questions.md \ No newline at end of file diff --git a/ama-summarizer/main.py b/ama-summarizer/main.py index e136224..2285ca4 100644 --- a/ama-summarizer/main.py +++ b/ama-summarizer/main.py @@ -1,60 +1,65 @@ import praw import os -from dotenv import dotenv_values + def get_reddit_instance(): # Reddit API credentials - user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14.2; rv:109.0) Gecko/20100101 Firefox/121.0' + user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 14.2; rv:109.0) Gecko/20100101 Firefox/121.0" client_id = os.environ["REDDIT_CLIENT_ID"] client_secret = os.environ["REDDIT_CLIENT_SECRET"] reddit_pass = os.environ["REDDIT_PASSWORD"] username = os.environ["REDDIT_USERNAME"] # Create a Reddit instance - reddit = praw.Reddit(client_id=client_id, - client_secret=client_secret, - password=reddit_pass, - user_agent=user_agent, - username=username) + reddit = praw.Reddit( + client_id=client_id, + client_secret=client_secret, + password=reddit_pass, + user_agent=user_agent, + username=username, + ) return reddit def get_post_url(): - - post_url = input("Enter the AMA post URL: ") # reddit.com URLs preferred + post_url = input("Enter the AMA post URL: ") # reddit.com URLs preferred return post_url -def get_guest_username(): - guest_username = input("Enter the AMA guest username: ") +def get_guest_username(): + guest_username = input("Enter the AMA guest username: ") return guest_username + def main(): reddit = get_reddit_instance() - + post_url = get_post_url() guest_username = get_guest_username() submission = reddit.submission(url=post_url) submission.comments.replace_more(limit=None) - markdown_file = '' + markdown_file = "" question_number = 1 for comment in submission.comments.list(): if comment.author and comment.author.name.lower() == guest_username.lower(): - question_text = comment.parent().body.replace('\n', ' ') - question_link = 'https://reddit.com' + comment.parent().permalink - markdown_file += f'{question_number}. [{question_text}]({question_link})\n' - question_number += 1 + # TODO truncate long questions with ellipsis + question_text = comment.parent().body.replace("\n", " ") + # avoid deleted questions/comments + if question_text != "[deleted]": + question_link = "https://reddit.com" + comment.parent().permalink + markdown_file += ( + f"{question_number}. [{question_text}]({question_link})\n" + ) + question_number += 1 - # UTF-8 encoding - with open('questions.md', 'w', encoding='utf-8') as file: + with open("questions.md", "w", encoding="utf-8") as file: file.write(markdown_file) - print('Markdown file questions.md generated successfully.') + print(f"{question_number} questions generated successfully.") + if __name__ == "__main__": main() - - diff --git a/ama-summarizer/readme.md b/ama-summarizer/readme.md index 18b4c47..4f40d10 100644 --- a/ama-summarizer/readme.md +++ b/ama-summarizer/readme.md @@ -1,5 +1,5 @@ -The Python script to help during AMAs. It generates a markdown file of questions and links of the questions the AMA guest has answered. +# ama-summarizer -This script is designed to generate a Markdown file containing questions and links from a Reddit post's comments. It specifically focuses on questions or comments answered by a specific user. - -The resulting Markdown file will contain a list of questions or comments that the specified guest has answered, with each question numbered and linked to the corresponding Reddit comment. +- The Python script to help during [AMAs](https://developersindia.in/ama-archive/). It generates a markdown file of questions and links to the questions the AMA guest has answered. +- This script is designed to generate a Markdown file containing questions and links from a Reddit post's comments. It specifically focuses on questions or comments answered by a specific user. +- The resulting Markdown file will contain a list of questions or comments that the specified guest has answered, with each question numbered and linked to the corresponding Reddit comment. diff --git a/ama-summarizer/requirements.txt b/ama-summarizer/requirements.txt new file mode 100644 index 0000000..9d9d90a --- /dev/null +++ b/ama-summarizer/requirements.txt @@ -0,0 +1 @@ +praw \ No newline at end of file