# Path: mod/models/pe_models.py
# class PEUser(Base):
#     __tablename__ = 'pe'
#     cardnum = Column(Integer, primary_key=True)
#     count = Column(String(10), nullable=False)
# 
#     def __repr__(self):
#         return '<PE (%s, %d)' % (self.cardnum, self.count)
# 
# Path: mod/models/tice_cache.py
# class TiceCache(Base):
#     __tablename__ = 'tice'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     date = Column(Integer, nullable=False)


from .._config import *
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from BeautifulSoup import BeautifulSoup
from ..models.pe_models import PEUser
from ..models.tice_cache import TiceCache
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime,mktime,strptime
from datetime import datetime, timedelta, date
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
import tornado.web
import tornado.gen
import urllib
import random
import json,socket,base64,traceback
import functools
        else:
            result = yield self.get_pe_count(cardnum)

            if result == -1:
                state = 'time_out'
                try:
                    # 超时取出缓存
                    user = self.db.query(PEUser).filter(
                        PEUser.cardnum == int(cardnum)).one()
                    retjson['content'] = user.count
		    retjson['rank'] = self.get_rank(user.count)
                    retjson['remain'] = self.get_remain_day()
                except NoResultFound:
                    retjson['remain'] = self.get_remain_day()
                    retjson['code'] = 408
                    retjson['content'] = 'time out'
            else:
                retjson['content'] = str(result)
		retjson['rank'] = self.get_rank(result)
                retjson['remain'] = self.get_remain_day()
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()

        if state == 'success':
            # 更新缓存
            try:
                user = self.db.query(PEUser).filter(
                    PEUser.cardnum == int(cardnum)).one()
                user.count = int(result)
            except NoResultFound:

# Path: mod/models/pe_models.py
# class PEUser(Base):
#     __tablename__ = 'pe'
#     cardnum = Column(Integer, primary_key=True)
#     count = Column(String(10), nullable=False)
# 
#     def __repr__(self):
#         return '<PE (%s, %d)' % (self.cardnum, self.count)
# 
# Path: mod/models/tice_cache.py
# class TiceCache(Base):
#     __tablename__ = 'tice'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(4096), nullable=False)
#     date = Column(Integer, nullable=False)


# Path: mod/pe/handler.py
from .._config import *
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from tornado.tcpclient import TCPClient
from tornado.ioloop import IOLoop
from BeautifulSoup import BeautifulSoup
from ..models.pe_models import PEUser
from ..models.tice_cache import TiceCache
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime,mktime,strptime
from datetime import datetime, timedelta, date
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor
import tornado.web
import tornado.gen
import urllib
import random
import json,socket,base64,traceback
import functools

        else:
            result = yield self.get_pe_count(cardnum)

            if result == -1:
                state = 'time_out'
                try:
                    # 超时取出缓存
                    user = self.db.query(PEUser).filter(
                        PEUser.cardnum == int(cardnum)).one()
                    retjson['content'] = user.count
		    retjson['rank'] = self.get_rank(user.count)
                    retjson['remain'] = self.get_remain_day()
                except NoResultFound:
                    retjson['remain'] = self.get_remain_day()
                    retjson['code'] = 408
                    retjson['content'] = 'time out'
            else:
                retjson['content'] = str(result)
		retjson['rank'] = self.get_rank(result)
                retjson['remain'] = self.get_remain_day()
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()

        if state == 'success':
            # 更新缓存
            try:
                user = self.db.query(PEUser).filter(
                    PEUser.cardnum == int(cardnum)).one()
                user.count = int(result)
            except NoResultFound:

                user = PEUser(cardnum=int(cardnum), count=int(result))