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 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")