# 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 *
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 requests
                    data = {
                        'account':userid,
                        'inputObject':"all",
                    }
                    response = requests.post(TODAYDATA_URL,headers={'Cookie':cookie},data=data)
                    soup = BeautifulSoup(response.content)
                    tr = soup.findAll('tr',{"class": re.compile("listbg")})
                    detail=[]
                    for td in tr:
                            td = td.findChildren()
                            tmp = {}
                            tmp['date'] = td[0].text
                            tmp['type'] = td[3].text
                            tmp['system'] = td[4].text
                            tmp['price'] = td[5].text
                            tmp['left'] = td[6].text
                            detail.append(tmp)
                    retjson['content'] = {'state':cardState,'left':cardLetf,'detial':detail}
                #get other days detail depend on timedelta
                else:
                    response = requests.get(INIT_URL,headers={'Cookie':cookie})
                    soup = BeautifulSoup(response.content)
                    __continue = soup.findAll('form',{'id':'accounthisTrjn1'})[0]['action']

                    now = datetime.datetime.now()
                    delta = datetime.timedelta(timedelta)
                    page = 1
                    data = {
                        'account':userid,
                        'inputObject':'all',

# 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/newhandler.py
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 requests

                    data = {
                        'account':userid,
                        'inputObject':"all",
                    }
                    response = requests.post(TODAYDATA_URL,headers={'Cookie':cookie},data=data)
                    soup = BeautifulSoup(response.content)
                    tr = soup.findAll('tr',{"class": re.compile("listbg")})
                    detail=[]
                    for td in tr:
                            td = td.findChildren()
                            tmp = {}
                            tmp['date'] = td[0].text
                            tmp['type'] = td[3].text
                            tmp['system'] = td[4].text
                            tmp['price'] = td[5].text
                            tmp['left'] = td[6].text
                            detail.append(tmp)
                    retjson['content'] = {'state':cardState,'left':cardLetf,'detial':detail}
                #get other days detail depend on timedelta
                else:
                    response = requests.get(INIT_URL,headers={'Cookie':cookie})
                    soup = BeautifulSoup(response.content)
                    __continue = soup.findAll('form',{'id':'accounthisTrjn1'})[0]['action']

                    now = datetime.datetime.now()
                    delta = datetime.timedelta(timedelta)
                    page = 1
                    data = {
                        'account':userid,
                        'inputObject':'all',

                        'inputStartDate':(now - delta).strftime('%Y%m%d'),