Skip to content

Commit

Permalink
Add crude http digest auth support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Logan Hanks committed Jul 18, 2011
1 parent 31479fc commit afb1066
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
16 changes: 13 additions & 3 deletions flairsync.py
Expand Up @@ -2,6 +2,7 @@

import argparse
import csv
import getpass
import htmlentitydefs
import re
import sys
Expand All @@ -18,6 +19,9 @@ def parse_args():

parser.add_argument('--help', action='help', default=argparse.SUPPRESS,
help='show this help message and exit')
parser.add_argument('-A', '--http_auth', default=False, const=True,
action='store_const',
help='set if HTTP basic authentication is needed')
parser.add_argument('-b', '--batch_size', type=int, default=100,
help='number of users to read at a time from the site')
parser.add_argument('-c', '--cookie_file',
Expand All @@ -29,8 +33,14 @@ def parse_args():
def ynprompt(prompt):
return raw_input(prompt).lower().startswith('y')

def log_in(host, cookie_file):
client = redditclient.RedditClient(host, cookie_file)
def log_in(host, cookie_file, use_http_auth):
if use_http_auth:
http_user = raw_input('HTTP auth username: ')
http_password = getpass.getpass('HTTP auth password: ')
options = dict(http_user=http_user, http_password=http_password)
else:
options = {}
client = redditclient.RedditClient(host, cookie_file, **options)
while not client.log_in():
print 'login failed'
return client
Expand Down Expand Up @@ -88,7 +98,7 @@ def main():
csv_flair = flair_from_csv(config.csvfile)

print 'Connecting to %s ...' % config.host
client = log_in(config.host, config.cookie_file)
client = log_in(config.host, config.cookie_file, config.http_auth)

print 'Fetching current flair from site ...'
reddit_flair = flair_from_reddit(client, config.subreddit,
Expand Down
30 changes: 23 additions & 7 deletions redditclient.py
Expand Up @@ -5,11 +5,18 @@
import urllib2

class RedditClient:
def __init__(self, host='http://reddit.com', cookie_file=None):
def __init__(self, host='http://reddit.com', cookie_file=None,
http_user=None, http_password=None):
while host.endswith('/'):
host = host[:-1]
self.host = host

self.password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
if http_user:
self.password_manager.add_password(None, self.host,
http_user, http_password)
self.auth_handler = urllib2.HTTPDigestAuthHandler(self.password_manager)

if cookie_file:
self.cookies = cookielib.LWPCookieJar(cookie_file)
try:
Expand Down Expand Up @@ -44,18 +51,27 @@ def _request(self, method, url, **data):
data = None
req = urllib2.Request(url, data)
self.cookies.add_cookie_header(req)
resp = urllib2.urlopen(req)
opener = urllib2.build_opener(self.auth_handler)
resp = opener.open(req)
self.cookies.extract_cookies(resp, req)
return json.load(resp)

def log_in(self):
for cookie in self.cookies:
if cookie.name == 'reddit_session':
return True
user = raw_input('reddit username: ')
password = getpass.getpass('reddit password: ')
response = self._post(self._url('/api/login'),
user=user, passwd=password)
logged_in = True
break
else:
logged_in = False

if not logged_in:
user = raw_input('reddit username: ')
password = getpass.getpass('reddit password: ')
response = self._post(self._url('/api/login'),
user=user, passwd=password)
else:
response = self._get(self._url('/api/me'))

for cookie in self.cookies:
if cookie.name == 'reddit_session':
try:
Expand Down

0 comments on commit afb1066

Please sign in to comment.