def get_access_token(): if _access_token[ 'token'] is None or _access_token['timestamp'] + 7000 <= now_sec(): _access_token['token'] = json.loads( HTTP.get(WX_URL_GET_ACCESS_TOKEN, grant_type='client_credential', appid=APP_ID, secret=APP_SECRET))['access_token'] _access_token['timestamp'] = now_sec() logger.info('access_token is: %s', _access_token['token']) return _access_token['token']
def duration(self): if not self.start_time: return 0 if not self.stop_time: return (now_sec() - self.start_time) // 60 return (self.stop_time - self.start_time) // 60
def encrypt(self): msg = Message() msg.TimeStamp = str(now_sec()) msg.Nonce = randstr() msg.Encrypt = self._encrypt() msg.MsgSignature = self._sign_msg(msg.TimeStamp, msg.Nonce, msg.Encrypt) return msg
def start(self): self.start_time = now_sec() self.stop_time = None self.total_income = 0 self.total_pay = 0 self.user_num = 0 self.pay_num = 0 self.started = True self._users_history = {}
def get_pay_sign(prepay_id): sign_items = { 'nonceStr': randstr(), 'timeStamp': str(now_sec()), 'package': 'prepay_id=' + prepay_id, 'signType': 'MD5', 'appId': APP_ID } sign_items['paySign'] = Signer.signstr(sign_items) return sign_items
def get_jsapi_sign(url): if _jsapi_ticket['ticket'] is None or _jsapi_ticket[ 'timestamp'] + 7000 <= now_sec(): access_token = get_access_token() _jsapi_ticket['ticket'] = json.loads( HTTP.get(WX_URL_GET_JSAPI_TICKET, access_token=access_token, type='jsapi'))['ticket'] _jsapi_ticket['timestamp'] = now_sec() noncestr = randstr() timestamp = now_sec() sign = _build_jsapi_sign(_jsapi_ticket['ticket'], noncestr, timestamp, url) return { 'noncestr': noncestr, 'sign': sign, 'timestamp': timestamp, 'appid': APP_ID }
def is_available(self): _now = now_sec() if _now >= self._disable_time(): return False if self.started: if 'duration' in self.config: return self.start_time + self.config['duration'] * 60 > _now if self.config['enable'] and _now >= self._available_time(): return True return False
def callback(): msg = Message(request.data) if app.config['TESTING']: msg = msg.decrypt() if msg.MsgType == 'event': if msg.Event == 'subscribe': return _handle_subscribe(g.service, msg) if msg.Event == 'unsubscribe': return _handle_unsubscribe(g.service, msg) if msg.Event == 'SCAN': return _handle_scan(g.service, msg) reply = Message() reply.ToUserName = msg.FromUserName reply.FromUserName = msg.ToUserName reply.CreateTime = now_sec() reply.MsgType = 'text' reply.Content = app.config['AUTH2_SHORT_URL'] if not app.config['TESTING']: reply = reply_msg.encrypt() return reply.xml()
def load_strategies(self): logger.info('load strategies') self.strategy_files = self._find_strategy_files() self.strategies = [Strategy(file) for file in self.strategy_files] self._load_strategies_time = now_sec()
def append(self, income, pay): self.history.append((income, pay, now_sec()))
def stop(self): self.stop_time = now_sec() self.started = False
def _strategies_old(self): return self._load_strategies_time + STRATEGY_REFREASH_INTERVAL <= now_sec( )