public
Created

how to log in to otaku-streamers in python (vbulletin)

  • Download Gist
otaku-login.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
import cookielib
import md5
import re
import sys
import urllib
import urllib2
 
 
def login(username, password, cookie_file):
'''login to otaku-streamers.com and save session cookies'''
 
login_url = 'http://otaku-streamers.com/community/login.php?do=login'
 
#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)
 
print 'cookies loaded, checking if they are still valid...'
#check to see if login cookies still valid
response = urllib2.urlopen('http://otaku-streamers.com/community/faq.php')
html = response.read()
 
if 'class="welcomelink"' in html:
print 'already logged in as %s.' % username
#lets get out of here!
return True
 
#if cookie file does not exist we just keep going...
except IOError:
pass
 
print 'logging in...'
 
#the onsubmit() javascript does this bit
password_md5 = md5.md5(password).hexdigest()
 
#build POST data (including hidden form fields)
post_data = urllib.urlencode({'vb_login_username': username,
's': '',
'securitytoken': 'guest',
'do': 'login',
'vb_login_md5password': password_md5,
'vb_login_md5password_utf': password_md5,
})
 
#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
 
#save cookies to disk (ignore_discard=True saves session cookies)
cj.save(cookie_file, ignore_discard=True)
return True
else:
print 'login failed'
return False
 
 
def resolve(web_url):
'''return the video stream link for the given web page'''
#grab the page (we already installed our cookie processing opener
#so no need to do it again)
response = urllib2.urlopen(web_url)
html = response.read()
#find the video url
video_url = re.findall('"file", "(http.+?video.mp4")', html)
return video_url[0]
 
#if called from command line....
if __name__ == '__main__':
#get username and password from command line eg.
#python otaku-login.py my_username my_password /path/to/otaku.cookies
username = sys.argv[1]
password = sys.argv[2]
cookie_file = sys.argv[3]
 
#do the login
success = login(username, password, cookie_file)
#resolve a link
url='http://otaku-streamers.com/watch/1955/ep1-Bartender'
print resolve(url)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.