Created
July 26, 2011 20:26
-
-
Save t0mm0/1107929 to your computer and use it in GitHub Desktop.
how to log in to otaku-streamers in python (vbulletin)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment