def getUser(user_name): ''' modify by bigzhu at 15/07/31 16:41:16 api找用户时是按专注度来排序的,名字绝对匹配的未必是第一位, 从10个里面找 ''' users = list(pg.select('instagram_user', where="lower(username)=lower('%s')" % user_name)) if users: return users[0] else: user = None for this_user in api.user_search(user_name, 10): if this_user.username.lower() == user_name.lower(): user = this_user break #如果没有这个用户 if user is None: public_db.delNoName('instagram', user_name) #user = api.user_search(user_name, 1)[0] user = api.user(user.id) db_user = storage() db_user.id = user.id db_user.username = user.username db_user.full_name = user.full_name db_user.profile_picture = user.profile_picture db_user.bio = user.bio db_user.website = user.website db_user.counts = json.dumps(user.counts) pg.insert('instagram_user', **db_user) #db_bz.insertIfNotExist(pg, 'instagram_user', db_user, "id=%s" % db_user.id) return getUser(user_name)
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 saveUserInfo(wechat, openid): ''' create by bigzhu at 15/07/19 01:47:09 顺便绑定用户 ''' # 检查用户是否存储了,没有的话存之 wechat_user_info = public_db.getWechatUserByOpenid(openid) if wechat_user_info: pass else: wechat_user_info = wechat.get_user_info(openid) pg.insert('wechat_user', **wechat_user_info)
def post(self): self.set_header("Content-Type", "application/json") parm = json.loads(self.request.body) openid = self.get_secure_cookie("openid") openid = checkOpenid(openid) print openid card_number = parm["card_number"] is_in = pg.select("available_card_numbers", where=" card_number='%s'" % card_number) if not is_in: raise Exception("卡号:%s 不是可以冲值的油卡,请联系英茂客服核实!" % card_number) parm["openid"] = openid pg.insert("bind_card_info", **parm) 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 post(self): self.set_header("Content-Type", "application/json") ''' #to 7+1 result = wechat_bz.callPlatform(self, url='http://admin.hoywe.com/api.php?hash=GO5KZ') self.write(result) return ''' wechat = self.wechat wechat.parse_data(self.request.body) message = wechat.get_message() response = None if isinstance(message, TextMessage): #wechat.send_text_message(message.source, 'test') response = wechat.response_text(content=u'文字信息') elif isinstance(message, VoiceMessage): response = wechat.response_text(content=u'语音信息') elif isinstance(message, ImageMessage): # 需要下载图片 def downloadImageFile(): http_client = tornado.httpclient.AsyncHTTPClient() http_client.fetch(message.picurl, callback=done) def done(response): with open("static/upload/images/" + message.media_id + '.jpg', "w") as f: f.write(response.body) downloadImageFile() # 检查用户是否存储了,没有的话存之 wechat_user_info = public_db.getWechatUserByOpenid(message.source) if wechat_user_info: pass else: wechat_user_info = wechat.get_user_info(message.source) pg.insert('wechat_user', **wechat_user_info) pg.insert('upload_info', openid=message.source, media_id=message.media_id) response = wechat.response_text(content=u'图片已经保存,请继续向我们发送对图片的描述') elif isinstance(message, VideoMessage): response = wechat.response_text(content=u'视频信息') elif isinstance(message, LinkMessage): wechat_user_info = wechat.get_user_info(message.source) response = wechat.response_text(content=u'链接信息') elif isinstance(message, LocationMessage): response = wechat.response_text(content=u'地理位置信息') elif isinstance(message, EventMessage): # 事件信息 if message.type == 'subscribe': # 关注事件(包括普通关注事件和扫描二维码造成的关注事件) if message.key and message.ticket: # 如果 key 和 ticket 均不为空,则是扫描二维码造成的关注事件 user_name = message.key.replace('qrscene_', '') wechat_oper.saveUserInfo(wechat, message.source) wechat_oper.bindUser(user_name, message.source) #response = wechat.response_text(content=u'用户尚未关注时的二维码扫描关注事件') response = wechat.response_text(content=u'感谢你关注FollowCenter, %s follow 的动态信息将会发送到您的微信上' % user_name) else: response = wechat.response_text(content=u'普通关注事件') elif message.type == 'unsubscribe': response = wechat.response_text(content=u'取消关注事件') elif message.type == 'scan': user_name = message.key wechat_oper.saveUserInfo(wechat, message.source) wechat_oper.bindUser(user_name, message.source) #response = wechat.response_text(content=u'用户已关注时的二维码扫描事件') response = wechat.response_text(content=u'成功与%s绑定, 其follow 的动态信息将会发送到您的微信上' % user_name) elif message.type == 'location': response = wechat.response_text(content=u'上报地理位置事件') elif message.type == 'click': response = wechat.response_text(content=u'自定义菜单点击事件') elif message.type == 'view': response = wechat.response_text(content=u'自定义菜单跳转链接事件') self.write(response)