Created
November 8, 2015 14:53
-
-
Save w1ndy/320e484e319c5ab015c5 to your computer and use it in GitHub Desktop.
Shandong University Course Selector
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
#!/usr/bin/python | |
# required libraries: requests beautifulsoup4 | |
# please fill in target_class, user, pwd | |
import requests | |
import random | |
import multiprocessing | |
from bs4 import BeautifulSoup | |
from time import sleep, ctime | |
target_class = [ | |
['0000000000', '000'] | |
] | |
user = '' | |
pwd = '' | |
interval = 1.5 | |
abort_on_first_success = False | |
login_url = 'http://jwxt.sdu.edu.cn:7890/pls/wwwbks/bks_login2.login?jym2005=' | |
req_url = 'http://jwxt.sdu.edu.cn:7890/pls/wwwbks/xk.CourseInput?jym2005=' | |
def login(user, pwd): | |
nocache_url = login_url + str(random.random() * 10000) | |
data = {'stuid': user, 'pwd': pwd} | |
r = requests.post(nocache_url, data=data, allow_redirects=False) | |
if r.status_code == 200: | |
soup = BeautifulSoup(r.text, 'html.parser') | |
print 'Login Error:', soup.get_text() | |
return None | |
return r.cookies | |
def makeReq(pipe, account, class_id, class_subid): | |
nocache_url = req_url + str(random.random() * 10000) | |
data = {'p_qxrxk': class_id, 'p_qxrxk_kxh': class_subid} | |
cookies = {'ACCOUNT': account} | |
print ctime(), 'Selecting %s::%s' % (class_id, class_subid) | |
r = requests.post(nocache_url, data=data, allow_redirects=False, cookies=cookies) | |
if r.status_code != 200: | |
print 'Error server returned code', r.status_code | |
pipe.send(False) | |
pipe.close() | |
return | |
if class_id in r.text and class_subid in r.text: | |
print 'Class %s::%s has been selected.' % (class_id, class_subid) | |
pipe.send(True) | |
pipe.close() | |
return | |
soup = BeautifulSoup(r.text, 'html.parser') | |
ret = soup.find_all('span', class_='t') | |
if ret: | |
print 'Selection for %s::%s failed:' % (class_id, class_subid) | |
print ret[0].get_text(); | |
else: | |
print 'Server not responding' | |
pipe.send(False) | |
pipe.close() | |
return | |
def main(): | |
arr = [False] * len(target_class) | |
cookies = login(user, pwd) | |
if cookies: | |
account = cookies['ACCOUNT'] | |
while not all(arr): | |
for i, p in enumerate(target_class): | |
if not arr[i]: | |
try: | |
retpp, retpc = multiprocessing.Pipe() | |
proc = multiprocessing.Process(target=makeReq, args=(retpc, account, p[0], p[1],)) | |
proc.start() | |
proc.join(10) | |
if proc.is_alive(): | |
print 'The request takes too long, terminating...' | |
proc.terminate() | |
proc.join() | |
print 'Request terminated' | |
else: | |
arr[i] = retpp.recv() | |
if arr[i] != False and abort_on_first_success: | |
return | |
except Exception as e: | |
print 'Exception Caught:', e | |
finally: | |
sleep(interval) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment