Advertisement
zbeucler

Reddit API PRAW Code

Jan 26th, 2021
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. try:
  2.     import os
  3.     import praw
  4. except ImportError:
  5.     print()
  6.     print("ERROR: Problem importing packages in {}").format(
  7.         os.path.realpath(__file__))
  8.     print("Please try \'pip install -r requirements.txt\' to install all required packages")
  9.     print()
  10.     exit()
  11.  
  12.  
  13. async def getRedditPost(PostType, AmountOfPosts):
  14.     # create reddit read-only instance
  15.     reddit = praw.Reddit(client_id="Client ID",
  16.                          client_secret="Client Secret",
  17.                          user_agent="User Agent")
  18.     # print(reddit.read_only)  # True means authorized
  19.     postContent = []
  20.  
  21.     if PostType == "t":
  22.         for submission in reddit.subreddit("cars").top(limit=AmountOfPosts):
  23.             # postContent.append(submission.title)
  24.             postContent.append(submission.selftext)
  25.     elif PostType == "h":
  26.         for submission in reddit.subreddit("cars").hot(limit=AmountOfPosts):
  27.             # postContent.append(submission.title)
  28.             postContent.append(submission.selftext)
  29.     elif PostType == "n":
  30.         for submission in reddit.subreddit("cars").new(limit=AmountOfPosts):
  31.             # postContent.append(submission.title)
  32.             postContent.append(submission.selftext)
  33.  
  34.     # print(postContent)
  35.     return postContent
  36.  
  37.  
  38. if __name__ == '__main__':
  39.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement