Skip to content

Instantly share code, notes, and snippets.

@t0mm0
Created July 26, 2011 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save t0mm0/1107929 to your computer and use it in GitHub Desktop.
Save t0mm0/1107929 to your computer and use it in GitHub Desktop.
how to log in to otaku-streamers in python (vbulletin)
import cookielib
import md5
import sys
import urllib
import urllib2
username = 'your_username'
password = 'your_password'
login_url = 'http://otaku-streamers.com/community/login.php?do=login'
cookie_file = 'otaku.cookies'
#handy function to build a query string from a dictionary
def build_query(queries):
return '&'.join([k+'='+urllib.quote(str(v)) for (k,v) in queries.items()])
#the onsubmit() javascript does this bit
password_md5 = md5.md5(password).hexdigest()
#build POST data (including hidden form fields)
post_data = build_query({'vb_login_username': username,
's': '',
'securitytoken': 'guest',
'do': 'login',
'vb_login_md5password': password_md5,
'vb_login_md5password_utf': password_md5,
})
#create cookiejar
cj = cookielib.LWPCookieJar()
#tell urllib2 to handle cookies
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
#try to load existing cookies
try:
#ignore_discard=True loads session cookies too
cj.load(cookie_file, ignore_discard=True)
#check to see if login cookies already saved
response = urllib2.urlopen('http://otaku-streamers.com/')
html = response.read()
if 'class="welcomelink"' in html:
print 'already logged in as %s.' % username
#lets get out of here!
sys.exit()
#if cookie file does not exist we just keep going...
except IOError:
pass
print 'logging in...'
#POST to login page
response = urllib2.urlopen(login_url, post_data)
#check for login string
html = response.read()
if 'Thank you for logging in, %s.' % username in html:
print 'logged in to otaku-streamers as %s.' % username
else:
print 'login failed'
#save cookies to disk (ignore_discard=True saves session cookies)
cj.save(cookie_file, ignore_discard=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment