# Path: mod/_config.py

# 
# Path: mod/models/jwc_cache.py
# class JWCCache(Base):
#     __tablename__ = 'jwc'
#     date = Column(Integer, primary_key=True)
#     text = Column(String(10240), nullable=False)


from .._config import jwcCacheTime
from BeautifulSoup import BeautifulSoup
from config import JWC_URL, TIME_OUT
from tornado.httpclient import HTTPRequest, HTTPClient
from ..models.jwc_cache import JWCCache
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime
import tornado.web
import tornado.gen
import json, base64
import urllib,traceback,os
            status = self.db.query(JWCCache).filter( JWCCache.date > int(time())-jwcCacheTime).order_by(JWCCache.date.desc()).all()
	    if len(status) == 0:
		raise NoResultFound
	    status = status[0]
            self.write(base64.b64decode(status.text))
            self.finish()
            return
        except NoResultFound:
            ret = self.refresh_status()
            if ret['code'] == 200:
                ret = json.dumps(ret, ensure_ascii=False, indent=2)
                status = JWCCache(date=int(time()), text=base64.b64encode(ret))
                self.db.add(status)
                self.db.commit()
                self.write(ret)
                self.finish()
                return
            else:
                retjson['code'] = 201
                retjson['content'] = 'refresh'
        except Exception,e:
            retjson['code'] = 500
            retjson['content'] = "error:"+str(e)
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()


    def refresh_status(self):
        try:
            lock = self.db.query(JWCCache).filter(

# Path: mod/_config.py

# 
# Path: mod/models/jwc_cache.py
# class JWCCache(Base):
#     __tablename__ = 'jwc'
#     date = Column(Integer, primary_key=True)
#     text = Column(String(10240), nullable=False)


# Path: mod/jwc/handler.py
from .._config import jwcCacheTime
from BeautifulSoup import BeautifulSoup
from config import JWC_URL, TIME_OUT
from tornado.httpclient import HTTPRequest, HTTPClient
from ..models.jwc_cache import JWCCache
from sqlalchemy.orm.exc import NoResultFound
from time import time, localtime, strftime
import tornado.web
import tornado.gen
import json, base64
import urllib,traceback,os

            status = self.db.query(JWCCache).filter( JWCCache.date > int(time())-jwcCacheTime).order_by(JWCCache.date.desc()).all()
	    if len(status) == 0:
		raise NoResultFound
	    status = status[0]
            self.write(base64.b64decode(status.text))
            self.finish()
            return
        except NoResultFound:
            ret = self.refresh_status()
            if ret['code'] == 200:
                ret = json.dumps(ret, ensure_ascii=False, indent=2)
                status = JWCCache(date=int(time()), text=base64.b64encode(ret))
                self.db.add(status)
                self.db.commit()
                self.write(ret)
                self.finish()
                return
            else:
                retjson['code'] = 201
                retjson['content'] = 'refresh'
        except Exception,e:
            retjson['code'] = 500
            retjson['content'] = "error:"+str(e)
        self.write(json.dumps(retjson, ensure_ascii=False, indent=2))
        self.finish()


    def refresh_status(self):
        try:
            lock = self.db.query(JWCCache).filter(

                        JWCCache.date == 0).one()