Guest User

reddit-user-comments

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