def save_formid(session_data, formid): openid = session_data['openid'] LOGGER.info("openid: %s, formid: %s", openid, formid) if formid.find('mock') != -1: return formid_obj = FormId(openid=openid, formid=formid) formid_obj.save()
def send_template_poetry(user_data, poetry_data): author_id = poetry_data['author_id'] author = poetry_data['author'] title = poetry_data['title'] openid = user_data['openid'] recited = user_data['recited'] if not isinstance(author, unicode): author = unicode(author, 'utf-8') if not isinstance(title, unicode): title = unicode(title, 'utf-8') form_obj = FormId(openid=openid) form_record = form_obj.get_one_formid() if not form_record: LOGGER.info("openid: %s not found formid", openid) return {} template_id = 'tm5oAyq-YcBJhP2EOIAskBbGZp2Iyq0JVzfv0083e-Y' data = {} data['touser'] = openid data['template_id'] = template_id data['form_id'] = form_record['formid'] data['page'] = '/pages/authorPoetry/authorPoetry?id=%s' % author_id keywords = {} keywords['keyword1'] = {'value': u'每日一学'.encode("utf-8")} keywords['keyword2'] = {'value': title.encode("utf-8")} keywords['keyword3'] = {'value': (u"%s诗词" % author).encode("utf-8")} keywords['keyword4'] = {'value': recited} keywords['keyword5'] = {'value': u"点击进去查看详情".encode("utf-8")} data['data'] = keywords LOGGER.info(data) client = WechatClient(XCX_APPID, XCX_APPSEC, session=data_cache) res = client.weapp.send_template_msg(data) return res
def pay_goods_id(gid, count, session, pay_type): ''' pay by the count and the id the goods; input: gid --- the id of the goods; count --- the count of the goods; session --- the login session of the user; pay_type --- payment type; ''' user_session = check_session(session) if not user_session: return 10000, u'login expire' LOG.info(user_session) user_openid = user_session['openid'] order_obj, data = place_order_by_gid(gid, count, user_openid, pay_type) if not order_obj: return -1, data total_fee = order_obj.total_fee out_trade_no = order_obj.out_trade_no desc = order_obj.body place_order_ret, ret_msg = unified_order(total_fee, pay_type, out_trade_no, user_openid, desc) if not place_order_ret: return -1, u'place order to wx error: %s' % ret_msg return 0, ret_msg
def wxpay_ok(data): res = xml_to_dict(data) LOG.info("wxpay pushed data:%s", res) out_trade_no = res['out_trade_no'] sign = res.pop('sign', '') real_sign = pay_sign(res, PAY_KEY) update_data = {} if real_sign != sign: LOG.error("id: %s, got unexcepted sign: %s, real: %s", out_trade_no, sign, real_sign) update_data.update({'status': OrderStatus.SIGNERROR.value}) return ret_code = res['return_code'] ret_msg = res.get('return_msg') if ret_code != 'SUCCESS': update_data.update({'status': OrderStatus.WXERROR.value}) LOG.error("id: %s, msg: %s", out_trade_no, ret_msg) return if not _check_order(out_trade_no): update_data.update({'status': OrderStatus.ORDERERROR.value}) LOG.error("id: %s, query error", out_trade_no) return transaction_id = res['transaction_id'] update_data.update({ "transaction_id": transaction_id, 'update_time': int(time.time() * 1000), "status": OrderStatus.OK.value }) order_instance = Order(out_trade_no=out_trade_no) order_instance.update(update_data) openid = res['openid'] do_add_credit(openid, out_trade_no)
def send_miniapp(openid, appid, title, page, thumb_path): data = {} thumb_media_id = '' try: res = CLIENT.material.add_temp_material("image", thumb_path) thumb_media_id = res.get("media_id", '') if isinstance(thumb_media_id, unicode): thumb_media_id = thumb_media_id.encode("utf-8") except Exception as ex: LOGGER.error("upload tmp media error: %s", ex) data['touser'] = openid data['msgtype'] = 'miniprogrampage' data['miniprogrampage'] = { 'title': title, 'appid': appid, 'pagepath': page, 'thumb_media_id': thumb_media_id } ret = False try: data = json.dumps(data, ensure_ascii=False) res = CLIENT.kefu_msg.send(data) if 'errcode' in res and res['errcode'] == 0: ret = True else: LOGGER.error("openid: %s, error: %s", openid, res.get("errmsg")) except Exception as ex: LOGGER.info("openid: %s, ex: %s", openid, ex) return ret
def login_ok(event_data): user_openid = event_data.get('FromUserName') qr_scene = event_data.get('EventKey') if not user_openid or not qr_scene: LOG.error("got error data: %s", event_data) return uid = qr_scene.split('_')[-1] if not uid: LOG.error("qrscene error: %s", qr_scene) return res = get_subscribe_userdata(user_openid) if res['errcode'] != 0: LOG.error("get userinfo error: %s", res.get("errmsg")) nickname = '' avatar = '' else: userinfo = res['data'] LOG.info(userinfo) nickname = userinfo.get("nickname", '') avatar = userinfo.get("headimgurl", '') user_obj = MpUser(uid=uid, nickname=nickname, avatar=avatar, openid=user_openid, status=0) user_obj.save_mpuser() return
def local_search(params): keyword = params.get("keyword") page = int(params.get("page", 1)) count = int(params.get("count", 20)) sort = int(params.get('tid', 1)) if isinstance(keyword, unicode): keyword = keyword.encode("utf-8") share_text_info = find_goods_info(keyword) if share_text_info: num_id = share_text_info['num_id'] save_goods_info(num_id, share_text_info) tmp = _ship_miniapp(share_text_info) return {'errcode': 0, 'data': [tmp]} data = [] sort_dict = {} if sort == 8: sort_dict.update({'coupon_amount': -1}) elif sort == 6: sort_dict.update({'sales': -1}) elif sort == 7: sort_dict.update({'coupon_fee': 1}) elif sort == 9: super_params = { 'keyword': keyword, 'page': page, 'count': count, 'yq': 0, 'tid': 0 } return super_search_miniapp(super_params) data = searcher.search(keyword, sort_dict, page=page, count=count) # ids = map(lambda x: int(x['id']), data) # LOG.info("data: %s", data) LOG.info('keyword: %s, ret: %s', keyword, len(data)) table_dict = {} ordered_id = [] for item in data: table = item.get("table", 'goods') table_dict.setdefault(table, []) table_dict[table].append(int(item['id'])) ordered_id.append(int(item['id'])) goods_obj = TbkGoods() data_dict = {} for table, ids in table_dict.items(): goods_obj.__table__ = table cond = {'num_id': {'$in': ids}} goods_list = goods_obj.find_goods_by_cond(cond, 1, count=100) for goods in goods_list: if goods.get('num_id') is None: LOG.info(goods['_id']) continue tmp = _ship_miniapp(goods) data_dict[goods['num_id']] = tmp result = [] for _id in ordered_id: tmp = data_dict.get(_id) if not tmp: continue result.append(tmp) return {'errcode': 0, 'data': result}
def upload_image(user, files): result_dict = {} for filename, file_storage in files.items(): url = upload_image_qiniu(file_storage.read()) LOGGER.info("filename: %s, url: %s, type: %s", filename, url, file_storage.mimetype) result_dict[file_storage.filename] = url return result_dict
def process(self): LOG.info(self.message_instance.msg_id) event_type = self.message_instance.event_type handler = MESSAGE_HANDLER.get(event_type) LOG.info("type: %s, handler: %s", event_type, handler) if handler: return handler(self.message_instance, self.visitor, self.channel).process() return
def code2userinfo(code): user_token_data = CLIENT.oauth.fetch_user_access_token(code) LOGGER.info(user_token_data) openid = user_token_data['openid'] refresh_token = user_token_data['refresh_token'] userdata = CLIENT.oauth.get_user_info(openid) userdata.update({"refresh_token": refresh_token}) LOGGER.info(userdata) return userdata
def wrapper(self, url, **kwargs): LOG.info(kwargs) res = func(self, url, **kwargs) if not res: return {'errcode': -1, 'errmsg': "no data return"} he_res = res['HeWeather6'][0] if he_res['status'] != 'ok': LOG.error("he weather return error: %s", he_res) return {"errcode": -1, 'errmsg': he_res['status']} return {'errcode': 0, 'data': he_res}
def check_image(sio): content = sio.read() filename = '/tmp/{}'.format(uuid.uuid4().hex) with open(filename, 'w') as _file: _file.write(content) res = check_img_risk(filename) LOGGER.info(res) if res.get('errcode') != 0: return False return True
def reply(self): '''reply content''' event_key = self.event_obj.key LOGGER.info(event_key) if event_key == 'we': return TextReply(message=self.event_obj, content=MY_MSG) elif event_key == 'randomPoetry': ret = send_random_poetry(self.event_obj.source) LOGGER.info("send openid: %s, ret: %s", self.event_obj.source, ret) return EmptyReply(message=self.event_obj)
def search_coupon_by_id(num_iid, TBK_REQ_CLIENT=None, fields_goods=False): if TBK_REQ_CLIENT: client = TBK_REQ_CLIENT else: client = tbk_client res = {'errcode': -1} goods_info = get_goods_info(num_iid) if not goods_info: res.update({'errmsg': u"商品不存在"}) return res title = goods_info['title'] page = 1 count = 200 sp = SearchParams() sp.keyword = title sp.page = int(page) sp.count = int(count) sp.is_tmall = 'false' sp.has_coupon = 'false' result = [] try: # result = client.super_search(sp) result = _super_search(sp, client) result = result['tbk_dg_material_optional_response'][ 'result_list']['map_data'] except Exception as ex: LOG.error("super search ex: %s, word: %s", ex, title, exc_info=True) res.update({'errmsg': u"exception: %s" % ex}) return res coupon_share_url = '' for goods in result: goods = _ship_goods_supers(goods) search_id = goods.get("num_id") if not search_id: continue LOG.info( "title: %s, good: %s, coupon: %s", title, goods['title'], goods['coupon_amount']) if num_iid == goods['num_id']: coupon_share_url = goods['coupon_share_url'] break if not coupon_share_url: res.update({'errmsg': u"获取失败,请稍后重试"}) return res if fields_goods: data = goods else: tpw = generate_tpw(coupon_share_url, title, goods['pic_url']) data = { 'coupon_tpw': tpw, 'num_iid': num_iid, "coupon_url": coupon_share_url } res.update({'errcode': 0, 'data': data}) return res
def get_data(self, params): res = self.validate(params) if res['errcode'] != 0: return res cond = self.build_condition() sort = self.get_sort_field() LOG.info("cond: %s, sort: %s", cond, sort) goods_obj = TbkGoods() ret = goods_obj.find_goods_by_cond(cond, self.page, self.count) if sort: ret.sort(sort) return {'errcode': 0, 'data': ret}
def find(self, table, cond, fields=[], page=1, count=20, sort=None): LOG.info("cond: %s, sort: %s", cond, sort) skip = (page - 1) * count if not fields: cursor = self.db[table].find(cond) else: cursor = self.db[table].find(cond, fields) if sort: cursor = cursor.sort(sort).skip(skip).limit(count) else: cursor = cursor.skip(skip).limit(count) return cursor
def user_share_morning(session_data, banner, content): res = check_msg_risk(content) LOG.info('content: %s, risk: %s', content, res) if res.get('errcode') != 0: res['errmsg'] = u'文本包含敏感词汇,请修改后重新提交' return res banner = get_local_path_from_url(banner) res = check_img_risk(banner) LOG.info('banner: %s, risk: %s', banner, res) if res.get('errcode') != 0: res['errmsg'] = u'图片包含敏感信息,请更换后重新提交' return res local_path = draw_morning_image(banner, '', '', '', content, 21) return {'errcode': 0, 'data': {'url': get_url_from_path(local_path)}}
def upload_pic(session_data, files): res = {'errcode': 0} result_dict = {} for filename, file_storage in files.items(): if not check_image(file_storage): res['errcode'] = -1 res['errmsg'] = u'图片包含敏感信息,请更换图片' return res file_storage.seek(0) url = upload_file_to_server(file_storage.read()) LOGGER.info("filename: %s, url: %s, type: %s", filename, url, file_storage.mimetype) result_dict[filename] = url res.update({'data': result_dict}) return res
def get_weather_by_ip(ip): # ip = '61.183.69.254' ip_instance = IPData() address = ip_instance.get_ip_location(ip) if not address: LOGGER.info("ip: %s, not found address: %s", ip, address) return '' weather_info = ip_instance.get_ip_weather(ip) # weather_info = get_weather_msg_hefeng(ip, address) # weather_info = get_weather_msg_turing(address) if isinstance(weather_info, unicode): weather_info = weather_info.encode("utf-8") if isinstance(address, unicode): address = address.encode("utf-8") return address + ' ' + weather_info
def crawler(date_str=None): if date_str is None: today = datetime.now() date_str = today.strftime('%Y-%m-%d') sen_instance = SentenceDaily(**{'date_str': date_str, 'type': 1}) if sen_instance.get_one_sentence(): LOG.info('date: %s already exists', date_str) return sen_data = get_sentence(date_str) sen_instance = SentenceDaily(**sen_data) succ, ret = sen_instance.save_data() if succ: LOG.info('save sentence: %s, ret: %s', date_str, ret) else: LOG.error('save sentence: %s, error: %s', date_str, ret)
def update_poetry(poetry_id, update_data): poetry_instance = Poetry(id=poetry_id) poetry_data = poetry_instance.find_poetry_by_id() if not poetry_data: return False, u"找不到记录" real_update_data = {} for key, value in update_data.items(): old_value = poetry_data.get(key) if isinstance(value, unicode): value = value.encode("utf-8") if old_value != value: real_update_data[key] = value if not real_update_data: return True, "OK" ret = poetry_instance.update_poetry_by_id(real_update_data) LOGGER.info("id: %s, data: %s, ret: %s", poetry_id, real_update_data, ret) return True, "OK"
def miniapp_message_receive(*args, **kwargs): token = 'diandishici' encoding_key = 'ssI29ef6wBsN55tDUBQRXp5P8MEdzGKDCRAJPxhkVG6' method = request.method.lower() if method == 'get': try: check_signature(token, kwargs['signature'], kwargs['timestamp'], kwargs['nonce']) except Exception: abort(403) return kwargs.get("echostr") elif method == 'post': data = request.data ret = process_miniapp_push(data, kwargs, encoding_key, token) LOGGER.info("msg_sign: %s, ret: %s", kwargs['signature'], ret) if ret.get("data"): return ret.get("data") return 'success'
def decrypt_tpw(tpw): ''' from tpw to long url; ''' resp = tbk_client.decrypt_tpw(tpw) _LOGGER.info(resp) if resp is None: return {} resp_data = resp.get('wireless_share_tpwd_query_response', {}) tpw_info = {} suc = resp_data.get('suc') if suc: title = resp_data.get('content', '') coupon_url = resp_data['url'] _id = get_gid_coupon_url(coupon_url) tpw_info['title'] = title tpw_info['id'] = _id return tpw_info
def crawler_one_author(link): print(link) content = download_page(link) if not content: return -1 # if should_crawler_author(link): # link = 'http://www.shicimingju.com/chaxun/zuozhe/13.html' author_data = _crawler_author(content) author_data = check_save_author(author_data) dom = fromstring(content) detail_list = dom.xpath("//h3/a/@href") for url in detail_list: detail_url = urljoin(link, url) try: poetry_data = crawler_one_poetry(detail_url, author_data) print save_crawled_poetry(poetry_data) except Exception as ex: LOG.info("url: %s, ex: %s", detail_url, ex)
def process_push_v2(xml_data, appid): """ reply message passive and return message; """ if not xml_data: return message = xmltodict.parse(xml_data)['xml'] message_type = message['MsgType'].lower() message_class = MESSAGE_TYPES.get(message_type, UnknownMessage) message_obj = message_class(message) ret = EmptyReply(message=message_obj) LOGGER.info(message_obj) message_handler_class = MESSAGE_HANDLER.get(message_obj.type) if message_handler_class is None: LOGGER.info("type: %s not found handler", message_obj.type) else: msg_handler_obj = message_handler_class(message_obj) ret = msg_handler_obj.reply return ret
def query_text(self, content, uid=None, location=None): ''' @parameter : param uid --- userid : param location --- user location;{'city': '北京', 'province':'北京', 'street': ''} ''' if isinstance(content, unicode): content = content.encode("utf-8") req_data = {"reqType": 0} req_data['perception'] = {"inputText": {'text': content}} if location: req_data['perception'].update({'selfInfo': {'location': location}}) req_data['userInfo'] = { 'apiKey': random.choice(self.API_KEY), 'userId': uid if uid else self.DEFAULT_UID } resp = self._HTTP_CLIENT.post(self.URL, json=req_data) LOGGER.info("turling query: %s, res: %s", content, resp) return self._handle_result(resp)
def update_banner(banner_id, params): poetry_id = params.get("poetry_id") banner_image = params.get("banner_url") enabled = params.get("enabled") if poetry_id is None and banner_image is None and enabled is None: return {"errcode": 400, "errmsg": "params error"} update_data = {} if poetry_id is not None: update_data['page_path'] = DETAIL_PAGE.format(poetry_id) if banner_image is not None: update_data['banner_url'] = banner_image if enabled is not None: update_data['enabled'] = int(enabled) banner_obj = Banner(id=banner_id) banner = banner_obj.get_banner_by_id() if not banner: return {"errcode": 404, 'errmsg': "Not Found"} LOG.info("update id: %s, data: %s", banner_id, update_data) banner_obj.update_banner_by_id(update_data) return {'errcode': 0}
def _query_ip_from_baidu(self, ip): url = 'http://www.baidu.com/s?wd=%s' url = url % ip ip_xpath = '//div[@id="content_left"]//div[@class="c-border"]'\ '//table/tr/td' content = self.http_client.get(url) dom = fromstring(content) ip_content = dom.xpath(ip_xpath) if ip_content: ip_content = ip_content[0] ip_content = ip_content.xpath("string()") ip_content = ip_content.strip() ip_content = ip_content.replace("\n", '') ip_content = ip_content.replace("\t", '') address = self._match_address(ip_content) # if address: # weather_cache.cache_ip_location(ip, address) else: address = '' LOGGER.info("ip: %s, addr: %s", ip, address) return address
def login_qrcode(union_id): '''带参数二维码关注后登陆''' default_expire = 86400 data = {} data['expire_seconds'] = default_expire data['action_name'] = 'QR_STR_SCENE' data['action_info'] = {"scene": {'scene_str': union_id}} res = None try: res = CLIENT.qrcode.create(data) except Exception as ex: LOGGER.error("qrcode get ex: %s", ex, exc_info=True) ticket = res.get("ticket") if not ticket: LOGGER.info("create qrcode: %s, res:%s", union_id, res) return resp = CLIENT.qrcode.showqrcode(ticket) if 'errcode' in resp and resp['errcode'] != 0: LOGGER.info("union_id: %s error: %s", union_id, resp.get("errmsg", '')) return return resp.get("data", '')
def generate_share_pic(session_data, poetry_id, banner_url=None): start = time.time() openid = session_data['openid'] user_obj = User(openid=openid) user = user_obj.find_by_openid() if not user: return False, u"请到首页点击“我的->点击登录”后重新生成分享图" poetry_obj = Poetry(id=poetry_id) poetry = poetry_obj.find_poetry_by_id() if not poetry: return False, u"找不到诗词" if banner_url.startswith('http'): return False, u'请先上传图片' if not banner_url: banner_path = random_choose_banner() else: banner_path = get_local_path_from_url(banner_url) res = check_img_risk(banner_path) LOGGER.info('check image res: %s', res) if res.get('errcode') != 0: return False, u'图片存在敏感信息,请更换后重新提交' LOGGER.info("read dbdata: %s", time.time() - start) scene = "id={poetry_id}&uid={uid}".format(poetry_id=poetry_id, uid=user['id']) headimg_url = user['headimgurl'] qrcode_path = get_qrcode(openid, poetry_id, scene) LOGGER.info("get qrcode from wx: %s", time.time() - start) nickname = user['nickname'] title = poetry['title'] content = poetry['content'] dynasty = poetry['dynasty'] author_name = poetry['author'] headimg_path = get_headimg_path(headimg_url, openid) LOGGER.info("here: %s", time.time() - start) share_pic_path = draw_poetry_share(nickname, headimg_path, qrcode_path, banner_path, title, content, dynasty, author_name) LOGGER.info("draw image: %s", time.time() - start) return True, get_url_from_path(share_pic_path)