侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python实现比对美团接口返回数据和本地mongo数据是否一致示例

Python  /  管理员 发布于 7年前   183

本文实例讲述了python实现比对美团接口返回数据和本地mongo数据是否一致。分享给大家供大家参考,具体如下:

应用背景:美团平台商品的上下架状态、库存、售价,和mongo库存储的是否一致。

tools文件内容

# -*- coding: utf-8 -*-import hashlibimport timeimport requestsdef 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 resultdef req_post_result(api_url,api_data):#post方法请求函数  req_post = requests.post(api_url,data=api_data)  result = req_post.json()  return resultdef 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_sortimport confimport datetimeimport timefrom pymongo import MongoClientapp_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_listdef 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_namedef 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程序设计有所帮助。


  • 上一条:
    python虚拟环境的安装和配置(virtualenv,virtualenvwrapper)
    下一条:
    python实现美团订单推送到测试环境,提供便利操作示例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客