# Path: mod/_config.py

# 
# Path: mod/models/exam_cache.py
# class ExamCache(Base):
#     __tablename__ = 'exam'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     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 examCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from BeautifulSoup import BeautifulSoup
from sqlalchemy.orm.exc import NoResultFound
from ..models.exam_cache import ExamCache
from time import time,localtime, strftime
from PIL import Image
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import json, base64,traceback,urllib
import io
            except Exception,e:
                self.db.rollback()
            finally:
                self.db.remove()
    @tornado.web.asynchronous
    @tornado.gen.engine
    def newSeuHandler(self,number,password,callback=None):
        retjson = {'code':200, 'content':''}
        try:
            client = AsyncHTTPClient()
            response = authApi(number,password)
            if response['code'] == 200:
                header['Cookie'] = response['content']
                request = HTTPRequest(
                    FIRST_URL,
                    method="GET",
                    headers = header,
                    request_timeout=TIME_OUT
                )
                response = yield tornado.gen.Task(client.fetch,request)
                header['Cookie'] = header['Cookie'].split(';')[0]+';'+response.headers['Set-Cookie']
                request = HTTPRequest(
                    DETAIL_URL,
                    method="GET",
                    headers = header,
                    request_timeout=TIME_OUT
                )
                response = yield tornado.gen.Task(client.fetch,request)
                retjson['content'] = self.dealData(response.body)
            else:

# Path: mod/_config.py

# 
# Path: mod/models/exam_cache.py
# class ExamCache(Base):
#     __tablename__ = 'exam'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     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/exam/handler.py
from .._config import examCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from BeautifulSoup import BeautifulSoup
from sqlalchemy.orm.exc import NoResultFound
from ..models.exam_cache import ExamCache
from time import time,localtime, strftime
from PIL import Image
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import json, base64,traceback,urllib
import io

            except Exception,e:
                self.db.rollback()
            finally:
                self.db.remove()
    @tornado.web.asynchronous
    @tornado.gen.engine
    def newSeuHandler(self,number,password,callback=None):
        retjson = {'code':200, 'content':''}
        try:
            client = AsyncHTTPClient()
            response = authApi(number,password)
            if response['code'] == 200:
                header['Cookie'] = response['content']
                request = HTTPRequest(
                    FIRST_URL,
                    method="GET",
                    headers = header,
                    request_timeout=TIME_OUT
                )
                response = yield tornado.gen.Task(client.fetch,request)
                header['Cookie'] = header['Cookie'].split(';')[0]+';'+response.headers['Set-Cookie']
                request = HTTPRequest(
                    DETAIL_URL,
                    method="GET",
                    headers = header,
                    request_timeout=TIME_OUT
                )
                response = yield tornado.gen.Task(client.fetch,request)
                retjson['content'] = self.dealData(response.body)
            else:

                retjson['code'] = 408