Guest User

Reddit comments save script

a guest
Sep 22nd, 2011
526
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. #-*- coding: Utf-8 -*-
  3.  
  4. import json
  5. import urllib2
  6. import codecs
  7.  
  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.  
  16.   comfile = open("%s.txt"%username, 'a')
  17.   for i in range(0,comcount):
  18.    
  19.       rawdata = "Thread Title: %s\nUrl: http://www.reddit.com/r/%s/%s\n\nComment:\n%s\n\n~------------------------------------------~ \n\n"  %(x[i]['data']['link_title'], x[i]['data']['subreddit'], x[i]['data']['link_id'].replace("t3_","").replace("t1_",""), x[i]['data']['body'])
  20.      
  21.       comments = rawdata.encode('utf-8')
  22.       print comments
  23.    
  24.       comfile.write(comments)
  25.  
  26.   nextpageurl = "http://www.reddit.com/user/%s/comments/.json?count=25&after=%s" %(username, x[i]['data']['name'])
  27.   comfile.close()
  28.    
  29.   if comcount >= 24:
  30.     start(nextpageurl)
  31.   else:
  32.     print "Either finished or messed up somewhere. Check the txt file"
  33.  
  34. global username
  35. username =  raw_input("Username:")
  36. comurl = "http://www.reddit.com/user/%s/comments/.json" %username
  37. start(comurl)
RAW Paste Data