# 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
                td = tr[i].findAll('td')
                retTemp = {
                    'course':td[3].text,
                    'type':td[4].text,
                    'teacher':td[5].text[:-6],
                    'time':td[6].text[:-6],
                    'location':td[7].text,
                    'hour':td[8].text
                }
                ret.append(retTemp)
        return ret

    def recognize(self, img):
        start = [13, 59, 105, 151]
        result = ''
        for i in start:
            sample = []
            for i in xrange(i, i + 40):
                temp = 0
                for j in xrange(0, 100):
                    temp += (img.getpixel((i, j))[1] < 40)
                sample.append(temp)
            min_score = 1000
            max_match = 0
            for idx, val in enumerate(STANDARD):
                diff = []
                for i in xrange(len(sample)):
                    diff.append(sample[i] - val[i])
                avg = float(sum(diff)) / len(diff)


# 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

                td = tr[i].findAll('td')
                retTemp = {
                    'course':td[3].text,
                    'type':td[4].text,
                    'teacher':td[5].text[:-6],
                    'time':td[6].text[:-6],
                    'location':td[7].text,
                    'hour':td[8].text
                }
                ret.append(retTemp)
        return ret

    def recognize(self, img):
        start = [13, 59, 105, 151]
        result = ''
        for i in start:
            sample = []
            for i in xrange(i, i + 40):
                temp = 0
                for j in xrange(0, 100):
                    temp += (img.getpixel((i, j))[1] < 40)
                sample.append(temp)
            min_score = 1000
            max_match = 0
            for idx, val in enumerate(STANDARD):
                diff = []
                for i in xrange(len(sample)):
                    diff.append(sample[i] - val[i])
                avg = float(sum(diff)) / len(diff)


                for i in xrange(len(sample)):