def getWechat(): ''' 控制最新的wechat ''' result = list(pg.select('wechat_dead_line')) if result: wechat_dead_line = result[0] now = datetime.datetime.now() if wechat_dead_line.access_token_expires_at is not None and now < wechat_dead_line.access_token_expires_at and now < wechat_dead_line.jsapi_ticket_expires_at: conf = WechatConf( token=token, appid=appid, appsecret=appsecret, encrypt_mode='compatible', encoding_aes_key=encoding_aes_key, jsapi_ticket=wechat_dead_line.jsapi_ticket, jsapi_ticket_expires_at=time_bz.datetimeToTimestamp(wechat_dead_line.jsapi_ticket_expires_at), access_token=wechat_dead_line.access_token, access_token_expires_at=time_bz.datetimeToTimestamp(wechat_dead_line.access_token_expires_at), ) wechat = WechatBasic(conf=conf) else: wechat, access_token, access_token_expires_at, jsapi_ticket, jsapi_ticket_expires_at = getNewWechatInfo() pg.update('wechat_dead_line', where='1=1', access_token=access_token, access_token_expires_at=access_token_expires_at, jsapi_ticket=jsapi_ticket, jsapi_ticket_expires_at=jsapi_ticket_expires_at) else: wechat, access_token, access_token_expires_at, jsapi_ticket, jsapi_ticket_expires_at = getNewWechatInfo() pg.insert('wechat_dead_line', access_token=access_token, access_token_expires_at=access_token_expires_at, jsapi_ticket=jsapi_ticket, jsapi_ticket_expires_at=jsapi_ticket_expires_at) return wechat
def saveLastId(user, medias): ''' create by bigzhu at 15/09/04 21:42:06 保存最后那条记录,删除重复记录 modify by bigzhu at 15/09/05 09:12:42 删除重复记录不应该影响update id ''' medias = medias['data'] if medias: if medias[-1]['id'] == user.last_id: # 会取出最后一条,要删了 del medias[-1] if medias: last_id = medias[0]['id'] pg.update('instagram_user', where="lower(username)=lower('%s')" % user.username, last_id=last_id)
def post(self): self.set_header("Content-Type", "application/json") print self.request.body json_data = xml2json(self.request.body.encode('utf8'), options) data = json.loads(json_data)['xml'] return_code = data['return_code'] if return_code == 'SUCCESS': openid = data['openid'] out_trade_no = data['out_trade_no'] cash_fee = data['cash_fee'] where = " openid='%s' and id=%s and (status<>'payed' or status is null) " % (openid, out_trade_no) from webpy_db import SQLLiteral count = pg.update('pay', status='payed', wexin_return=json_data, stat_date=SQLLiteral('NOW()'), where=where) if count != 1: error_info = 'update failure: count=%s where=%s' % (count, where) print error_info raise Exception(error_info) else: wechat = wechat_oper.getWechat() content = '''您支付的 %s 元已进入充值系统,正在向您的油卡充值,请耐心等候......''' % (int(cash_fee) / 100.00) wechat.send_text_message(openid, content) else: print data['return_msg'] #{u'openid': u'oGXiIwHwx_zB8ekXibYjdt3Xb_fE', u'trade_type': u'JSAPI', u'cash_fee': u'1', u'nonce_str': u'798243e4902342c83e833c71141385f', u'return_code': u'SUCCESS', u'is_subscribe': u'Y', u'bank_type': u'CFT', u'mch_id': u'1308443701', u'out_trade_no': u'86', u'result_code': u'SUCCESS', u'total_fee': u'1', u'appid': u'wx907d8a3f50de65db', u'fee_type': u'CNY', u'time_end': u'20160215113326', u'transaction_id': u'1002230516201602153283628055', u'sign': u'CAD12073F45232BB600B8F066B434A30'} success = ''' <xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_msg><![CDATA[OK]]></return_msg> </xml> ''' self.write(success)
def delete(self, id): self.set_header("Content-Type", "application/json") openid = self.get_secure_cookie("openid") openid = checkOpenid(openid) where = " id=%s and openid='%s' " % (id, openid) count = pg.update("bind_card_info", where=where, is_delete=1) if count != 1: raise Exception("删除失败,删除记录 %s 条" % count) self.write(json.dumps({"error": "0"}, cls=public_bz.ExtEncoder))
def saveLast(last_time, last_message_id, user_id): ''' create by bigzhu at 15/08/16 16:22:39 保存最后一条的message ''' last_time = int(last_time) datetime_last_time = time_bz.timestampToDateTime(last_time, millisecond=True) id = db_bz.insertIfNotExist(pg, 'last', {'user_id': user_id, 'last_time': datetime_last_time, 'last_message_id': last_message_id}, "user_id=%s" % user_id) if id is None: count = pg.update('last', where='last_time< to_timestamp(%s/1000) and user_id=%s' % (last_time, user_id), last_message_id=last_message_id, last_time=datetime_last_time) return count return id
def put(self): self.set_header("Content-Type", "application/json") parm = json.loads(self.request.body) id = parm["id"] openid = self.get_secure_cookie("openid") openid = checkOpenid(openid) where = " id=%s and openid='%s' " % (id, openid) count = pg.update("bind_card_info", where=where, **parm) if count != 1: raise Exception("更新失败,更新记录 %s 条" % count) self.write(json.dumps({"error": "0"}, cls=public_bz.ExtEncoder))
def getWechat(): ''' 控制最新的wechat ''' result = list(pg.select('wechat_dead_line')) if result: wechat_dead_line = result[0] now = datetime.datetime.now() if now < wechat_dead_line.access_token_expires_at and now < wechat_dead_line.jsapi_ticket_expires_at: wechat = WechatBasic(jsapi_ticket=wechat_dead_line.jsapi_ticket, jsapi_ticket_expires_at=time_bz.datetimeToTimestamp(wechat_dead_line.jsapi_ticket_expires_at), access_token=wechat_dead_line.access_token, access_token_expires_at=time_bz.datetimeToTimestamp(wechat_dead_line.access_token_expires_at), token=token, appid=appid, appsecret=appsecret) else: wechat, access_token, access_token_expires_at, jsapi_ticket, jsapi_ticket_expires_at = getNewWechatInfo() pg.update('wechat_dead_line', where='1=1', access_token=access_token, access_token_expires_at=access_token_expires_at, jsapi_ticket=jsapi_ticket, jsapi_ticket_expires_at=jsapi_ticket_expires_at) else: wechat, access_token, access_token_expires_at, jsapi_ticket, jsapi_ticket_expires_at = getNewWechatInfo() pg.insert('wechat_dead_line', access_token=access_token, access_token_expires_at=access_token_expires_at, jsapi_ticket=jsapi_ticket, jsapi_ticket_expires_at=jsapi_ticket_expires_at) return wechat
def getWechat(): ''' 控制最新的wechat ''' result = list(pg.select('wechat_dead_line')) if result: wechat_dead_line = result[0] now = datetime.datetime.now() if wechat_dead_line.access_token_expires_at is not None and now < wechat_dead_line.access_token_expires_at and now < wechat_dead_line.jsapi_ticket_expires_at: wechat = WechatBasic(jsapi_ticket=wechat_dead_line.jsapi_ticket, jsapi_ticket_expires_at=time_bz.datetimeToTimestamp(wechat_dead_line.jsapi_ticket_expires_at), access_token=wechat_dead_line.access_token, access_token_expires_at=time_bz.datetimeToTimestamp(wechat_dead_line.access_token_expires_at), token=token, appid=appid, appsecret=appsecret) else: wechat, access_token, access_token_expires_at, jsapi_ticket, jsapi_ticket_expires_at = getNewWechatInfo() pg.update('wechat_dead_line', where='1=1', access_token=access_token, access_token_expires_at=access_token_expires_at, jsapi_ticket=jsapi_ticket, jsapi_ticket_expires_at=jsapi_ticket_expires_at) else: wechat, access_token, access_token_expires_at, jsapi_ticket, jsapi_ticket_expires_at = getNewWechatInfo() pg.insert('wechat_dead_line', access_token=access_token, access_token_expires_at=access_token_expires_at, jsapi_ticket=jsapi_ticket, jsapi_ticket_expires_at=jsapi_ticket_expires_at) return wechat
def saveLast(last_message_id, user_id): ''' create by bigzhu at 15/08/16 16:22:39 保存最后一条的message ''' #last_time = int(last_time) #datetime_last_time = time_bz.timestampToDateTime(last_time, millisecond=True) id = db_bz.insertIfNotExist(pg, 'last', { 'user_id': user_id, 'last_message_id': last_message_id }, "user_id=%s" % user_id) if id is None: count = pg.update('last', where='last_message_id< %s and user_id=%s' % (last_message_id, user_id), last_message_id=last_message_id) return count return 1
def bindUser(user_name, openid): count = pg.update('wechat_user', where="openid='%s'" % openid, user_name=user_name) if count != 1: raise Exception('绑定失败: count=%s, openid=%s' % (count, openid))
def getMedia(user_name=None, with_next_url=None, user=None): if user_name: user = getUser(user_name) if user is None: return # min_id 会查出大于等于这个id的 try: medias, next_ = api.user_recent_media(user_id=user.id, min_id=user.last_id) except instagram.bind.InstagramClientError: print public_bz.getExpInfoAll() public_db.delNoName('instagram', user_name) return if medias: last_id = medias[0].id pg.update('instagram_user', where="lower(username)=lower('%s')" % user_name, last_id=last_id) else: medias, next_ = api.user_recent_media(with_next_url=with_next_url) for media in medias: db_media = storage() if media.caption: caption = media.caption.__dict__ caption['user_id'] = caption['user'].id del caption['user'] else: caption = '' db_media.caption = json.dumps(caption, cls=public_bz.ExtEncoder) db_media.comment_count = media.comment_count if media.comments: media.comments = [d.__dict__ for d in media.comments] for comment in media.comments: comment['user'] = comment['user'].__dict__ db_media.comments = json.dumps(media.comments, cls=public_bz.ExtEncoder) db_media.created_time = media.created_time # 8小时的问题 db_media.created_time += timedelta(hours=8) db_media.filter = media.filter db_media.low_resolution = json.dumps(media.images['low_resolution'].__dict__) db_media.standard_resolution = json.dumps(media.images['standard_resolution'].__dict__) db_media.thumbnail = json.dumps(media.images['thumbnail'].__dict__) db_media.id_str = media.id db_media.like_count = media.like_count # likes里有User对象,暂时不存了 #db_media.likes = json.dumps(media.likes) db_media.link = media.link db_media.type = media.type db_media.user_id = user.id id = db_bz.insertIfNotExist(pg, 'instagram_media', db_media, "id_str='%s'" % db_media.id_str) print 'new=', media.id, user.username if id is not None and len(medias) <= 2: # 新增加消息,微信通知只通知2条以内 openids = public_db.getOpenidsByName('instagram', user.username) for data in openids: if caption != '': text = caption.get('text') else: text = '' wechat_oper.sendInstagram(data.openid, text, media.images['standard_resolution'].url, user.username, id) # 递归查出 if next_ != with_next_url: getMedia(with_next_url=next_, user=user)
def updateEtag(user_name, etag): count = pg.update('github_user', where="lower(login)=lower('%s')" % user_name, etag=etag) if count != 1: raise Exception('更新etag 失败, %s, user_name=%s' % (count, user_name))