forked from dairoot/school-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustom_handle.py
More file actions
56 lines (42 loc) · 1.47 KB
/
custom_handle.py
File metadata and controls
56 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from school_api import SchoolClient
from school_api.exceptions import SchoolException, LoginException, IdentityException
school = SchoolClient('http://61.142.33.204', use_ex_handle=False)
def service_resp():
def decorator(func):
def warpper(*args, **kwargs):
try:
client = school.user_login(*args, **kwargs)
data = func(client)
except IdentityException as reqe:
# 账号密码错误
return {'error': reqe}
except LoginException as reqe:
# 登录失败 (包含以上错误)
return {'error': reqe}
except SchoolException as reqe:
# 请求失败 (包含以上错误)
return {'error': reqe}
except Exception:
# 模块报错 (包含以上错误)
return {'error': '教务请求失败'}
else:
# 数据处理 (如:缓存)
return data
return warpper
return decorator
@service_resp()
def bind_account(user):
return user.get_info()
@service_resp()
def get_schedule(user):
return user.get_schedule()
@service_resp()
def get_score(user):
return user.get_score()
if __name__ == '__main__':
account = 'user'
password = 'password'
data = bind_account(account, password, use_cookie_login=False)
print(data)