溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-09-16 04:30:45 來源:腳本之家 閱讀:136 作者:zhizunyu2009 欄目:開發(fā)技術(shù)

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

應(yīng)用背景:美團平臺商品的上下架狀態(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():#獲取當前的時間戳
  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+"售價不一致,美團:"+skus['price']+',數(shù)據(jù)庫:'+str(shopPrice)
        file_edit(file_name,wirte_price)
      if float(skus['stock']) != stock:
        wirte_stock = sku_id+"庫存不一致,美團:"+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)不一致,美團:"+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īng)典教程》及《Python文件與目錄操作技巧匯總》

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

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI