OAuth 2 login support. see get_secret.py and praw.ini.example

pull/38/head
David Trail 10 years ago
parent f016d95783
commit 2e9beb1d91

@ -10,7 +10,8 @@ from simpleconfigparser import simpleconfigparser
from datetime import datetime, timedelta from datetime import datetime, timedelta
import praw import praw
from praw.errors import InvalidUser, InvalidUserPass, RateLimitExceeded from praw.errors import InvalidUser, InvalidUserPass, RateLimitExceeded, \
HTTPException
from praw.objects import Comment, Submission from praw.objects import Comment, Submission
try: try:
@ -67,11 +68,17 @@ 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/3.3") r = praw.Reddit(user_agent="shreddit/4.0")
if save_directory: if save_directory:
r.config.store_json_result = True r.config.store_json_result = True
def login(user=None, password=None): def login(user=None, password=None):
try:
# This is OAuth 2
r.refresh_access_information()
if verbose:
print("Logged in with OAuth.")
except HTTPException:
try: try:
if user and password: if user and password:
r.login(_user, _pass) r.login(_user, _pass)
@ -88,7 +95,7 @@ if not r.is_logged_in():
login(user=_user, password=_pass) login(user=_user, password=_pass)
if verbose: if verbose:
print("Logged in as {user}".format(user=r.user)) print("Logged in as {user}.".format(user=r.user))
if verbose: if verbose:
print("Deleting messages before {time}.".format( print("Deleting messages before {time}.".format(
@ -103,16 +110,17 @@ if verbose and whitelist:
subs=', '.join(whitelist)) subs=', '.join(whitelist))
) )
things = [] def get_things(after=None):
if item == "comments": if item == "comments":
things = r.user.get_comments(limit=None, sort=sort) return r.user.get_comments(limit=None, sort=sort)
elif item == "submitted": elif item == "submitted":
things = r.user.get_submitted(limit=None, sort=sort) return r.user.get_submitted(limit=None, sort=sort)
elif item == "overview": elif item == "overview":
things = r.user.get_overview(limit=None, sort=sort) return r.user.get_overview(limit=None, sort=sort)
else: else:
raise Exception("Your deletion section is wrong") raise Exception("Your deletion section is wrong")
def remove_things(things):
for thing in things: for thing in things:
# Seems to be in users's timezone. Unclear. # Seems to be in users's timezone. Unclear.
thing_time = datetime.fromtimestamp(thing.created_utc) thing_time = datetime.fromtimestamp(thing.created_utc)
@ -178,3 +186,7 @@ for thing in things:
thing.edit(replacement_text) thing.edit(replacement_text)
if not edit_only: if not edit_only:
thing.delete() thing.delete()
things = get_things()
remove_things(things)

Loading…
Cancel
Save