﻿#-*- coding: utf-8 -*-

import time
import requests
from lxml import etree
from werkzeug.contrib.cache import SimpleCache

import ap
import leave
import bus
import notification
import news

cache = SimpleCache()

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):
    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):
    return bus.query(session, *date.split("-"))


def bus_reserve_query(session):
    return bus.reserve(session)


def bus_booking(session, busId, action):
    return bus.book(session, busId, action)
    

def notification_query(page=1):
    return notification.get(page)
    

def news_query():
    return news.news()


def news_status():
    return news.news_status()


def server_status():
    if not cache.get("server_status"):
        ap_status = ap.status()
        leave_status = leave.status()
        bus_status = bus.status()

        server_status = [ap_status, leave_status, bus_status]

        cache.set("server_status", server_status, timeout=180)
    else:
        server_status = cache.get("server_status")


    return server_status
    

if __name__ == "__main__":
    import requests
    s = requests.Session()
    is_login = login(s, "guest", "123")

    print(is_login)

@@ -30,16 +30,28 @@
 rt time%0A
+import json%0A
 import r
@@ -58,16 +58,16 @@
 equests%0A
-
 from lxm
@@ -196,16 +196,93 @@
 t news%0A%0A
+%0ABUS_TIMEOUT = 1800%0ASERVER_STATUS_TIMEOUT = 180%0ABUS_QUERY_TAG = %22bus_query%22%0A%0A
 cache = 
@@ -1227,51 +1227,551 @@
     
-return bus.query(session, *date.split(%22-%22))
+bus_cache_key = BUS_QUERY_TAG + date%0A%0A    if not cache.get(bus_cache_key):%0A        bus_q = bus.query(session, *date.split(%22-%22))%0A        cache.set(bus_cache_key, bus_q, timeout=BUS_TIMEOUT)%0A    else:%0A        bus_q = cache.get(bus_cache_key)%0A%0A        # Check if have reserve, and change isReserve value to 0%0A        reserve = bus_reserve_query(session)%0A%0A        for r in reserve:%0A            for q in bus_q:%0A                if r%5B'time'%5D == q%5B'runDateTime'%5D:%0A                    q%5B'isReserve'%5D = 0%0A                    break%0A    %0A%0A    return bus_q
 %0A%0A%0Ad
@@ -2378,19 +2378,37 @@
 timeout=
-180
+SERVER_STATUS_TIMEOUT
 )%0A    el

Update bus query cache