# Path: mod/models/pe_models.py
# class PeDetailCache(Base):
#     __tablename__ = 'pedetail'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(10240))
#     date = Column(Integer, nullable=False)


from tornado.httpclient import HTTPRequest, AsyncHTTPClient,HTTPError
from sgmllib import SGMLParser
from config import loginurl1,runurl
from sqlalchemy.orm.exc import NoResultFound
from ..models.pe_models import PeDetailCache
from time import time,localtime, strftime
import json
import tornado.web
import tornado.gen
import urllib
import base64
                    headers = header,
                    connect_timeout=2,
                    request_timeout=3
                )
                
                response = yield client.fetch(request)
                cookie1 = response.headers['Set-Cookie']
                header['Cookie'] = init_cookie1+';'+cookie1+';'+';amblcookie=02'  
                getpeurl = "http://zccx.seu.edu.cn/SportWeb/gym/gymExercise/gymExercise_query_result_2.jsp?xh=%s"%(cardnum)
                request = HTTPRequest(
                    getpeurl,
                    headers = header,
                    connect_timeout=2,
                    request_timeout=8
                    )
                response = yield client.fetch(request)
                spider = RunningParser()
                spider.getRunningTable(response.body)
                retjson['content'] = spider.table
            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:
            status.date = int(time())
            status.text = base64.b64encode(ret)

# Path: mod/models/pe_models.py
# class PeDetailCache(Base):
#     __tablename__ = 'pedetail'
#     cardnum = Column(Integer, primary_key=True)
#     text = Column(String(10240))
#     date = Column(Integer, nullable=False)


# Path: mod/pe/pedetailHandler.py
from tornado.httpclient import HTTPRequest, AsyncHTTPClient,HTTPError
from sgmllib import SGMLParser
from config import loginurl1,runurl
from sqlalchemy.orm.exc import NoResultFound
from ..models.pe_models import PeDetailCache
from time import time,localtime, strftime
import json
import tornado.web
import tornado.gen
import urllib
import base64

                    headers = header,
                    connect_timeout=2,
                    request_timeout=3
                )
                
                response = yield client.fetch(request)
                cookie1 = response.headers['Set-Cookie']
                header['Cookie'] = init_cookie1+';'+cookie1+';'+';amblcookie=02'  
                getpeurl = "http://zccx.seu.edu.cn/SportWeb/gym/gymExercise/gymExercise_query_result_2.jsp?xh=%s"%(cardnum)
                request = HTTPRequest(
                    getpeurl,
                    headers = header,
                    connect_timeout=2,
                    request_timeout=8
                    )
                response = yield client.fetch(request)
                spider = RunningParser()
                spider.getRunningTable(response.body)
                retjson['content'] = spider.table
            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:
            status.date = int(time())
            status.text = base64.b64encode(ret)

            self.db.add(status)