Guest User

Updated reddit comments downloader for /r/rational

a guest
Dec 26th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python2.7
  2. #-*- coding: Utf-8 -*-
  3.  
  4. import json
  5. import urllib2
  6. import codecs
  7. import time
  8.  
  9.  
  10. def start(url):
  11.  
  12.   compage = urllib2.urlopen(url).read()
  13.   x = json.loads(compage)['data']['children']
  14.   comcount = len(x) - 1
  15.   comfile = open("%s.txt"%username, 'a')
  16.   for i in range(0,comcount):
  17.       ##"no more then 30 requests per minute"
  18.       time.sleep(60/30)
  19.  
  20.       rawdata = x[i]['data']['body']
  21.  
  22.       comments = rawdata.encode('utf-8')
  23.       print(comments+"\n\n\n\n")
  24.  
  25.       comfile.write(comments)
  26.  
  27.   nextpageurl = "http://www.reddit.com/user/%s/comments/.json?count=25&after=%s" %(username, x[i]['data']['name'])
  28.   comfile.close()
  29.  
  30.   if comcount >= 24:
  31.     start(nextpageurl)
  32.   else:
  33.     print "Either finished or messed up somewhere. Check the txt file"
  34.  
  35. global username
  36. username =      raw_input("Username:")
  37. comurl = "http://www.reddit.com/user/%s/comments/.json" %username
  38. start(comurl)
Add Comment
Please, Sign In to add comment