-
-
Save legnaleurc/5797659 to your computer and use it in GitHub Desktop.
NCU sign in/out
This file contains hidden or 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
#! /usr/bin/env python | |
import cookielib | |
import sys | |
import urllib | |
from BeautifulSoup import BeautifulSoup | |
import mechanize | |
class Bot(object): | |
def __init__(self, id_, pw, action): | |
self._id = id_ | |
self._pw = pw | |
self._action = action | |
self._url = 'http://140.115.182.62/PartTime/parttime.php/{0}'.format(action) | |
# Browser | |
self._br = mechanize.Browser() | |
# Cookie Jar | |
cj = cookielib.LWPCookieJar() | |
self._br.set_cookiejar(cj) | |
# Browser options | |
self._br.set_handle_equiv(True) | |
self._br.set_handle_redirect(True) | |
self._br.set_handle_referer(True) | |
self._br.set_handle_robots(False) | |
# Follows refresh 0 but not hangs on refresh > 0 | |
self._br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) | |
# User-Agent (this is cheating, ok?) | |
self._br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] | |
def _getProject(self, soup, pid): | |
projects = soup.findAll('input', attrs={'type': 'radio'}) | |
project = projects[pid] | |
msg = unicode(project) | |
items = msg.strip().split(' ') | |
num = items[4].strip().split('\"') | |
return num[1] | |
def _login(self): | |
self._br.open(self._url) | |
self._br.select_form(nr=0) | |
self._br.form['j_username'] = self._id | |
self._br.form['j_password'] = self._pw | |
return self._br.submit() | |
def _doAction(self, pid): | |
link = self._login() | |
content = link.read() | |
link.close() | |
soup = BeautifulSoup(content) | |
project = self._getProject(soup, pid) | |
param = { | |
self._action: project, | |
'submit': '%E9%80%81%E5%87%BA', | |
} | |
data = urllib.urlencode(param) | |
self._br.open(self._url, data) | |
def __call__(self, pid=0): | |
self._doAction(pid) | |
if __name__ == '__main__': | |
action = sys.argv[1] | |
id_ = sys.argv[2] | |
pw = sys.argv[3] | |
if len(sys.argv) > 4: | |
pid = int(sys.argv[4]) | |
else: | |
pid = None | |
bot = Bot(id_, pw, action) | |
if action == 'signin' and pid is not None: | |
bot(pid) | |
elif action == 'signout': | |
bot() | |
else: | |
sys.exit(1) | |
# ex: ts=4 sts=4 sw=4 et |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment