|
|
@ -1,6 +1,8 @@
|
|
|
|
#!/usr/bin/env python
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import argparse
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
|
@ -14,6 +16,10 @@ from praw.errors import InvalidUser, InvalidUserPass, RateLimitExceeded, \
|
|
|
|
HTTPException, OAuthAppRequired
|
|
|
|
HTTPException, OAuthAppRequired
|
|
|
|
from praw.objects import Comment, Submission
|
|
|
|
from praw.objects import Comment, Submission
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(stream=sys.stdout)
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
log.setLevel(level=logging.WARNING)
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
from loremipsum import get_sentence # This only works on Python 2
|
|
|
|
from loremipsum import get_sentence # This only works on Python 2
|
|
|
|
except ImportError:
|
|
|
|
except ImportError:
|
|
|
@ -68,22 +74,25 @@ save_directory = config.get('main', 'save_directory')
|
|
|
|
_user = config.get('main', 'username')
|
|
|
|
_user = config.get('main', 'username')
|
|
|
|
_pass = config.get('main', 'password')
|
|
|
|
_pass = config.get('main', 'password')
|
|
|
|
|
|
|
|
|
|
|
|
r = praw.Reddit(user_agent="shreddit/4.0")
|
|
|
|
r = praw.Reddit(user_agent="shreddit/4.1")
|
|
|
|
if save_directory:
|
|
|
|
if save_directory:
|
|
|
|
r.config.store_json_result = True
|
|
|
|
r.config.store_json_result = True
|
|
|
|
|
|
|
|
|
|
|
|
def login(user=None, password=None):
|
|
|
|
if verbose:
|
|
|
|
try:
|
|
|
|
log.setLevel(level=logging.INFO)
|
|
|
|
# This is OAuth 2
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
# Try to login with OAuth2
|
|
|
|
r.refresh_access_information()
|
|
|
|
r.refresh_access_information()
|
|
|
|
if verbose:
|
|
|
|
log.debug("Logged in with OAuth.")
|
|
|
|
print("Logged in with OAuth.")
|
|
|
|
except (HTTPException, OAuthAppRequired) as e:
|
|
|
|
except (HTTPException, OAuthAppRequired) as e:
|
|
|
|
log.warning("You should migrate to OAuth2 using get_secret.py before \
|
|
|
|
|
|
|
|
Reddit disables this login method.")
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
if user and password:
|
|
|
|
|
|
|
|
r.login(_user, _pass)
|
|
|
|
r.login(_user, _pass)
|
|
|
|
else:
|
|
|
|
except InvalidUserPass:
|
|
|
|
r.login() # Let the user supply details
|
|
|
|
r.login() # Supply details on the command line
|
|
|
|
except InvalidUser as e:
|
|
|
|
except InvalidUser as e:
|
|
|
|
raise InvalidUser("User does not exist.", e)
|
|
|
|
raise InvalidUser("User does not exist.", e)
|
|
|
|
except InvalidUserPass as e:
|
|
|
|
except InvalidUserPass as e:
|
|
|
@ -91,22 +100,15 @@ def login(user=None, password=None):
|
|
|
|
except RateLimitExceeded as e:
|
|
|
|
except RateLimitExceeded as e:
|
|
|
|
raise RateLimitExceeded("You're doing that too much.", e)
|
|
|
|
raise RateLimitExceeded("You're doing that too much.", e)
|
|
|
|
|
|
|
|
|
|
|
|
if not r.is_logged_in():
|
|
|
|
log.info("Logged in as {user}.".format(user=r.user))
|
|
|
|
login(user=_user, password=_pass)
|
|
|
|
log.debug("Deleting messages before {time}.".format(
|
|
|
|
|
|
|
|
time=datetime.now() - timedelta(hours=hours)))
|
|
|
|
if verbose:
|
|
|
|
|
|
|
|
print("Logged in as {user}.".format(user=r.user))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
|
|
|
|
print("Deleting messages before {time}.".format(
|
|
|
|
|
|
|
|
time=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 and whitelist:
|
|
|
|
if whitelist:
|
|
|
|
print("Keeping messages from subreddits {subs}".format(
|
|
|
|
log.debug("Keeping messages from subreddits {subs}".format(
|
|
|
|
subs=', '.join(whitelist))
|
|
|
|
subs=', '.join(whitelist))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
@ -151,10 +153,9 @@ def remove_things(things):
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
if trial_run: # Don't do anything, trial mode!
|
|
|
|
if trial_run: # Don't do anything, trial mode!
|
|
|
|
if verbose:
|
|
|
|
|
|
|
|
content = thing
|
|
|
|
content = thing
|
|
|
|
content = str(content).encode('ascii', 'ignore')
|
|
|
|
content = str(content).encode('ascii', 'ignore')
|
|
|
|
print("Would have deleted {thing}: '{content}'".format(
|
|
|
|
log.debug("Would have deleted {thing}: '{content}'".format(
|
|
|
|
thing=thing.id, content=thing))
|
|
|
|
thing=thing.id, content=thing))
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
@ -168,14 +169,12 @@ def remove_things(things):
|
|
|
|
thing.clear_vote()
|
|
|
|
thing.clear_vote()
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(thing, Submission):
|
|
|
|
if isinstance(thing, Submission):
|
|
|
|
if verbose:
|
|
|
|
log.info('Deleting submission: #{id} {url}'.format(
|
|
|
|
print('Deleting submission: #{id} {url}'.format(
|
|
|
|
|
|
|
|
id=thing.id,
|
|
|
|
id=thing.id,
|
|
|
|
url=thing.url.encode('utf-8'))
|
|
|
|
url=thing.url.encode('utf-8'))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif isinstance(thing, Comment):
|
|
|
|
elif isinstance(thing, Comment):
|
|
|
|
replacement_text = get_sentence()
|
|
|
|
replacement_text = get_sentence()
|
|
|
|
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(b'\n\r\t', ' ', thing.body[:78].encode('utf-8')),
|
|
|
|
sub(b'\n\r\t', ' ', thing.body[:78].encode('utf-8')),
|
|
|
@ -183,12 +182,11 @@ def remove_things(things):
|
|
|
|
thing.subreddit
|
|
|
|
thing.subreddit
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if edit_only:
|
|
|
|
if edit_only:
|
|
|
|
print('Editing {msg}'.format(msg=msg))
|
|
|
|
log.info('Editing (not removing) {msg}'.format(msg=msg))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
print('Editing and deleting {msg}'.format(msg=msg))
|
|
|
|
log.info('Editing and deleting {msg}'.format(msg=msg))
|
|
|
|
|
|
|
|
|
|
|
|
thing.edit(replacement_text)
|
|
|
|
thing.edit(replacement_text)
|
|
|
|
removal_count += 1 # rename? this includes edits as well as deletions
|
|
|
|
|
|
|
|
if not edit_only:
|
|
|
|
if not edit_only:
|
|
|
|
thing.delete()
|
|
|
|
thing.delete()
|
|
|
|
removal_count += 1
|
|
|
|
removal_count += 1
|
|
|
|