def available_data(request: HttpRequest) -> HttpResponse: openID = request.GET['openid'] queryStudent = MongoConn().db.get_collection(mongoCollections[0]).find_one( {'openID': openID}, ) resp = [] if queryStudent == None: resp = [] else: studentID = queryStudent['studentID'] password = queryStudent['password'] queryDate = MongoConn().db.get_collection( mongoCollections[4]).find_one({'studentID': studentID}, {'_id': False}) if queryDate != None: for key in queryDate: if key != 'studentID': resp.append(key) resp.sort(reverse=True) # 降序排列 # 第一次访问 else: spiderTest = auto_login(studentID, password, openID) spiderTest.getCookie() spiderTest.logon() classTableQueryTest = auto_query_classTable(spiderTest.cookies) DateList = classTableQueryTest.getDateList() if studentID[0:2].isdigit(): startYear = '20' + studentID[0:2] startIndex = 0 for index, Date in enumerate(DateList): if startYear in Date: startIndex = index if startYear in DateList[index + 1]: startIndex += 1 break resp = DateList[0:startIndex + 1] else: resp = DateList mongodata = {"studentID": studentID} # 设置原始dateList for Date in resp: mongodata[Date] = False MongoConn().db.get_collection(mongoCollections[4]).update_one( {"studentID": studentID}, {"$set": mongodata}, upsert=True) return HttpResponse(json.dumps(resp), content_type="application/json;charset=utf-8")
def grade(request: HttpRequest) -> HttpResponse: openid = request.GET['openid'] queryStudentID = MongoConn().db.get_collection( mongoCollections[0]).find_one({'openID': openid}, { '_id': False, 'studentID': True }) if queryStudentID != None: studentID = queryStudentID['studentID'] data = list(MongoConn().db.get_collection(mongoCollections[2]).find( { '学号': studentID }, mongoFilter).sort("开学时间", -1) # --降序 ) if data != []: resp = {"status": "correct", "data": data} # 考虑还未开始抓取 else: password = MongoConn().db.get_collection( mongoCollections[0]).find_one({'openID': openid}, { '_id': False, 'password': True })['password'] spiderTest = auto_login(studentID, password, openid) spiderTest.getCookie() spiderTest.logon() queryTest = auto_query_grade(spiderTest.cookies) queryTest.getDateList() queryTest.dellAllSemester(studentID) data = list(MongoConn().db.get_collection( mongoCollections[2]).find({ '学号': studentID }, mongoFilter).sort("开学时间", -1) # --降序 ) resp = {"status": "correct", "data": data} # 考虑未绑定或绑定错误 else: resp = {"status": "notBinding", "data": []} return HttpResponse(json.dumps(resp), content_type="application/json;charset=utf-8")
class Updater: """ 该类实现成绩类的自动更新 """ def __init__(self, cycle_s=300): """ :param cycle_s: 定义扫描周期,默认5min(300s) """ self.collectionID = MongoConn().db.get_collection(mongoCollections[0]) def _get_all_id(self): """ 获取所有的有效ID :return: """ self.collectionID.find()
def withdrawBinding(request: HttpRequest) -> HttpResponse: if request.method == "POST": # 保存请求数据 openid = request.POST['openid'] if MongoConn().db.get_collection(mongoCollections[0]).delete_one({ "openID": openid }).deleted_count < 1: resp = {'status': 'notFind'} else: resp = {'status': 'correct'} return HttpResponse(json.dumps(resp), content_type="application/json")
def index(request: HttpRequest) -> HttpResponse: openid = request.GET['openid'] # print(openid) isBinding = MongoConn().db.get_collection(mongoCollections[0]).find_one( {'openID': openid}) if isBinding != None: resp = {'isBinding': True, 'status': isBinding['studentID']} return HttpResponse(json.dumps(resp), content_type="application/json") else: resp = {'isBinding': False, 'status': ''} return HttpResponse(json.dumps(resp), content_type="application/json")
def get(request: HttpRequest) -> HttpResponse: openID = request.GET['openid'] semester = request.GET['semester'] week = int(request.GET['week']) queryStudent = MongoConn().db.get_collection(mongoCollections[0]).find_one( {'openID': openID}, ) resp = [] if queryStudent == None: resp = [] else: studentID = queryStudent['studentID'] quertDate = MongoConn().db.get_collection( mongoCollections[4]).find_one({'studentID': studentID}) password = queryStudent['password'] # 第一次访问该学期 print('字典:', quertDate.__class__) if quertDate is None or \ semester not in dict(quertDate).keys(): # 条件是不存在任何数据,或则该学期数据 spiderTest = auto_login(studentID, password, openID) spiderTest.getCookie() spiderTest.logon() classTableQueryTest = auto_query_classTable(spiderTest.cookies) classTableQueryTest.getDateList() classTableQueryTest.getOneSemester(semester, studentID) #更新学期 MongoConn().db.get_collection(mongoCollections[4]).update_one( {"studentID": studentID}, {"$set": { semester: True }}, upsert=True) collection = MongoConn().db.get_collection(mongoCollections[3]) filter = { 'semester': semester, 'studentID': studentID, 'startWeek': { '$lte': week }, 'endWeek': { "$gte": week }, } if week % 2 == 1: filter['isOdd'] = True else: filter['isEven'] = True classTable = list(collection.find(filter, {'_id': False})) for classItem in classTable: respItem = {} respItem['day'] = classItem['day'] respItem['startNum'] = classItem['startNum'] respItem[ 'continued'] = classItem['endNum'] - classItem['startNum'] + 1 respItem['extraMessage'] = { 'sameClass': classItem['sameClass'], 'extra': ' '.join(classItem['extraMessage']), } message = { 'name': classItem['name'], 'location': classItem['location'], 'teacher': classItem['teacher'], } respItem['message'] = message resp.append(respItem) return HttpResponse(json.dumps(resp), content_type="application/json;charset=utf-8")
def gpa(request: HttpRequest) -> HttpResponse: openid = request.GET['openID'] queryStudentID = MongoConn().db.get_collection( mongoCollections[0]).find_one({'openID': openid}, { '_id': False, 'studentID': True }) if queryStudentID != None: studentID = queryStudentID['studentID'] data = list(MongoConn().db.get_collection(mongoCollections[2]).find( { '学号': studentID }, mongoFilter).sort("开学时间", 1) # --升序 ) # 考虑还未开始抓取 if data == []: password = MongoConn().db.get_collection( mongoCollections[0]).find_one({'openID': openid}, { '_id': False, 'password': True })['password'] spiderTest = auto_login(studentID, password, openid) spiderTest.getCookie() spiderTest.logon() queryTest = auto_query_grade(spiderTest.cookies) queryTest.getDateList() queryTest.dellAllSemester(studentID) # TODO 考虑未满60 data = list(MongoConn().db.get_collection( mongoCollections[2]).find( { '学号': studentID, '总成绩': { '$gte': '60' } }, mongoFilter).sort("开学时间", 1) # --升序 ) # 对数据进行处理(绩点计算) currentSemester = data[0]['开学时间'] resp = {'gpa': {}, 'credit': {}, 'GPA': 0} creditList = [] # 记录一学期学分 gradeList = [] # 记录一学期成绩 for index, item in enumerate(data): if item['开学时间'] != currentSemester or index == len(data) - 1: if index == len(data) - 1: creditList.append(float(item['学分'])) gradeList.append(float(item['总成绩'])) resp['credit'][currentSemester] = sum(creditList) resp['gpa'][currentSemester] = sum([ creditList[index] * gradeList[index] for index in range(len(creditList)) ]) / resp['credit'][currentSemester] currentSemester = item['开学时间'] creditList = [] gradeList = [] creditList.append(float(item['学分'])) gradeList.append(float(item['总成绩'])) resp['GPA'] = sum([resp['gpa'][key] * resp['credit'][key] for key in [key for key,value in resp['credit'].items()]]) / \ sum(resp['credit'].values()) # 四舍五入 resp['GPA'] = round((resp['GPA'] - 50) / 10, 2) for key, value in resp['gpa'].items(): resp['gpa'][key] = round((value - 50) / 10, 2) return HttpResponse(json.dumps(resp), content_type="application/json;charset=utf-8")
def __init__(self, cycle_s=300): """ :param cycle_s: 定义扫描周期,默认5min(300s) """ self.collectionID = MongoConn().db.get_collection(mongoCollections[0])
def __init__(self): MongoConn.__init__(self)