1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import absolute_import, unicode_literals |
3
|
|
|
|
4
|
|
|
from bs4 import BeautifulSoup |
5
|
|
|
from requests import RequestException, TooManyRedirects |
6
|
|
|
from school_api.client.api.base import BaseSchoolApi |
7
|
|
|
from school_api.exceptions import UserInfoException |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class UserlInfo(BaseSchoolApi): |
11
|
|
|
''' 用户信息查询 部门教师不可用 ''' |
12
|
|
|
|
13
|
|
|
def get_info(self, **kwargs): |
14
|
|
|
''' 用户信息 获取入口 ''' |
15
|
|
|
info_url = self.school_url['INFO_URL'] + self.user.account |
16
|
|
|
|
17
|
|
|
try: |
18
|
|
|
res = self._get(info_url, **kwargs) |
19
|
|
|
except TooManyRedirects: |
20
|
|
|
raise UserInfoException(self.school_code, '用户信息接口已关闭') |
21
|
|
|
except RequestException: |
22
|
|
|
raise UserInfoException(self.school_code, '获取用户信息失败') |
23
|
|
|
|
24
|
|
|
return UserlInfoParse(self.school_code, self.user.user_type, res.text).user_info |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class UserlInfoParse(): |
28
|
|
|
''' 信息页面解析模块 ''' |
29
|
|
|
|
30
|
|
|
def __init__(self, school_code, user_type, html): |
31
|
|
|
self.data = {} |
32
|
|
|
self.school_code = school_code |
33
|
|
|
self.soup = BeautifulSoup(html, "html.parser") |
34
|
|
|
[self._html_parse_of_student, self._html_parse_of_teacher][user_type]() |
35
|
|
|
|
36
|
|
|
def _html_parse_of_student(self): |
37
|
|
|
table = self.soup.find("table", {"class": "formlist"}) |
38
|
|
|
if not table: |
39
|
|
|
raise UserInfoException(self.school_code, '获取学生用户信息失败') |
40
|
|
|
|
41
|
|
|
real_name = table.find(id="xm").text |
42
|
|
|
grade = table.find(id="lbl_dqszj").text |
43
|
|
|
class_name = table.find(id="lbl_xzb").text |
44
|
|
|
faculty = table.find(id="lbl_xy").text |
45
|
|
|
specialty = table.find(id="lbl_zymc").text |
46
|
|
|
enrol_time = table.find(id="lbl_rxrq").text |
47
|
|
|
education_level = table.find(id="lbl_CC").text |
48
|
|
|
eductional_systme = table.find(id="lbl_xz").text |
49
|
|
|
|
50
|
|
|
sfzh = table.find(id="lbl_sfzh") |
51
|
|
|
id_card = sfzh.text if sfzh else table.find(id="sfzh")['value'] |
52
|
|
|
|
53
|
|
|
csrq = table.find(id="lbl_csrq") |
54
|
|
|
birth_date = csrq.text if csrq else table.find(id="csrq")['value'] |
55
|
|
|
|
56
|
|
|
lydq = table.find(id="lbl_lydq") |
57
|
|
|
hometown = lydq.text if lydq else table.find(id="lydq")['value'] |
58
|
|
|
|
59
|
|
|
xb = table.find(id='XB') |
60
|
|
|
sex = xb.find('option', attrs={'selected': 'selected'}).text if xb else table.find(id="lbl_xb").text |
61
|
|
|
|
62
|
|
|
self.data = { |
63
|
|
|
"real_name": real_name, |
64
|
|
|
"sex": sex, |
65
|
|
|
"grade": grade, |
66
|
|
|
"birth_date": None if birth_date == 'NULL' else birth_date.replace('/', '-'), |
67
|
|
|
"class_name": class_name, |
68
|
|
|
"faculty": faculty, |
69
|
|
|
"specialty": specialty, |
70
|
|
|
"hometown": hometown, |
71
|
|
|
"enrol_time": enrol_time.replace('/', '-'), |
72
|
|
|
"education_level": education_level, |
73
|
|
|
"eductional_systme": eductional_systme, |
74
|
|
|
"id_card": id_card |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
def _html_parse_of_teacher(self): |
78
|
|
|
table = self.soup.find(id="Table3") |
79
|
|
|
if not table: |
80
|
|
|
raise UserInfoException(self.school_code, '获取教师用户信息失败') |
81
|
|
|
|
82
|
|
|
real_name = table.find(id='xm').text |
83
|
|
|
sex = table.find(id='xb').text |
84
|
|
|
dept = table.find(id='bm').text |
85
|
|
|
position = table.find(id='zw').text |
86
|
|
|
associate_degree = table.find(id='xl').text |
87
|
|
|
positional_title = table.find(id='zc').text |
88
|
|
|
self.data = { |
89
|
|
|
"real_name": real_name, |
90
|
|
|
"sex": sex, |
91
|
|
|
"dept": dept, |
92
|
|
|
"position": position, |
93
|
|
|
"associate_degree": associate_degree, |
94
|
|
|
"positional_title": positional_title |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
@property |
98
|
|
|
def user_info(self): |
99
|
|
|
''' 返回用户信息json格式 ''' |
100
|
|
|
return self.data |
101
|
|
|
|