diff --git a/shreddit.py b/shreddit.py index c6644d5..a27fcc7 100755 --- a/shreddit.py +++ b/shreddit.py @@ -1,6 +1,9 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 -import praw, argparse, ConfigParser +import praw +from praw.errors import * +import argparse +from simpleconfigparser import simpleconfigparser from praw.objects import Comment, Submission from datetime import datetime, timedelta from re import sub @@ -9,13 +12,19 @@ from time import sleep try: from loremipsum import get_sentence except: - get_sentence = lambda: '''I have been Shreddited for privacy!\n\nhttps://github.com/x89/Shreddit/''' + def get_sentence(): + return '''I have been Shreddited for privacy!\n\n\ + https://github.com/x89/Shreddit/''' 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 the default shreddit.cfg" +) args = parser.parse_args() -config = ConfigParser.RawConfigParser() +config = simpleconfigparser() if args.config: config.read(args.config) else: @@ -34,29 +43,39 @@ item = config.get('main', 'item') _user = config.get('main', 'username') _pass = config.get('main', 'password') -r = praw.Reddit(user_agent="Shreddit-PRAW 2.1") +r = praw.Reddit(user_agent="Shreddit-PRAW 3") -if _user and _pass: + +def login(user=None, password=None): try: - r.login(_user, _pass) - except: - # Try again (often get HTTP when Reddit is down for a second errors) - sleep(10) - r.login(_user, _pass) -else: - r.login() + if user and password: + r.login(_user, _pass) + else: + r.login() # Let the user supply details + except InvalidUserPass as e: + raise InvalidUserPass(e) + except RateLimitExceeded: + raise RateLimitExceeded() + except NonExistentUser: + raise NonExistentUser("User does not exist") + +login(user=_user, password=_pass) if verbose: - print "Logged in as {0}".format(r.user) + print("Logged in as {user}".format(user=r.user)) if verbose: - print "Deleting messages before {0}.".format(datetime.now() - timedelta(hours=hours)) + print("Deleting messages before {time}.".format( + time=datetime.now() - timedelta(hours=hours)) + ) whitelist = [y.strip().lower() for y in whitelist.split(',')] whitelist_ids = [y.strip().lower() for y in whitelist_ids.split(',')] -if verbose: - print "Keeping messages from subreddits {0}".format(', '.join(whitelist)) +if verbose and whitelist: + print("Keeping messages from subreddits {subs}".format( + subs=', '.join(whitelist)) + ) things = [] if item == "comments": @@ -67,14 +86,13 @@ elif item == "overview": things = r.user.get_overview(limit=None, sort=sort) else: raise Exception("Your deletion section is wrong") - + for thing in things: thing_time = datetime.fromtimestamp(thing.created_utc) # Delete things after after_time after_time = datetime.utcnow() - timedelta(hours=hours) if thing_time > after_time: continue - # For edit_only we're assuming that the hours aren't altered. # This saves time when deleting (you don't edit already edited posts). if edit_only: @@ -91,20 +109,23 @@ for thing in things: thing.clear_vote() if isinstance(thing, Submission): if verbose: - print u'Deleting submission: #{0} {1}'.format(thing.id, thing.url) + print(u'Deleting submission: #{id} {url}'.format( + id=thing.id, + url=thing.url) + ) elif isinstance(thing, Comment): replacement_text = get_sentence() if verbose: msg = '/r/{3}/ #{0} with:\n\t"{1}" to\n\t"{2}"'.format( thing.id, - sub(r'[\t\r\n]', ' ', thing.body.encode('ascii', 'ignore')[:78]), + sub(r'\W', ' ', thing.body.encode('ascii', 'ignore')[:78]), replacement_text[:78], thing.subreddit ) if edit_only: - print 'Editing ' + msg + print('Editing {msg}'.format(msg=msg)) else: - print 'Editing and deleting ' + msg + print('Editing and deleting {msg}'.format(msg=msg)) thing.edit(replacement_text) if not edit_only: thing.delete()