# Path: mod/_config.py

# 
# Path: mod/models/lecture_cache.py
# class LectureCache(Base):
#     __tablename__ = 'lecture'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096))
#     date = Column(Integer, nullable=False)
# 
# Path: mod/auth/handler.py
# def authApi(username,password):
#     data = {
#             'username':username,
#             'password':password
#         }
#     result = {'code':200,'content':''}
#     try:
#         client = HTTPClient()
#         request = HTTPRequest(
#             CHECK_URL,
#             method='POST',
#             body=urllib.urlencode(data),
#             validate_cert=False,
#             request_timeout=TIME_OUT)
#         response = client.fetch(request)
#         header = response.headers
#         if 'Ssocookie' in header.keys():
#             headertemp = json.loads(header['Ssocookie'])
#             cookie = headertemp[0]['cookieName']+"="+headertemp[0]['cookieValue']
#             cookie += ";"+header['Set-Cookie'].split(";")[0]
#             result['content'] = cookie
#         else:
#             result['code'] = 400
#     except HTTPError as e:
#         if e.code == 401:
#           result['code'] = 401
#         else:
#           result['code'] = 500
#     except Exception,e:
#         result['code'] = 500
#     return result


from .._config import lectureCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from BeautifulSoup import BeautifulSoup
from ..models.lecture_cache import LectureCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import json, base64
import urllib, re
import traceback
        except NoResultFound:
            status = LectureCache(cardnum=cardnum, text='*', date=int(time()))
            self.db.add(status)
            try:
                self.db.commit()
            except:
                self.db.rollback()

        try:
            response = authApi(cardnum,self.get_argument('password'))
            if response['code']==200:
                cookie = response['content']
                client = AsyncHTTPClient()
                request = HTTPRequest(
                    LOGIN_URL,
                    method='GET',
                    headers={'Cookie':cookie},
                    request_timeout=TIME_OUT)
                response = yield tornado.gen.Task(client.fetch, request)
                cookie += ';' + response.headers['Set-Cookie'].split(';')[0]
                request = HTTPRequest(
                    USERID_URL,
                    method='GET',
                    headers={'Cookie':cookie},
                    request_timeout=TIME_OUT)
                response = yield tornado.gen.Task(client.fetch, request)
                soup = BeautifulSoup(response.body)
                userid = soup.findAll(attrs={"align": "left"})[2].text

                page = 1

# Path: mod/_config.py

# 
# Path: mod/models/lecture_cache.py
# class LectureCache(Base):
#     __tablename__ = 'lecture'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096))
#     date = Column(Integer, nullable=False)
# 
# Path: mod/auth/handler.py
# def authApi(username,password):
#     data = {
#             'username':username,
#             'password':password
#         }
#     result = {'code':200,'content':''}
#     try:
#         client = HTTPClient()
#         request = HTTPRequest(
#             CHECK_URL,
#             method='POST',
#             body=urllib.urlencode(data),
#             validate_cert=False,
#             request_timeout=TIME_OUT)
#         response = client.fetch(request)
#         header = response.headers
#         if 'Ssocookie' in header.keys():
#             headertemp = json.loads(header['Ssocookie'])
#             cookie = headertemp[0]['cookieName']+"="+headertemp[0]['cookieValue']
#             cookie += ";"+header['Set-Cookie'].split(";")[0]
#             result['content'] = cookie
#         else:
#             result['code'] = 400
#     except HTTPError as e:
#         if e.code == 401:
#           result['code'] = 401
#         else:
#           result['code'] = 500
#     except Exception,e:
#         result['code'] = 500
#     return result


# Path: mod/lecture/handler.py
from .._config import lectureCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from BeautifulSoup import BeautifulSoup
from ..models.lecture_cache import LectureCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import json, base64
import urllib, re
import traceback

        except NoResultFound:
            status = LectureCache(cardnum=cardnum, text='*', date=int(time()))
            self.db.add(status)
            try:
                self.db.commit()
            except:
                self.db.rollback()

        try:
            response = authApi(cardnum,self.get_argument('password'))
            if response['code']==200:
                cookie = response['content']
                client = AsyncHTTPClient()
                request = HTTPRequest(
                    LOGIN_URL,
                    method='GET',
                    headers={'Cookie':cookie},
                    request_timeout=TIME_OUT)
                response = yield tornado.gen.Task(client.fetch, request)
                cookie += ';' + response.headers['Set-Cookie'].split(';')[0]
                request = HTTPRequest(
                    USERID_URL,
                    method='GET',
                    headers={'Cookie':cookie},
                    request_timeout=TIME_OUT)
                response = yield tornado.gen.Task(client.fetch, request)
                soup = BeautifulSoup(response.body)
                userid = soup.findAll(attrs={"align": "left"})[2].text

                page = 1

                data = {