Guest User

Code

a guest
Jun 19th, 2022
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. from os import getenv, environ
  2.  
  3. import praw
  4.  
  5. from utils.console import print_step, print_substep
  6. from utils.subreddit import get_subreddit_undone
  7. from utils.videos import check_done
  8. from praw.models import MoreComments
  9.  
  10. TEXT_WHITELIST = set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890")
  11.  
  12.  
  13. def textify(text):
  14. return "".join(filter(TEXT_WHITELIST.__contains__, text))
  15.  
  16.  
  17. def get_subreddit_threads():
  18. """
  19. Returns a list of threads from the AskReddit subreddit.
  20. """
  21. global submission
  22. print_substep("Logging into Reddit.")
  23.  
  24. content = {}
  25. if str(getenv("REDDIT_2FA")).casefold() == "yes":
  26. print("\nEnter your two-factor authentication code from your authenticator app.\n")
  27. code = input("> ")
  28. print()
  29. pw = getenv("REDDIT_PASSWORD")
  30. passkey = f"{pw}:{code}"
  31. else:
  32. passkey = getenv("REDDIT_PASSWORD")
  33. reddit = praw.Reddit(
  34. client_id=getenv("REDDIT_CLIENT_ID"),
  35. client_secret=getenv("REDDIT_CLIENT_SECRET"),
  36. user_agent="Accessing Reddit threads",
  37. username=getenv("REDDIT_USERNAME"),
  38. passkey=passkey,
  39. check_for_async=False,
  40. )
  41. """
  42. Ask user for subreddit input
  43. """
  44. print_step("Getting subreddit threads...")
  45. if not getenv(
  46. "SUBREDDIT"
  47. ): # note to self. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
  48. subreddit = reddit.subreddit(
  49. input("What subreddit would you like to pull from? ")
  50. ) # if the env isnt set, ask user
  51. else:
  52. print_substep(f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config")
  53. subreddit = reddit.subreddit(
  54. getenv("SUBREDDIT")
  55. ) # Allows you to specify in .env. Done for automation purposes.
  56.  
  57. if getenv("POST_ID"):
  58. submission = reddit.submission(id=getenv("POST_ID"))
  59. else:
  60. threads = subreddit.hot(limit=25)
  61. submission = get_subreddit_undone(threads, subreddit)
  62. submission = check_done(submission) # double checking
  63. if submission is None:
  64. return get_subreddit_threads() # submission already done. rerun
  65. upvotes = submission.score
  66. ratio = submission.upvote_ratio * 100
  67. num_comments = submission.num_comments
  68.  
  69. print_substep(f"Video will be: {submission.title} :thumbsup:", style="bold green")
  70. print_substep(f"Thread has {upvotes} upvotes", style="bold blue")
  71. print_substep(f"Thread has a upvote ratio of {ratio}%", style="bold blue")
  72. print_substep(f"Thread has {num_comments} comments", style="bold blue")
  73. environ["VIDEO_TITLE"] = str(textify(submission.title)) # todo use global instend of env vars
  74. environ["VIDEO_ID"] = str(textify(submission.id))
  75.  
  76. content["thread_url"] = f"https://reddit.com{submission.permalink}"
  77. content["thread_title"] = submission.title
  78. # content["thread_content"] = submission.content
  79. content["comments"7] = []
  80. for top_level_comment in submission.comments:
  81. if isinstance(top_level_comment, MoreComments):
  82. continue
  83. if top_level_comment.body in ["[removed]", "[deleted]"]:
  84. continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78
  85. if not top_level_comment.stickied:
  86. if len(top_level_comment.body) <= int(environ["MAX_COMMENT_LENGTH"]):
  87. content["comments"].append(
  88. {
  89. "comment_body": top_level_comment.body,
  90. "comment_url": top_level_comment.permalink,
  91. "comment_id": top_level_comment.id,
  92. }
  93. )
  94. print_substep("Received subreddit threads Successfully.", style="bold green")
  95. return content
  96.  
Advertisement
Add Comment
Please, Sign In to add comment