2024-01-20 16:35:28 +05:30
|
|
|
import praw
|
|
|
|
import os
|
2024-01-21 19:28:58 +05:30
|
|
|
|
2024-01-20 16:35:28 +05:30
|
|
|
|
|
|
|
def get_reddit_instance():
|
|
|
|
# Reddit API credentials
|
2024-01-21 19:28:58 +05:30
|
|
|
user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 14.2; rv:109.0) Gecko/20100101 Firefox/121.0"
|
2024-01-20 16:35:28 +05:30
|
|
|
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
|
2024-01-21 19:28:58 +05:30
|
|
|
reddit = praw.Reddit(
|
|
|
|
client_id=client_id,
|
|
|
|
client_secret=client_secret,
|
|
|
|
password=reddit_pass,
|
|
|
|
user_agent=user_agent,
|
|
|
|
username=username,
|
|
|
|
)
|
2024-01-20 16:35:28 +05:30
|
|
|
return reddit
|
|
|
|
|
|
|
|
|
|
|
|
def get_post_url():
|
2024-01-21 19:28:58 +05:30
|
|
|
post_url = input("Enter the AMA post URL: ") # reddit.com URLs preferred
|
2024-01-20 16:35:28 +05:30
|
|
|
return post_url
|
|
|
|
|
|
|
|
|
2024-01-21 19:28:58 +05:30
|
|
|
def get_guest_username():
|
|
|
|
guest_username = input("Enter the AMA guest username: ")
|
2024-01-20 16:35:28 +05:30
|
|
|
return guest_username
|
|
|
|
|
2024-01-21 19:28:58 +05:30
|
|
|
|
2024-01-20 16:35:28 +05:30
|
|
|
def main():
|
|
|
|
reddit = get_reddit_instance()
|
2024-01-21 19:28:58 +05:30
|
|
|
|
2024-01-20 16:35:28 +05:30
|
|
|
post_url = get_post_url()
|
|
|
|
guest_username = get_guest_username()
|
|
|
|
|
|
|
|
submission = reddit.submission(url=post_url)
|
|
|
|
submission.comments.replace_more(limit=None)
|
|
|
|
|
2024-01-21 19:28:58 +05:30
|
|
|
markdown_file = ""
|
2024-01-20 16:35:28 +05:30
|
|
|
question_number = 1
|
|
|
|
|
|
|
|
for comment in submission.comments.list():
|
|
|
|
if comment.author and comment.author.name.lower() == guest_username.lower():
|
2024-01-21 19:28:58 +05:30
|
|
|
# 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 += (
|
2024-03-05 13:35:01 +05:30
|
|
|
f"{question_number}. [{question_text}]({question_link})\n\n"
|
|
|
|
) # Add an extra newline after each question
|
2024-01-21 19:28:58 +05:30
|
|
|
question_number += 1
|
|
|
|
|
|
|
|
with open("questions.md", "w", encoding="utf-8") as file:
|
2024-01-20 16:35:28 +05:30
|
|
|
file.write(markdown_file)
|
|
|
|
|
2024-03-05 13:35:01 +05:30
|
|
|
print(f"{question_number - 1} questions generated successfully.")
|
2024-01-21 19:28:58 +05:30
|
|
|
|
2024-01-20 16:35:28 +05:30
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|