# Path: mod/models/library_auth_cache.py
# class LibraryAuthCache(Base):
#     __tablename__ = 'library_auth'
#     cardnum = Column(Integer, primary_key=True)
#     cookie = Column(String(4096))
#     captcha = Column(String(4096))
#     date = Column(Integer, nullable=False)


from config import *
from tornado.httpclient import HTTPClient
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from bs4 import BeautifulSoup
from ..models.library_auth_cache import LibraryAuthCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
import re
import json
import urllib
import base64
import tornado.web, tornado.gen
# -*- coding: utf-8 -*-


class LibAuthHandler(tornado.web.RequestHandler):

    @property
    def db(self):
        return self.application.db

    def on_finish(self):
        self.db.close()

    def get(self):

        cardnum = self.get_argument('cardnum', default=None)
        if not cardnum:
            # 因为需要返回图片，所以一卡通为空不做提示
            raise Exception
        try:
            client = HTTPClient()
            response = client.fetch(GET_CAPTCHA)
            cookie = response.headers['Set-Cookie'].split(';')[0]
            try:

# Path: mod/models/library_auth_cache.py
# class LibraryAuthCache(Base):
#     __tablename__ = 'library_auth'
#     cardnum = Column(Integer, primary_key=True)
#     cookie = Column(String(4096))
#     captcha = Column(String(4096))
#     date = Column(Integer, nullable=False)


# Path: mod/library/library_auth.py
from config import *
from tornado.httpclient import HTTPClient
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from bs4 import BeautifulSoup
from ..models.library_auth_cache import LibraryAuthCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
import re
import json
import urllib
import base64
import tornado.web, tornado.gen

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


class LibAuthHandler(tornado.web.RequestHandler):

    @property
    def db(self):
        return self.application.db

    def on_finish(self):
        self.db.close()

    def get(self):

        cardnum = self.get_argument('cardnum', default=None)
        if not cardnum:
            # 因为需要返回图片，所以一卡通为空不做提示
            raise Exception
        try:
            client = HTTPClient()
            response = client.fetch(GET_CAPTCHA)
            cookie = response.headers['Set-Cookie'].split(';')[0]
            try:

                status = self.db.query(LibraryAuthCache).filter(LibraryAuthCache.cardnum == cardnum).one()