def super_search(params): keyword = params.get("keyword") if keyword is None: return False, u"搜索关键字不能为空" page = params.get("page", 1) count = params.get("count", 50) is_tmall = params.get("is_tmall") if isinstance(page, (unicode, str)) and not page.isdigit() or\ (isinstance(count, (unicode, str)) and not count.isdigit()): return False, u"page非法" sp = SearchParams() sp.keyword = keyword sp.page = int(page) sp.count = int(count) sp.is_tmall = 'true' if is_tmall else 'false' result = [] try: # result = tbk_client.super_search(sp) result = _super_search(sp, tbk_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, keyword) return False, u"数据异常: %s" % ex goods_data = [] for goods in result: tmp = _ship_goods_supers(goods) if not tmp: continue num_id = tmp['num_id'] save_goods_info(num_id, tmp) tmp = _ship_db_goods(tmp) goods_data.append(tmp) return True, goods_data
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 super_search(self, search_params): top.setDefaultAppInfo(self.appid, self.appsec) keyword = search_params.keyword client_req = top.api.TbkDgMaterialOptionalRequest() if keyword: if isinstance(keyword, unicode): keyword = keyword.encode("utf-8") client_req.q = keyword if not keyword: client_req.cat = search_params.cat client_req.page_no = search_params.page client_req.page_size = search_params.count client_req.platform = search_params.platform client_req.is_overseas = search_params.is_overseas client_req.is_tmall = search_params.is_tmall if search_params.sort: client_req.sort = search_params.sort client_req.has_coupon = search_params.has_coupon client_req.need_free_shipment = search_params.need_free_shipment client_req.adzone_id = self.adzone_id client_req.site_id = self.site_id res = {'errcode': -1} try: res = client_req.getResponse() res['errcode'] = 0 except Exception as ex: LOG.error("super search keyword: %s, ex: %s", keyword, ex, exc_info=True) return res
def get_subscribe_userdata(openid): res = {"errcode": -1} try: userdata = CLIENT.user.get(openid) res.update({'errcode': 0, 'data': userdata}) except Exception as ex: LOGGER.error("openid: %s, ex: %s", openid, ex, exc_info=True) res.update({"errmsg": ex}) return res
def _check_order(out_trade_no): order_data = {} try: order_data = pay_client.order.query(out_trade_no) except Exception as ex: LOG.error("query order: %s, ex: %s", out_trade_no, ex) if order_data.get("trade_state") == 'SUCCESS': return True return False
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 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 decrypt_tpw(self, tpw): top.setDefaultAppInfo(self.appid, self.appsec) client = top.api.TbkTpwQueryGetRequest() client.password_content = tpw client.adzone_id = self.adzone_id res = None try: res = client.getResponse() except Exception as ex: LOG.error("tpw: %s ex: %s", tpw, ex, exc_info=True) return res
def upload_qiniu_storage(url): _http = HttpClient() res = None try: res = _http.get(url) except Exception as ex: LOGGER.error("url: %s, ex: %s", url, ex, exc_info=True) return '' if not res: return '' return upload_image_qiniu(res)
def get_ip_info(ip): weather = '----\n----\n气温--℃/--℃' lunar_date = '' today = datetime.now() try: weather = get_ip_weather(ip) lunar_date = get_lunar_date() except Exception as ex: LOGGER.error("get ip info ex: %s", ex, exc_info=True) today = datetime.now() lunar_date = u'----\n----\n星期{}'.format(WEEK_MAP[today.weekday()]) return {'weather': weather, 'lunar_date': lunar_date}
def tbk_goods_recommend(self, num_iid): top.setDefaultAppInfo(self.appid, self.appsec) client_req = top.api.TbkItemRecommendGetRequest() client_req.num_iid = str(num_iid) client_req.fields = 'num_iid,title,pict_url,small_images,'\ 'reserve_price,zk_final_price,user_type,item_url' client_req.count = 20 res = None try: res = client_req.getResponse() except Exception as ex: LOG.error("num_id: %s, ex: %s", num_iid, ex) return res
def tbk_goods_info(self, num_id, platform=2): top.setDefaultAppInfo(self.appid, self.appsec) if isinstance(num_id, list): num_id = ','.join(num_id) client_req = top.api.TbkItemInfoGetRequest() client_req.num_iids = str(num_id) client_req.platform = platform res = None try: res = client_req.getResponse() except Exception as ex: LOG.error("num_id: %s, ex: %s", num_id, ex) return res
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 super_search_miniapp(params): res = {'errcode': -1} keyword = params.get("keyword") if keyword is None: res.update({'errmsg': u"搜索关键字不能为空"}) return res page = params.get("page", 1) count = params.get("count", 50) is_tmall = params.get("is_tmall") tid = params.get("tid") yq = params.get("yq") if isinstance(page, (unicode, str)) and not page.isdigit() or\ (isinstance(count, (unicode, str)) and not count.isdigit()): res.update({'errmsg': u"page非法"}) return res yq = int(yq) tid = int(tid) sp = SearchParams() sp.keyword = keyword sp.page = int(page) sp.count = int(count) sp.is_tmall = 'true' if is_tmall else 'false' sp.has_coupon = 'true' if yq else 'false' if tid == 5: sp.sort = 'tk_total_sales_des' elif tid == 6: sp.sort = 'total_sales_des' result = [] try: # result = tbk_client.super_search(sp) result = _super_search(sp, tbk_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, keyword) res.update({'errmsg': u"数据异常: %s" % ex}) return res goods_data = [] for goods in result: tmp = _ship_goods_supers(goods) if not tmp: continue num_id = tmp['num_id'] save_goods_info(num_id, tmp) tmp = _ship_miniapp(tmp) goods_data.append(tmp) res.update({'errcode': 0, 'data': goods_data}) return res
def dg_coupon_list(self, keyword, page, count): top.setDefaultAppInfo(self.appid, self.appsec) client_req = top.api.TbkDgItemCouponGetRequest() client_req.adzone_id = self.adzone_id client_req.platform = 2 client_req.page_size = count client_req.page_no = page if isinstance(keyword, unicode): keyword = keyword.encode("utf-8") client_req.q = keyword res = None try: res = client_req.getResponse() except Exception as ex: LOG.error("keyword: %s, coupon list ex: %s", keyword, ex) return res
def _request(self, method, url, **kwargs): data_format = kwargs.pop("data_fromat", "") if not data_format: data_format = 'json' data = kwargs.get("data", {}) if data and data_format == 'json' and isinstance(data, dict): kwargs['data'] = json.dumps(data) if 'timeout' not in kwargs: kwargs['timeout'] = self.timeout if 'headers' not in kwargs: kwargs['headers'] = DEFAULT_HEADER res = self._http.request(method=method, url=url, **kwargs) try: res.raise_for_status() except requests.RequestException as reqex: LOGGER.error("request exception: %s", reqex) return self._handle_result(res)
def convert_tpw(self, coupon_url, text, logo_pic_url): if isinstance(text, unicode): text = text.encode("utf-8") if isinstance(coupon_url, unicode): coupon_url = coupon_url.encode("utf-8") if isinstance(logo_pic_url, unicode): logo_pic_url = logo_pic_url.encode("utf-8") top.setDefaultAppInfo(self.appid, self.appsec) client_req = top.api.TbkTpwdCreateRequest() client_req.text = text client_req.url = coupon_url client_req.logo = logo_pic_url res = None try: res = client_req.getResponse() except Exception as ex: LOG.error("convert tpw ex: %s", ex) return res
def upload_file(self, file_path, remote_path='images'): file_data = None try: with open(file_path, 'rb') as _file: file_data = _file.read() except Exception as ex: LOGGER.error("update file: %s ex: %s", file_path, ex) return False, ex date_str = datetime.now().strftime("%Y-%m-%d") filename = remote_path.split("/")[-1] if filename.find('.') == -1: filename = file_path.split('/')[-1] up_path = os.path.join(date_str, filename) if remote_path: up_path = os.path.join(remote_path, up_path) else: up_path = remote_path return self._upload_data(file_data, up_path)
def search_juhuasuan(self, keyword, page=1, count=20, is_post='false'): if isinstance(keyword, unicode): keyword = keyword.encode("utf-8") top.setDefaultAppInfo(self.appid, self.appsec) client_req = top.api.JuItemsSearchRequest() params_query = {} params_query['current_page'] = page params_query['page_size'] = count params_query['pid'] = self.pid params_query['postage'] = is_post params_query['word'] = keyword client_req.param_top_item_query = params_query res = None try: res = client_req.getResponse() except Exception as ex: LOG.error("ju search ex: %s", ex) return res
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 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 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 get_gid_coupon_url(coupon_url): ''' get goods id from coupon url like "https://uland.taobao.com/coupon/edetail" ''' pattern = re.compile(_ID_PATTERN) match = pattern.search(coupon_url) _id = 0 if match: _id = match.group("id") else: location = '' try: resp = requests.head(coupon_url) headers = resp.headers location = headers.get('location') except Exception as ex: _LOGGER.error("head url: %s, ex: %s", coupon_url, ex) if location: match = pattern.search(location) if match: _id = match.group('id') return long(_id)