# Path: mod/_config.py

# 
# Path: mod/models/card_cache.py
# class CardCache(Base):
#     __tablename__ = 'card'
#     cardnum = Column(BIGINT, primary_key=True)
#     text = Column(LONGTEXT)
#     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 cardCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from BeautifulSoup import BeautifulSoup
from ..models.card_cache import CardCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import urllib, re
import json, base64
import datetime
import traceback
import IPython
                            tmp['date'] = td[0].text
                            tmp['type'] = td[3].text#.encode('ISO-8859-1').decode('gbk')
                            tmp['system'] = td[4].text#.encode('ISO-8859-1').decode('gbk')
                            tmp['price'] = td[5].text
                            tmp['left'] = td[6].text
                            if(tmp['type']== u'扣款'):
                                tmp['type'] = '水电扣费'
                            detial.append(tmp)
                        page += 1
                        data['pageNum'] = page
                        request = HTTPRequest(
                            DATA_URL,
                            method='POST',
                            headers={'Cookie':cookie},
                            body=urllib.urlencode(data),
                            request_timeout=TIME_OUT)
                        response = yield tornado.gen.Task(client.fetch, request)
                        soup = BeautifulSoup(response.body.decode('gb2312','ignore'))
                    retjson['content'] = {'state':cardState, 'left': cardLetf, 'cardLeft':cardLetf, 'detail':detial, 'detial': detial}
            else:
                retjson['code'] = 401
                retjson['content'] = 'wrong card number or password'
        except Exception,e:
            retjson['code'] = 500
            retjson['content'] = str(e)

        ret = json.dumps(retjson, ensure_ascii=False, indent=2)
        self.write(ret)
        self.finish()
        # refresh cache

# Path: mod/_config.py

# 
# Path: mod/models/card_cache.py
# class CardCache(Base):
#     __tablename__ = 'card'
#     cardnum = Column(BIGINT, primary_key=True)
#     text = Column(LONGTEXT)
#     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/card/handler.py
from .._config import cardCacheTime
from config import *
from tornado.httpclient import HTTPRequest, AsyncHTTPClient
from BeautifulSoup import BeautifulSoup
from ..models.card_cache import CardCache
from sqlalchemy.orm.exc import NoResultFound
from time import time
from ..auth.handler import authApi
import tornado.web
import tornado.gen
import urllib, re
import json, base64
import datetime
import traceback
import IPython

                            tmp['date'] = td[0].text
                            tmp['type'] = td[3].text#.encode('ISO-8859-1').decode('gbk')
                            tmp['system'] = td[4].text#.encode('ISO-8859-1').decode('gbk')
                            tmp['price'] = td[5].text
                            tmp['left'] = td[6].text
                            if(tmp['type']== u'扣款'):
                                tmp['type'] = '水电扣费'
                            detial.append(tmp)
                        page += 1
                        data['pageNum'] = page
                        request = HTTPRequest(
                            DATA_URL,
                            method='POST',
                            headers={'Cookie':cookie},
                            body=urllib.urlencode(data),
                            request_timeout=TIME_OUT)
                        response = yield tornado.gen.Task(client.fetch, request)
                        soup = BeautifulSoup(response.body.decode('gb2312','ignore'))
                    retjson['content'] = {'state':cardState, 'left': cardLetf, 'cardLeft':cardLetf, 'detail':detial, 'detial': detial}
            else:
                retjson['code'] = 401
                retjson['content'] = 'wrong card number or password'
        except Exception,e:
            retjson['code'] = 500
            retjson['content'] = str(e)

        ret = json.dumps(retjson, ensure_ascii=False, indent=2)
        self.write(ret)
        self.finish()
        # refresh cache

        if retjson['code'] == 200: