Guest User

Untitled

a guest
Mar 11th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. import json
  2. import praw
  3.  
  4. reddit = praw.Reddit(
  5. client_id='',
  6. client_secret='',
  7. password='',
  8. user_agent='',
  9. username=''
  10. )
  11.  
  12.  
  13. def get_comment_body():
  14. threads = get_subreddit_id(sub)
  15.  
  16. for thread in threads:
  17. submission = reddit.submission(id=thread)
  18. submission.comments.replace_more(limit=0)
  19.  
  20. for comment in submission.comments.list():
  21. with open('subreddits.txt', 'a') as outfile:
  22. outfile.write(comment.body)
  23.  
  24.  
  25. def get_subreddit_id(sub):
  26. submissions = []
  27.  
  28. for submit in reddit.subreddit(sub).hot(limit=25):
  29. submissions.append(submit.id)
  30.  
  31. return submissions
  32.  
  33.  
  34. def get_user_comments(redditor, tfile=False):
  35. for comment in reddit.redditor(redditor).comments.new(limit=None):
  36.  
  37. if tfile:
  38. with open('usercomments.txt', 'a') as outfile:
  39. outfile.write(comment.body)
  40.  
  41. else:
  42. data = {comment.id: comment.body}
  43. with open('comment.json', 'a') as outfile:
  44. json.dump(data, outfile)
  45.  
  46.  
  47. def subreddit_to_json():
  48. threads = get_subreddit_id(sub)
  49.  
  50. for thread in threads:
  51. submission = reddit.submission(id=thread)
  52. submission.comments.replace_more(limit=0)
  53. comments = submission.comments.list()
  54. data = [{comment.id: comment.body} for comment in comments]
  55.  
  56. with open('data.json', 'a') as outfile:
  57. json.dump(data, outfile)
  58.  
  59. sub = 'learnpython'
  60. name = 'danceprometheus'
  61.  
  62. get_comment_body()
  63. get_user_comments(name)
  64. subreddit_to_json()
Add Comment
Please, Sign In to add comment