Double tabs? Who thought of that!

pull/11/head
David Trail 11 years ago
parent aac66354b2
commit 6f452c32dc

@ -5,10 +5,11 @@ from praw.objects import Comment, Submission
from datetime import datetime, timedelta from datetime import datetime, timedelta
from re import sub from re import sub
from time import sleep from time import sleep
try: try:
from loremipsum import get_sentence from loremipsum import get_sentence
except: except:
get_sentence = lambda: '''I have been Shreddited for privacy!\n\nhttps://github.com/x89/Shreddit/''' get_sentence = lambda: '''I have been Shreddited for privacy!\n\nhttps://github.com/x89/Shreddit/'''
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config', help="config file to use instead of shreddit.cfg") parser.add_argument('-c', '--config', help="config file to use instead of shreddit.cfg")
@ -36,75 +37,74 @@ _pass = config.get('main', 'password')
r = praw.Reddit(user_agent="Shreddit-PRAW 2.1") r = praw.Reddit(user_agent="Shreddit-PRAW 2.1")
if _user and _pass: if _user and _pass:
try: try:
r.login(_user, _pass) r.login(_user, _pass)
except: except:
# Try again (often get HTTP when Reddit is down for a second errors) # Try again (often get HTTP when Reddit is down for a second errors)
sleep(10) sleep(10)
r.login(_user, _pass) r.login(_user, _pass)
else: else:
r.login() r.login()
if verbose: if verbose:
print "Logged in as {0}".format(r.user) print "Logged in as {0}".format(r.user)
if verbose: if verbose:
print "Deleting messages before {0}.".format(datetime.now() - timedelta(hours=hours)) print "Deleting messages before {0}.".format(datetime.now() - timedelta(hours=hours))
whitelist = [y.strip().lower() for y in whitelist.split(',')] whitelist = [y.strip().lower() for y in whitelist.split(',')]
whitelist_ids = [y.strip().lower() for y in whitelist_ids.split(',')] whitelist_ids = [y.strip().lower() for y in whitelist_ids.split(',')]
if verbose: if verbose:
print "Keeping messages from subreddits {0}".format(', '.join(whitelist)) print "Keeping messages from subreddits {0}".format(', '.join(whitelist))
things = [] things = []
if item == "comments": if item == "comments":
things = r.user.get_comments(limit=None, sort=sort) things = r.user.get_comments(limit=None, sort=sort)
elif item == "submitted": elif item == "submitted":
things = r.user.get_submitted(limit=None, sort=sort) things = r.user.get_submitted(limit=None, sort=sort)
elif item == "overview": elif item == "overview":
things = r.user.get_overview(limit=None, sort=sort) things = r.user.get_overview(limit=None, sort=sort)
else: else:
raise Exception("Your deletion section is wrong") raise Exception("Your deletion section is wrong")
for thing in things: for thing in things:
thing_time = datetime.fromtimestamp(thing.created_utc) thing_time = datetime.fromtimestamp(thing.created_utc)
# Delete things after after_time # Delete things after after_time
after_time = datetime.utcnow() - timedelta(hours=hours) after_time = datetime.utcnow() - timedelta(hours=hours)
if thing_time > after_time: if thing_time > after_time:
continue continue
# For edit_only we're assuming that the hours aren't altered. # For edit_only we're assuming that the hours aren't altered.
# This saves time when deleting (you don't edit already edited posts). # This saves time when deleting (you don't edit already edited posts).
if edit_only: if edit_only:
end_time = after_time - timedelta(hours=hours) end_time = after_time - timedelta(hours=hours)
if thing_time < end_time: if thing_time < end_time:
continue continue
if str(thing.subreddit).lower() in whitelist or \ if str(thing.subreddit).lower() in whitelist or \
thing.id in whitelist_ids: thing.id in whitelist_ids:
continue continue
if not trial_run: if not trial_run:
if clear_vote: if clear_vote:
thing.clear_vote() thing.clear_vote()
if isinstance(thing, Submission): if isinstance(thing, Submission):
if verbose: if verbose:
print u'Deleting submission: #{0} {1}'.format(thing.id, thing.url) print u'Deleting submission: #{0} {1}'.format(thing.id, thing.url)
elif isinstance(thing, Comment): elif isinstance(thing, Comment):
replacement_text = get_sentence() replacement_text = get_sentence()
if verbose: if verbose:
msg = '/r/{3}/ #{0} with:\n\t"{1}" to\n\t"{2}"'.format( msg = '/r/{3}/ #{0} with:\n\t"{1}" to\n\t"{2}"'.format(
thing.id, thing.id,
sub(r'[\t\r\n]', ' ', thing.body.encode('ascii', 'ignore')[:78]), sub(r'[\t\r\n]', ' ', thing.body.encode('ascii', 'ignore')[:78]),
replacement_text[:78], replacement_text[:78],
thing.subreddit thing.subreddit
) )
if edit_only: if edit_only:
print 'Editing ' + msg print 'Editing ' + msg
else: else:
print 'Editing and deleting ' + msg print 'Editing and deleting ' + msg
thing.edit(replacement_text) thing.edit(replacement_text)
if not edit_only: if not edit_only:
thing.delete() thing.delete()

Loading…
Cancel
Save