def query_empty_room(room_id, year, semester): """/user/empty-room/info Query the room course table In this function, without use cache_ap_query use webap_crawler.query and use GUEST account. Args: room_id ([str]): After get from room_list year ([str]): 107 108. semester ([str]): semester 1,2... Returns: [str]: result type is json """ cache_redis_name = "room_coursetable_{room_id}_{year}_{semester}".format( room_id=room_id, year=year, semester=semester) if red_string.exists(cache_redis_name): return red_string.get(cache_redis_name) login_status = login(username=config.AP_GUEST_ACCOUNT, password=config.AP_GUEST_PASSWORD) if login_status == error_code.CACHE_WENAP_LOGIN_SUCCESS: session = get_session() # load guest cookie session.cookies = pickle.loads( red_bin.get('webap_cookie_%s' % config.AP_GUEST_ACCOUNT)) yms_data = "{year}#{semester}".format(year=year, semester=semester) query_res = webap_crawler.query(session=session, qid='ag302_02', room_id=room_id, yms_yms=yms_data) if query_res == False: return error_code.QUERY_EMPTY_ROOM_ERROR elif isinstance(query_res, requests.models.Response): room_coursetable_data = json.dumps(parse.query_room( query_res.text)) if len(room_coursetable_data) < 160: # avoid null data save in redis. return error_code.CACHE_WEBAP_ERROR red_string.set(name=cache_redis_name, value=room_coursetable_data, ex=config.CACHE_SEMESTERS_EXPIRE_TIME) return room_coursetable_data else: return error_code.CACHE_WEBAP_ERROR return error_code.CACHE_WEBAP_ERROR
def cache_ap_query(username, qid, expire_time=config.CACHE_WEBAP_QUERY_DEFAULT_EXPIRE_TIME, **kwargs): """cache query save html cache to redis Args: username ([str]): use to get redis cache or set. qid ([str]): NKUST query url qrgs expire_time ([int]): Defaults to config.CACHE_WEBAP_QUERY_DEFAULT_EXPIRE_TIME. kwargs: (e.g.) cache_ap_query(username, qid='ag008', yms='107,2', arg01='107', arg02='2') post data will = { 'yms':'107,2', 'arg01':'107', 'arg02':'2' } Returns: [str]: html. [bool]: something erorr False. """ if not red_bin.exists('webap_cookie_%s' % username): return error_code.CACHE_AP_QUERY_COOKIE_ERROR # webap_query_1105133333_ag008_107,2_... redis_name = "webap_query_{username}_{qid}".format( username=username, qid=qid) + '_'.join(map(str, kwargs.values())) # return cache (html) if red_string.exists(redis_name): return red_string.get(redis_name) # load redis cookie session = get_session() session.cookies = pickle.loads(red_bin.get('webap_cookie_%s' % username)) res = webap_crawler.query(session=session, qid=qid, **kwargs) if res != False: if res.status_code == 200: red_string.set(name=redis_name, value=res.text, ex=expire_time) return res.text return False
def coursetable(username, year, semester): """After use webap_login_cache_required. Retrun course table. This function not save html in redis, is json(str). Because parse course table is too ...( ;u; ) Args: username ([str]): NKUST webap username year ([str]): 107 108 .. term year semester ([str]): semester Returns: [str]: coursetable_data, json (str) in any error [int]: COURSETABLE_PARSE_ERROR COURSETABLE_QUERY_ERROR WEBAP_ERROR """ if red_string.exists('coursetable_%s_%s_%s' % (username, year, semester)): return red_string.get('coursetable_%s_%s_%s' % (username, year, semester)) session = get_session() # load webap cookie session.cookies = pickle.loads(red_bin.get('webap_cookie_%s' % username)) query_res = webap_crawler.query(session=session, qid='ag222', arg01=year, arg02=semester) if not query_res: return error_code.COURSETABLE_QUERY_ERROR elif isinstance(query_res, requests.models.Response): res = parse.coursetable(query_res.text) if res is False: return error_code.COURSETABLE_PARSE_ERROR coursetable_data = json.dumps(res) red_string.set(name='coursetable_%s_%s_%s' % (username, year, semester), value=coursetable_data, ex=config.CACHE_COURSETABLE_EXPIRE_TIME) return coursetable_data return error_code.WEBAP_ERROR
def room_list(campus): """/user/room/list campus 1=建工/2=燕巢/3=第一/4=楠梓/5=旗津 get campus room list In this function, without use cache_ap_query use webap_crawler.query and use GUEST account. Returns: [str]: result type is json error: [int] ROOM_LIST_ERROR CACHE_WEBAP_LOGIN_FAIL (111) CACHE_WEBAP_SERVER_ERROR (112) CACHE_WEBAP_ERROR (113) """ if red_string.exists('campus_%s' % campus): return red_string.get('campus_%s' % campus) login_status = login(username=config.AP_GUEST_ACCOUNT, password=config.AP_GUEST_PASSWORD) if login_status == error_code.CACHE_WENAP_LOGIN_SUCCESS: session = get_session() # load guest cookie session.cookies = pickle.loads( red_bin.get('webap_cookie_%s' % config.AP_GUEST_ACCOUNT)) query_res = webap_crawler.query(session=session, qid='ag302_01', cmp_area_id=campus) if query_res == False: return error_code.ROOM_LIST_ERROR elif isinstance(query_res, requests.models.Response): room_list_data = json.dumps(parse.room_list(query_res.text)) red_string.set(name='campus_%s' % campus, value=room_list_data, ex=config.CACHE_SEMESTERS_EXPIRE_TIME) return room_list_data else: return error_code.CACHE_WEBAP_ERROR return error_code.CACHE_WEBAP_ERROR
def semesters(): """/user/semesters In this function, without use cache_ap_query use webap_crawler.query and use GUEST account. Returns: [str]: result type is json Why don't use dict? redis can't save dict :P error: [int] SEMESTERS_QUERY_ERROR CACHE_WEBAP_LOGIN_FAIL (111) CACHE_WEBAP_SERVER_ERROR (112) CACHE_WEBAP_ERROR (113) """ if red_string.exists('semesters'): return red_string.get('semesters') login_status = login(username=config.AP_GUEST_ACCOUNT, password=config.AP_GUEST_PASSWORD) if login_status == error_code.CACHE_WENAP_LOGIN_SUCCESS: session = get_session() # load guest cookie session.cookies = pickle.loads( red_bin.get('webap_cookie_%s' % config.AP_GUEST_ACCOUNT)) query_res = webap_crawler.query(session=session, qid='ag304_01') if query_res == False: return error_code.SEMESTERS_QUERY_ERROR elif isinstance(query_res, requests.models.Response): semesters_data = json.dumps(parse.semesters(query_res.text)) red_string.set(name='semesters', value=semesters_data, ex=config.CACHE_SEMESTERS_EXPIRE_TIME) return semesters_data else: return error_code.CACHE_WEBAP_ERROR return error_code.CACHE_WEBAP_ERROR