Skip to content

Commit

Permalink
0.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
MXWXZ committed Apr 2, 2019
1 parent 07f0716 commit 121af5e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.2.1 2019-04-02
1. 添加未开放选课处理
2. cookie打印修复

v0.2.0 2019-03-27
1. 支持新版教务系统
2. 支持多线程同步选课
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)**

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion sjtu_automata/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = '[email protected]'
__license__ = 'GNU General Public License v3 (GPLv3)'
Expand Down
50 changes: 28 additions & 22 deletions sjtu_automata/autoelect.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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.
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.1

0 comments on commit 121af5e

Please sign in to comment.