日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python實(shí)現(xiàn)比對美團(tuán)接口返回數(shù)據(jù)和本地mongo數(shù)據(jù)是否一致示例

系統(tǒng) 1921 0

本文實(shí)例講述了python實(shí)現(xiàn)比對美團(tuán)接口返回數(shù)據(jù)和本地mongo數(shù)據(jù)是否一致。分享給大家供大家參考,具體如下:

應(yīng)用背景:美團(tuán)平臺商品的上下架狀態(tài)、庫存、售價,和mongo庫存儲的是否一致。

tools文件內(nèi)容

            
# -*- 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():#獲取當(dāng)前的時間戳
  t = time.time()
  return int(t)
def req_get_result(api_url,api_data):#get方法請求函數(shù)
  req_get = requests.get(api_url,api_data)
  result = req_get.json()
  return result
def req_post_result(api_url,api_data):#post方法請求函數(shù)
  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&timestamp=%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):#根據(jù)門店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&timestamp=%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):#根據(jù)門店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&timestamp=%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+"售價不一致,美團(tuán):"+skus['price']+',數(shù)據(jù)庫:'+str(shopPrice)
        file_edit(file_name,wirte_price)
      if float(skus['stock']) != stock:
        wirte_stock = sku_id+"庫存不一致,美團(tuán):"+skus['stock']+',數(shù)據(jù)庫:'+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+":狀態(tài)不一致,美團(tuán):"+is_sold+',數(shù)據(jù)庫:'+is_sold_out
        file_edit(file_name,wirte_sold)
      print('已完成',sku_id)
  client.close()


          

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設(shè)計有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 鄂尔多斯市| 万全县| 光泽县| 社会| 泰顺县| 石首市| 林口县| 杭锦旗| 内乡县| 即墨市| 府谷县| 广河县| 宁津县| 广平县| 龙川县| 买车| 东海县| 珠海市| 九龙城区| 平阴县| 永丰县| 嘉祥县| 泾源县| 久治县| 赫章县| 梧州市| 西林县| 寻乌县| 应城市| 德清县| 土默特右旗| 彭水| 铜川市| 芦溪县| 平武县| 孝感市| 阿拉尔市| 武冈市| 遂平县| 右玉县| 黑龙江省|