python实现比对美团接口返回数据和本地mongo数据是否一致示例
作者:zhizunyu2009
这篇文章主要介绍了python实现比对美团接口返回数据和本地mongo数据是否一致,涉及Python基于requests模块的数据请求与比较运算相关操作技巧,需要的朋友可以参考下
本文实例讲述了python实现比对美团接口返回数据和本地mongo数据是否一致。分享给大家供大家参考,具体如下:
应用背景:美团平台商品的上下架状态、库存、售价,和mongo库存储的是否一致。
tools文件内容
# -*- coding: utf-8 -*- import hashlib import time import requests def get_md5(string):#返回字符串md5加密后的串 hl = hashlib.md5() hl.update(string.encode('utf-8')) return hl.hexdigest() def get_tamp():#获取当前的时间戳 t = time.time() return int(t) def req_get_result(api_url,api_data):#get方法请求函数 req_get = requests.get(api_url,api_data) result = req_get.json() return result def req_post_result(api_url,api_data):#post方法请求函数 req_post = requests.post(api_url,data=api_data) result = req_post.json() return result def file_edit(file_name,wr_str):#写入txt文件 f1 = open(r'D:\%s.txt'%file_name,'a') f1.write(wr_str+'\n') f1.close() def param_sort(param_dict):#传入字典,返回排序后并且连接好的字符串 keys_list = sorted(param_dict.keys()) rb_str = '' for k in keys_list: key_value = k + '=' + str(param_dict[k]) rb_str = rb_str + key_value +'&' rb_str = rb_str[0:-1] #不保留字符串末尾的& return rb_str
下面是主逻辑
# -*- coding: utf-8 -*- from tools import get_tamp,get_md5,req_get_result,file_edit,param_sort import conf import datetime import time from pymongo import MongoClient app_id = conf.appinfo[1]['app_id'] secret = conf.appinfo[1]['secret'] def get_shop_id_list(app_id,secret):#获取门店id的列表 api_url = 'http://waimaiopen.meituan.com/api/v1/poi/getids' timestamp = get_tamp() params_str = api_url+'?app_id=%s×tamp=%s'%(app_id,timestamp) sig = get_md5(params_str + secret) api_data = { 'app_id':app_id, 'sig':sig, 'timestamp':timestamp, } result = req_get_result(api_url,api_data) shop_id_list = result['data'] del shop_id_list[-1]#去掉最后一个非门店id元素 return shop_id_list def get_shop_detail(shop_id):#根据门店id,返回门店名称 api_url = 'http://waimaiopen.meituan.com/api/v1/poi/mget' timestamp = get_tamp() app_poi_codes = shop_id params_str = api_url+'?app_id=%s&app_poi_codes=%s×tamp=%s'%(app_id,app_poi_codes,timestamp) sig = get_md5(params_str + secret) api_data = { 'app_id':app_id, 'sig':sig, 'timestamp':timestamp, 'app_poi_codes':app_poi_codes } result = req_get_result(api_url,api_data) shop_name = result['data'][0]['name'] return shop_name def get_goods(shop_id):#根据门店id,查询门店商品,返回列表 api_url = 'http://waimaiopen.meituan.com/api/v1/retail/list' timestamp = get_tamp() app_poi_code = shop_id params_str = api_url+'?app_id=%s&app_poi_code=%s×tamp=%s'%(app_id,app_poi_code,timestamp) sig = get_md5(params_str + secret) api_data = { 'app_id':app_id, 'sig':sig, 'timestamp':timestamp, 'app_poi_code':app_poi_code } result = req_get_result(api_url,api_data) return result['data'] if __name__ == '__main__': shop_ids = get_shop_id_list(app_id,secret) file_name = datetime.datetime.now().strftime('%Y.%m.%d.%H.%M.%S') client = MongoClient(conf.mongo_online,conf.mongo_port) db = client['oh-product'] collection = db.outerShopSku for shop_id in shop_ids: shop_name = get_shop_detail(shop_id) goods_list = get_goods(shop_id) wirte_shop_info = shop_id + '--' + shop_name + str(len(goods_list)) +'个商品' file_edit(file_name,wirte_shop_info) for i in range(0,len(goods_list)): skus = eval(goods_list[i]['skus'])[0] sku_id = skus['sku_id'] result = collection.find({'channel':'MeiTuan','outerShopId':shop_id,'outerSkuId':sku_id}) shopPrice = result[0]['shopPrice'] #int,单位:分 stock = result[0]['stock'] #float is_sold_out = result[0]['status'] #str online/offline if round(float(skus['price'])*100) != shopPrice: wirte_price = sku_id+"售价不一致,美团:"+skus['price']+',数据库:'+str(shopPrice) file_edit(file_name,wirte_price) if float(skus['stock']) != stock: wirte_stock = sku_id+"库存不一致,美团:"+skus['stock']+',数据库:'+str(stock) file_edit(file_name,wirte_stock) if goods_list[i]['is_sold_out'] == 0: is_sold = 'offline' else: is_sold = 'online' if is_sold != is_sold_out: wirte_sold = sku_id+":状态不一致,美团:"+is_sold+',数据库:'+is_sold_out file_edit(file_name,wirte_sold) print('已完成',sku_id) client.close()
更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。