Guest User

reddit-user-comments.py

a guest
May 19th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import praw
  4. import sys
  5. import time
  6.  
  7. comment_limit = None
  8.  
  9. def info(*args, **kwargs):
  10.     print(*args, file=sys.stderr, **kwargs)
  11.  
  12.  
  13. ################################################################################
  14.  
  15. if len(sys.argv) < 3:
  16.     raise Exception('Usage: %s USERNAME OUTPUT_FILE' % sys.argv[0])
  17.  
  18. redditor_name = sys.argv[1]
  19. output_file   = sys.argv[2]
  20.  
  21. # set up API objects
  22. info('fetching comments by ' + redditor_name)
  23. r = praw.Reddit(user_agent='reddit-user-comments.py')
  24. user = r.get_redditor(redditor_name)
  25. comments = user.get_comments(limit=comment_limit)
  26.  
  27. # fetch data from Reddit
  28. comment_bodies = []
  29. for comment in comments:
  30.     comment_bodies.append(['___\n***#',
  31.                            ': [%s](%s)***\n\n%s\n___\n' % (time.strftime('%Y-%m-%d %H:%M:%S UTC',
  32.                                                                          time.gmtime(comment.created_utc)),
  33.                                                            comment.permalink,
  34.                                                            comment.body.strip())])
  35.     info('\rdownloaded %d comments' % len(comment_bodies), end='')
  36. info('')
  37.  
  38. # write output file
  39. info('writing ' + output_file)
  40. with open(output_file, 'w') as out:
  41.     for n, body in enumerate(reversed(comment_bodies)):
  42.         print(body[0] + str(n+1) + body[1], file=out)
  43.  
  44. info('done!')
Add Comment
Please, Sign In to add comment