Thank you lovely

nopraw
David Trail 13 years ago
parent 6ff39d8535
commit cad8605fe5

@ -24,7 +24,6 @@ while True:
after = reddit['data']['after'] after = reddit['data']['after']
children = reddit['data']['children'] children = reddit['data']['children']
# This bit fills datum with the id (for removal) and the date (for saving recent posts) # This bit fills datum with the id (for removal) and the date (for saving recent posts)
for child in children: for child in children:
child_data = child['data'] child_data = child['data']

@ -1,5 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
from __future__ import with_statement
try: import json try: import json
except ImportError: import simplejson as json except ImportError: import simplejson as json
import sys, httplib, urllib import sys, httplib, urllib
@ -16,25 +17,19 @@ user = data['user']
passwd = data['passwd'] passwd = data['passwd']
## Load our json which should be all the user's history ## Load our json which should be all the user's history
f = open('data.json', 'r') with open('data.json', 'r') as f:
data = json.load(f) data = json.load(f)
f.close()
# Every thing before this time will be deleted # Every thing before this time will be deleted
before_time = datetime.now() - timedelta(days=days) before_time = datetime.now() - timedelta(days=days)
## Fill an array of IDs that are to be deleted ## Fill an array of IDs that are to be deleted
deletion_ids = [] deletion_ids = [item for item in data if datetime.fromtimestamp(item['created']) < before_time]
for d in data:
date = datetime.fromtimestamp(d['created'])
if date < before_time:
deletion_ids.append(d)
if len(deletion_ids) == 0: if len(deletion_ids) == 0:
print "Couldn't find any posts to delete" print "Couldn't find any posts to delete"
exit(0) exit(0)
## This part logs you in. ## This part logs you in.
headers = { headers = {
"Content-type": "application/x-www-form-urlencoded", "Content-type": "application/x-www-form-urlencoded",
@ -53,21 +48,21 @@ headers.update({'Cookie': 'reddit_session=%s' % tmp['cookie']})
modhash = tmp['modhash'] modhash = tmp['modhash']
for dat in deletion_ids: for dat in deletion_ids:
id = dat['id'] rid = dat['id']
time = datetime.fromtimestamp(dat['created']).date() time = datetime.fromtimestamp(dat['created']).date()
subreddit = dat['subreddit'] subreddit = dat['subreddit']
text = dat[u'body'][:20] text = dat[u'body'][:20]
#print '{id}: {time} {subreddit}: "{text}..."'.format(subreddit=subreddit, id=id, time=time, text=text) #print '{rid}: {time} {subreddit}: "{text}..."'.format(subreddit=subreddit, rid=rid, time=time, text=text)
# And now for the deleting # And now for the deleting
conn = httplib.HTTPConnection('www.reddit.com') conn = httplib.HTTPConnection('www.reddit.com')
params = urllib.urlencode({ params = urllib.urlencode({
'id': id, 'id': rid,
'uh': modhash, 'uh': modhash,
'api_type': 'json'}) 'api_type': 'json'})
#headers.update({"Content-Length": len(params)}) #headers.update({"Content-Length": len(params)})
conn.request('POST', '/api/del', params, headers) conn.request('POST', '/api/del', params, headers)
http = conn.getresponse() http = conn.getresponse()
if http.read() != '{}': if http.read() != '{}':
print '''Failed to delete "%s" (%s - %s - %s)''' % (text, id, time, subreddit) print '''Failed to delete "%s" (%s - %s - %s)''' % (text, rid, time, subreddit)
sleep(2) sleep(2)

Loading…
Cancel
Save