Esempio n. 1
0
    def get_grade(self):
        """获取本学期的成绩

        :raise: :class:`snnusdk.exceptions.UnauthorizedError`
        :rtype: list of dict
        :return: 参照例子

        >>> u.get_grade()
        [
            {
                '课程号': '1243432', 
                '课序号': '01', 
                '课程名': '高级数据结构', 
                '英文课程名': 'Advanced Data Structures', 
                '学分': '2', 
                '课程属性': '任选', 
                '课堂最高分': '', 
                '课堂最低分': '', 
                '课堂平均分': '', 
                '成绩': '', 
                '名次': '', 
                '未通过原因': ''
            },
            ...
        ]

        """
        if self.verify == False:
            raise UnauthorizedError('您还没有登录!')
        soup = self.get_soup(self.URLs.GRADE)
        table = soup.find(name='table', attrs={'class': 'titleTop2'})
        table_list = table_to_list(table)
        return table_list[1:]
Esempio n. 2
0
def GetIpList():
    '''
    从代理网站库上获取代理ip
    '''
    ip_list = []
    r = requests.get('http://ip.jiangxianli.com/')
    soup = BeautifulSoup(r.text, 'lxml')
    table = soup.find(name='table', attrs={
                      "class": "table table-hover table-bordered table-striped"})
    ls = table_to_list(table)
    for dic in ls:
        ip_list.append(dic['IP'] + ':' + dic['端口'])
    return ip_list[:6]
Esempio n. 3
0
    def get_list(self):
        """查询消费明细

        :rtype: dict
        :return: 参见例子

        >>> c = get_list()
        {
            'success': True, 
            'msg': '查询成功',
            'result': 
            [
                {
                    '卡号': '201608735', 
                    '时间': '2018-12-5 22:48:10', 
                    '次数': '2760', 
                    '原金额': '98.09', 
                    '交易额': '30.70', 
                    '卡余额': '67.39', 
                    '记录信息': '99CB399', 
                    '备注': ''
                }, 
                ...
            ]
        }
        """
        ret = {}
        try:
            r = requests.post(self.URLs.CONSUMPTION, data=self.data)
            r.encoding = 'utf-8'
            soup = BeautifulSoup(r.text, 'lxml')
            ls = table_to_list(
                soup.find(name='table', attrs={'class': 'hovertable'}))
            ret['success'] = True
            ret['msg'] = '查询成功'
            ret['result'] = ls
        except AttributeError:
            ret['success'] = False
            ret['msg'] = '请核对校园卡号'
            ret['result'] = []
        except ConnectionError:
            ret['success'] = False
            ret['msg'] = '网络连接失败'
            ret['result'] = []
        except Exception:
            ret['success'] = False
            ret['msg'] = '未知错误'
            ret['result'] = []
        finally:
            return ret
Esempio n. 4
0
def get_borrow_info():
    """预约到馆信息

    :rtype: dict
    :return: 预约到馆信息的字典

    >>> get_borrow_info()
    {
        'success': True, 
        'msg': '查询成功',
        'result': [
            {
                '预约者': '张三', 
                '书名': 'C语言程序设计', 
                '著者': '李四', 
                '保留结束日期': '2018-12-06', 
                '单册分馆': '长安西密集库', 
                '取书地点': '雁塔总服务台'
            },
            ...
            ]
    }
    """
    url = 'http://opac.snnu.edu.cn:8991/F?func=file&file_name=hold_shelf'
    ret = {}
    try:
        r = requests.get(url)
        r.encoding = 'utf-8'
        soup = BeautifulSoup(r.text, 'lxml')
        table = soup.find(name='table', attrs={'summary': 'Script output'})
        ls = table_to_list(table)
        ret['success'] = True
        ret['result'] = ls
        ret['msg']='查询成功'
    except Exception as e:
        ret['success'] = False
        ret['result'] = []
        ret['msg']=e.message
    finally:
        return ret
Esempio n. 5
0
    def get_all_grades(self, year, semester):
        """获取指定学期的已及格成绩

        :param str year: 学年 格式为 "2017-2018"
        :param int semester: 学期 数字1或2
        :raise: :class:`snnusdk.exceptions.UnauthorizedError`
        :raise: :class:`snnusdk.exceptions.YearNotExistError`
        :rtype: list
        :return: 参照例子

        >>> u.get_all_grades(year='2017-2018', semester=1)
        [
            {
                '课程号':'01111',
                '课序号': '62', 
                '课程名': '大学外语(一)', 
                '英文课程名': 'College English 1', 
                '学分': '3', 
                '课程属性': '必修', 
                '成绩': '73.0'
            },
            ...
        ]
        """
        if self.verify == False:
            raise UnauthorizedError('您还没有登录!')
        year_list = self.get_grade_year_list()
        year_set = set()
        for year_item in year_list:
            year_set.add(
                re.search(r'\d{4,4}-\d{4,4}', year_item, re.S).group(0))
        if year not in year_set or semester not in [1, 2]:
            raise YearNotExistError('不存在该学期')

        key = "{}学年{}(两学期)".format(year, '春' if semester == 2 else '秋')
        tables = self.get_soup(self.URLs.ALL_GRADE).find_all(
            name='table', attrs={'class': 'displayTag'})
        table = tables[year_list.index(key)]
        table_list = table_to_list(table)
        return table_list
Esempio n. 6
0
    def get_gpa(self):
        """计算绩点

        :raise: :class:`snnusdk.exceptions.UnauthorizedError`
        :rtype: double
        :return: 只计算必修课后的绩点

        >>> u.get_gpa()
        73.00
        """
        if self.verify == False:
            raise UnauthorizedError('您还没有登录!')
        ret = 0.0
        num = 0.0
        tables = self.get_soup(self.URLs.ALL_GRADE).find_all(
            name='table', attrs={'class': 'displayTag'})
        for table in tables:
            table_list = table_to_list(table)
            for dic in table_list:
                if dic['课程属性'] == '必修':
                    num += float(dic['学分'])
                    ret += float(dic['学分']) * float(dic['成绩'])
        return round(ret / num, 2)
Esempio n. 7
0
    def get_courses(self):
        """获取本学期的选课情况

        :raise: :class:`snnusdk.exceptions.UnauthorizedError`
        :rtype: list of dict
        :return:  参照例子

        >>> urp.get_courses()
        [
            {
                'id': '1241416', 
                'name': '算法设计与分析', 
                'number': '01', 
                'credits': 3.0, 
                'attributes': '必修', 
                'teacher': '王小明*', 
                'status': '置入', 
                'info': [
                            {
                                'week': '1-18周上', 
                                'day': '2', 
                                'timeOfClass': '1', 
                                'numOfClass': '2', 
                                'campus': '长安校区', 
                                'buildings': '长安文津楼', 
                                'room': '1511'
                            }
                        ]
            }
        ]
        """
        if self.verify == False:
            raise UnauthorizedError('您还没有登录!')
        soup = self.get_soup(method='get', url=self.URLs.SELECTED_COURSES)
        tables = soup.findAll("table", attrs={'id': "user"})
        table = tables[1]
        table_list = table_to_list(table, remove_index_list=[8,
                                                             9])  # 0,6虽不用,不可写
        courses = []
        temp_dic = {}
        keys = ['周次', '星期', '节次', '节数', '校区', '教学楼', '教室']
        for dic in table_list:
            dic_len = len(dic)
            if dic_len > 9:
                courses.append({
                    'id': dic['课程号'],
                    'name': dic['课程名'],
                    'number': dic['课序号'],
                    'credits': float(dic['学分']),
                    'attributes': dic['课程属性'],
                    'teacher': dic['教师'],
                    'status': dic['选课状态'],
                    'info': []
                })
            else:
                dic = dict(zip(keys, [dic[key] for key in dic.keys()]))
            for key in dic.keys():
                if key in keys:
                    temp_dic = {
                        'week': dic['周次'],
                        'day': dic['星期'],
                        'timeOfClass': dic['节次'],
                        'numOfClass': dic['节数'],
                        'campus': dic['校区'],
                        'buildings': dic['教学楼'],
                        'room': dic['教室']
                    }

            courses[-1]['info'].append(temp_dic)
            temp_dic = {}
        return courses
Esempio n. 8
0
    def get_old_courses(self, year, semester):
        """获取指定学期的课表

        :param str year: 学年 格式为 "2017-2018"
        :param int semester: 学期 数字1或2
        :raise: :class:`snnusdk.exceptions.UnauthorizedError`
        :raise: :class:`snnusdk.exceptions.YearNotExistError`
        :rtype: list of dict
        :return: 参照例子

        >>> u.get_old_courses(year='2017-2018', semester=1)
        [
            {
                'id': '1241416', 
                'name': '算法设计与分析', 
                'number': '01', 
                'credits': 3.0, 
                'attributes': '必修', 
                'teacher': '王小明*', 
                'status': '置入', 
                'info': [
                            {
                                'week': '1-18周上', 
                                'day': '2', 
                                'timeOfClass': '1', 
                                'numOfClass': '2', 
                                'campus': '长安校区', 
                                'buildings': '长安文津楼', 
                                'room': '1511'
                            }
                        ]
            }
        ]
        """
        if self.verify == False:
            raise UnauthorizedError('您还没有登录!')
        soup = self.get_soup(self.URLs.OLD_COURSES)
        year_list = [i.get('value') for i in soup.find_all(name='option')]
        key = "{}-{}-1".format(year, semester)
        if key not in year_list:
            raise YearNotExistError('不存在该学期!')

        soup = self.get_soup(self.URLs.OLD_COURSES,
                             'post',
                             data={'zxjxjhh': key})
        table = soup.find_all(name='table', attrs={'id': 'user'})[1]
        table_list = table_to_list(table, remove_index_list=[8])

        courses = []
        temp_dic = {}
        keys = ['周次', '星期', '节次', '节数', '校区', '教学楼', '教室']
        for dic in table_list:
            dic_len = len(dic)
            if dic_len > 7:
                courses.append({
                    'id': dic['课程号'],
                    'name': dic['课程名'],
                    'number': dic['课序'],
                    'credits': float(dic['学分']),
                    'attributes': dic['课程属性'],
                    'teacher': dic['教师'],
                    'status': dic['选课状态'],
                    'info': []
                })
            else:
                dic = dict(zip(keys, [dic[key] for key in dic.keys()]))
            for key in dic.keys():
                if key in keys:
                    temp_dic = {
                        'week': dic['周次'],
                        'day': dic['星期'],
                        'timeOfClass': dic['节次'],
                        'numOfClass': dic['节数'],
                        'campus': dic['校区'],
                        'buildings': dic['教学楼'],
                        'room': dic['教室']
                    }

            courses[-1]['info'].append(temp_dic)
            temp_dic = {}
        return courses