diff --git a/CHANGELOG b/CHANGELOG index ea52ba5..17b98d9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +v0.2.1 2019-04-02 +1. 添加未开放选课处理 +2. cookie打印修复 + v0.2.0 2019-03-27 1. 支持新版教务系统 2. 支持多线程同步选课 diff --git a/README.md b/README.md index 2713894..4b2f0ce 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # sjtu-automata -![Version](https://img.shields.io/badge/Version-0.2.0-blue.svg) ![Language](https://img.shields.io/badge/Language-Python3-red.svg) ![License](https://img.shields.io/badge/License-GPL--3.0-yellow.svg) +![Version](https://img.shields.io/badge/Version-0.2.1-blue.svg) ![Language](https://img.shields.io/badge/Language-Python3-red.svg) ![License](https://img.shields.io/badge/License-GPL--3.0-yellow.svg) **注意!此版本为BETA版,未经过严格测试,可能存在BUG,如有问题请提交[issue](https://github.com/MXWXZ/AutoElect/issues)** @@ -57,15 +57,15 @@ Ubuntu 18.04: ### CLI 使用:`autoelect [OPTIONS] [CLASSTYPE-CLASSID]` -|参数|长参数形式|说明| -|:--:|:--:|:--:| -|-v|--version|显示版本| -||--no-update|关闭更新检查| -|-o|--ocr|使用OCR识别验证码| -||--print-cookie|打印登陆cookie| -|-d|--delay|两次尝试选课间隔(默认1s)| -|-n|--number|每个课程的线程数(默认为1)| -|-h|--help|显示帮助| +| 参数 | 长参数形式 | 说明 | +| :---: | :------------: | :-------------------------: | +| -v | --version | 显示版本 | +| | --no-update | 关闭更新检查 | +| -o | --ocr | 使用OCR识别验证码 | +| | --print-cookie | 打印登陆cookie | +| -d | --delay | 两次尝试选课间隔(默认1s) | +| -n | --number | 每个课程的线程数(默认为1) | +| -h | --help | 显示帮助 | - `CLASSTYPE`和`CLASSID`成对出现,可以出现多对同步进行,但至少有一对 - `CLASSTYPE`:主修课为0,通识课为1,通选课为2(更多需要请提交issue) diff --git a/sjtu_automata/__version__.py b/sjtu_automata/__version__.py index 91219d3..9051f97 100644 --- a/sjtu_automata/__version__.py +++ b/sjtu_automata/__version__.py @@ -2,7 +2,7 @@ __description__ = 'Auto elect script for SJTUer.' __url__ = 'https://github.com/MXWXZ/sjtu-automata' __update_url__ = 'https://raw.githubusercontent.com/MXWXZ/sjtu-automata/master/version' -__version__ = '0.2.0' +__version__ = '0.2.1' __author__ = 'MXWXZ' __author_email__ = 'me@imwxz.com' __license__ = 'GNU General Public License v3 (GPLv3)' diff --git a/sjtu_automata/autoelect.py b/sjtu_automata/autoelect.py index d5a6373..73a040b 100644 --- a/sjtu_automata/autoelect.py +++ b/sjtu_automata/autoelect.py @@ -1,3 +1,8 @@ +from sjtu_automata.electsys.automata import ( + get_studentid, get_params, elect_class) +from sjtu_automata.credential import login +from sjtu_automata.__version__ import __author__, __url__, __version__ +from sjtu_automata import check_update, echoerror, echoinfo, echowarning import getopt import sys from time import sleep @@ -8,12 +13,6 @@ sys.path.append('../') -from sjtu_automata import check_update, echoerror, echoinfo, echowarning -from sjtu_automata.__version__ import __author__, __url__, __version__ -from sjtu_automata.credential import login -from sjtu_automata.electsys.automata import ( - get_studentid, get_params, elect_class) - class UserInterface(object): def __init__(self): @@ -26,18 +25,19 @@ def __init__(self): self.tl = [] # thread lock self.tclass = [] # thread classid self.id = 0 # thread id - self.glock = threading.Lock() # global lock + self.glock = threading.Lock() # global lock def print_cookie(self): echoinfo('Your cookie:') echo(('; '.join( ['='.join(item) for item in self.session.cookies.items()])).replace('"', '')) - def login(self, ocr): + def login(self, ocr, delay): """Login user. Args: ocr: bool, True to use ocr + delay: int, retry delay Returns: true for success. @@ -51,22 +51,25 @@ def login(self, ocr): echoerror('Can\'t find student id!') return False - self.params = get_params(self.session, self.studentid) - if not self.params['xkkz_id'] or not self.params['xkkz_id'][0] or not self.params['xkkz_id'][1] or not self.params['xkkz_id'][2]: - echoerror('Can\'t find xkkzid!') - return False + while 1: + self.params = get_params(self.session, self.studentid) + if self.params['xkkz_id'][0] and self.params['xkkz_id'][1] and self.params['xkkz_id'][2] and self.params['njdm_id'] and self.params['zyh_id']: + break + echoinfo('Not open, retry in %d seconds...' % delay) + sleep(delay) echoinfo('Login successful!') return True def __elect_thread(self, tid, classtype, classid, delay): while self.status[tid] == 2 or self.status[tid] == 4 or self.status[tid] == -1: - ret = elect_class(self.session, self.studentid, self.params, classtype, classid) + ret = elect_class(self.session, self.studentid, + self.params, classtype, classid) with self.tl[tid]: if self.status[tid] != 0 and self.status[tid] != 1 and self.status[tid] != 3: self.status[tid] = ret if ret == 0 or ret == 1 or ret == 3: - self.__parse_status(tid,ret) + self.__parse_status(tid, ret) break sleep(delay) @@ -85,7 +88,7 @@ def start_elect(self): i.daemon = True i.start() - def __parse_status(self,tid,status): + def __parse_status(self, tid, status): with self.glock: if status == -1: secho('['+self.tclass[tid]+'] ', fg='red', nl=False) @@ -109,17 +112,17 @@ def __parse_status(self,tid,status): def fetch_status(self): echoinfo('Current status:') for i, j in enumerate(self.status): - self.__parse_status(i,j) + self.__parse_status(i, j) def get_input(self): while True: - cmd=input() + cmd = input() if cmd == 's': self.fetch_status() def check_alive(self): for i in self.status: - if i==-1 or i==2 or i==4: + if i == -1 or i == 2 or i == 4: return True return False @@ -145,10 +148,11 @@ def print_version(ctx, param, value): @click.option('-o', '--ocr', is_flag=True, help='use OCR to auto fill captcha') @click.option('--print-cookie', is_flag=True, help='print the cookie for advanced use') @click.option('-d', '--delay', default=1, type=int, help='delay seconds between attempts, default is 1') +@click.option('-c', '--check-delay', default=3, type=int, help='delay seconds for open elect check, default is 3') @click.option('-n', '--number', default=1, type=int, help='thread number per class, default is 1') # argument @click.argument('classtypeid', required=True, nargs=-1) -def cli(no_update, ocr, print_cookie, delay, number, classtypeid): +def cli(no_update, ocr, print_cookie, delay, check_delay, number, classtypeid): version = __version__ echo('AutoElect by '+__author__) echo('Version: '+version) @@ -166,15 +170,17 @@ def cli(no_update, ocr, print_cookie, delay, number, classtypeid): exit() ui = UserInterface() - ui.login(ocr) + ui.login(ocr, check_delay) + if print_cookie: + ui.print_cookie() for i in range(0, len(classtypeid), 2): if not (0 <= int(classtypeid[i]) <= 2): echowarning('Class type ' + classtypeid[i] + ' invalid! Ignore.') continue ui.add_elect(number, int(classtypeid[i]), classtypeid[i+1], delay) ui.start_elect() - cmd=threading.Thread(target=ui.get_input) - cmd.daemon=True + cmd = threading.Thread(target=ui.get_input) + cmd.daemon = True cmd.start() while ui.check_alive(): sleep(0.5) diff --git a/version b/version index 341cf11..7dff5b8 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.2.0 \ No newline at end of file +0.2.1 \ No newline at end of file