diff --git a/kill.py b/kill.py index 46db45e..a5a3e3f 100755 --- a/kill.py +++ b/kill.py @@ -1,21 +1,67 @@ #!/usr/bin/env python2 +import json, sys, httplib, urllib from datetime import datetime, timedelta -from json import loads -import sys +from time import sleep -if len(sys.argv) < 2: - raise Exception("Need an amount of keep-days of which to save your comments.") - -days = int(sys.argv[1]) +## Get the data we need to log into the API +f = open('user.json', 'r') +data = json.load(f) +f.close() -before_time = datetime.now() - timedelta(days=days) +days = data['days'] +user = data['user'] +passwd = data['passwd'] +## Load our json which should be all the user's history f = open('data.json', 'r') -data = loads(f.read()) +data = json.load(f) f.close() +# Every thing before this time will be deleted +before_time = datetime.now() - timedelta(days=days) + +## Fill an array of IDs that are to be deleted +deletion_ids = [] for d in data: - date = datetime.fromtimestamp(d['date']) + date = datetime.fromtimestamp(d['created']) if date < before_time: - delete_post(d['id']) + deletion_ids.append(d) + +if len(deletion_ids) == 0: + print "Couldn't find any posts to delete" + exit(0) + + +## This part logs you in. +headers = {"Content-type": "application/x-www-form-urlencoded"} +conn = httplib.HTTPSConnection('ssl.reddit.com') +params = urllib.urlencode({ + 'user': user, + 'passwd': passwd, + 'api_type': 'json'}) +conn.request("POST", "/api/login/%s" % user, params, headers) +http = conn.getresponse() +tmp = json.loads(http.read())['json']['data'] +headers.update({'Cookie': 'reddit_session=%s' % tmp['cookie']}) +modhash = tmp['modhash'] + +print '# Headers: %s' % headers + +for dat in deletion_ids: + print u'''{time} {subreddit}: {text}...'''.format( + subreddit = dat['subreddit'], + id = dat['id'], + time = datetime.fromtimestamp(dat['created']).date(), + text = dat[u'body'][:20]) + # And now for the deleting + conn = httplib.HTTPConnection('www.reddit.com') + params = urllib.urlencode({ + 'id': dat['id'], + 'uh': modhash, + 'api_type': 'json'}) + conn.request('POST', '/api/del', params, headers) + http = conn.getresponse() + print http.getheaders(), http.read() + break + #sleep(2)