Skip to main content

Magic Island Technologies auto sign in script

Since MIT will automatically log you out after 3 hours of use, I created this script to fix that. To use it, make sure you have python installed. Then customize the username and password, replacing the "blah" and "pass" in quotes with your actual username and password.

text/x-python auto_sign_in.py — 3 KB

File contents

import time, HTMLParser, urllib2, urllib
from datetime import datetime
from threading import Timer

sign_in_url = "https://vbc.mitglobalnet.net/index.php"
ping_page = "http://www.nathanvangheem.com/give_me_something.html"
redirect_page = "http://welcome.mit/".rstrip('/')
last_opened_page = '__saved_last_opened.html'
user_agent = "	Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5"
username = "blah"
password = "pass"
interval_to_check = 70 #amount of time in seconds to check if you're logged in

post_vars = {
    'passwd' : password,
    'username' : username,
    'remember' : 'no'
}

def print_time():
    n = datetime.now()
    print n.ctime(),

class MyParser(HTMLParser.HTMLParser):
    "A simple parser class."
    
    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.input_tags = {}
    
    def get_attr(self, name, attrs):
        for n, v in attrs:
            if n == name:
                return v
                
        return None
    
    def handle_starttag(self, tag, attrs):
        if tag == 'input':
            name = self.get_attr('name', attrs)
            value = self.get_attr('value', attrs)
            self.input_tags[name] = value

    def parse(self, s):
        "Parse the given string 's'."
        self.feed(s)
        self.close()

def write_to_file(page_data):
    fi = open(last_opened_page, 'w')
    fi.write(page_data)
    fi.close()

def count_down(sec):
    
    while sec > 0:
        print str(sec),
        time.sleep(1)
        sec -= 1

def get_page(url, data=None, prev_headers=None):
    headers = {
        'User-Agent' : user_agent,
        'Accept' : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
        'Accept-Language' : "en,fr;q=0.7,en-us;q=0.3",
        'Accept-Charset' : "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
        'Keep-Alive' : '300'
    }
    
    if prev_headers:
        # extract my session id by loading a page from the site
        cookie = prev_headers.getheader("Set-Cookie").split(' ')[0]
        headers["Cookie"] = cookie

    if data:
        req = urllib2.Request(url, urllib.urlencode(data), headers=headers)
    else:
        req = urllib2.Request(url, headers=headers)
        
    
    try_again = True
    page = None
    while try_again:
        try:
            page = urllib2.urlopen(req)
        except urllib2.HTTPError, e:
            print e
        except urllib2.URLError, e:
            print 'lost connection, trying again in.. ',
            count_down(5)
        else:
            try_again = False
        
    page.raw_html = page.read()
    write_to_file(page.raw_html)
    return page

def get_parser(page):
    parser = MyParser()
    parser.parse(page.raw_html)
    return parser

def check(reconnect_count):
    print_time()
    page = get_page(ping_page)
    
    if page.url.rstrip('/') != ping_page:
        #got redirected, let's log in...
        print '--not logged in... logging in.',
        page = get_page(sign_in_url)
        parser = get_parser(page)
        
        input_tags = parser.input_tags
        input_tags.update(post_vars)
        
        login_page = get_page(sign_in_url, input_tags, page.headers)
        
        reconnect_count += 1
        print '--reconnected %s times' % reconnect_count
        
        if "Log out" not in login_page.raw_html:
            print "logging in did NOT work!"
    else:
        print 'logged in.'
    
    Timer(float(interval_to_check), check, [reconnect_count]).start()

check(0)