<commit_before>﻿#-*- coding: utf-8 -*-

import os
import time
import json
import redis
import hashlib
import requests
from lxml import etree
from werkzeug.contrib.cache import SimpleCache


import ap
import leave
import bus
import notification
import news


AP_QUERY_EXPIRE = 86400
BUS_EXPIRE_TIME = 1800
SERVER_STATUS_EXPIRE_TIME = 180
NOTIFICATION_EXPIRE_TIME = 1800

BUS_QUERY_TAG = "bus"
NOTIFICATION_TAG = "notification"

cache = SimpleCache()
red = redis.StrictRedis()
SERECT_KEY = str(os.urandom(32))


def login(session, username, password):
    is_login = False

    # AP Login
    try:
        is_login = ap.login(session, username, password)
    except:
        pass


    # Login bus system
    try:
        bus.init(session)
        is_login = bus.login(session, username, password)
    except:
        pass


    # Login leave system
    try:
        is_login = leave.login(session, username, password)
    except:
        pass


    return is_login


def ap_query(session, qid=None, args=None, username=None):
    ap_query_key = qid + hashlib.sha512(str(username) + str(args) + SERECT_KEY).hexdigest()

    if not red.get(ap_query_key):
        ap_query_content = ap.query(session, qid, args)

        red.set(ap_query_key, json.dumps(ap_query_content))
        red.expire(ap_query_key, AP_QUERY_EXPIRE)
    else:
        ap_query_content = json.loads(red.get(ap_query_key))


    return ap.query(session, qid, args)


def leave_query(session, year="102", semester="2"):
    return leave.getList(session, year, semester)


def leave_submit(session, start_date, end_date, reason_id, reason_text, section):
    leave_dict = {"reason_id": reason_id, "reason_text": reason_text, "section": section}

    return leave.submitLeave(session, start_date, end_date, leave_dict)


def bus_query(session, date):
    bus_cache_key = BUS_QUERY_TAG + date.replace("-", "")

    #if not cache.get(bus_cache_key):
    if not red.get(bus_cache_key):
        bus_q = bus.query(session, *date.split("-"))
        for q in bus_q:
            q['isReserve'] = -1
<commit_msg>Fix ap query cache return value<commit_after>﻿#-*- coding: utf-8 -*-

import os
import time
import json
import redis
import hashlib
import requests
from lxml import etree
from werkzeug.contrib.cache import SimpleCache


import ap
import leave
import bus
import notification
import news


AP_QUERY_EXPIRE = 86400
BUS_EXPIRE_TIME = 1800
SERVER_STATUS_EXPIRE_TIME = 180
NOTIFICATION_EXPIRE_TIME = 1800

BUS_QUERY_TAG = "bus"
NOTIFICATION_TAG = "notification"

cache = SimpleCache()
red = redis.StrictRedis()
SERECT_KEY = str(os.urandom(32))


def login(session, username, password):
    is_login = False

    # AP Login
    try:
        is_login = ap.login(session, username, password)
    except:
        pass


    # Login bus system
    try:
        bus.init(session)
        is_login = bus.login(session, username, password)
    except:
        pass


    # Login leave system
    try:
        is_login = leave.login(session, username, password)
    except:
        pass


    return is_login


def ap_query(session, qid=None, args=None, username=None):
    ap_query_key = qid + hashlib.sha512(str(username) + str(args) + SERECT_KEY).hexdigest()

    if not red.get(ap_query_key):
        ap_query_content = ap.query(session, qid, args)

        red.set(ap_query_key, json.dumps(ap_query_content))
        red.expire(ap_query_key, AP_QUERY_EXPIRE)
    else:
        ap_query_content = json.loads(red.get(ap_query_key))


    return ap_query_content


def leave_query(session, year="102", semester="2"):
    return leave.getList(session, year, semester)


def leave_submit(session, start_date, end_date, reason_id, reason_text, section):
    leave_dict = {"reason_id": reason_id, "reason_text": reason_text, "section": section}

    return leave.submitLeave(session, start_date, end_date, leave_dict)


def bus_query(session, date):
    bus_cache_key = BUS_QUERY_TAG + date.replace("-", "")

    #if not cache.get(bus_cache_key):
    if not red.get(bus_cache_key):
        bus_q = bus.query(session, *date.split("-"))
        for q in bus_q:
            q['isReserve'] = -1

{'fraction_non_alphanumeric': 0.10405601158860454, 'fraction_numerical': 0.013037180106228875, 'mean_word_length': 3.5179934569247546, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '60703', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1395, 'n_words': 333}