拉取易倉API的亞馬遜Listing資料-listing表現介面

liskov_design發表於2024-11-02

設計一個程式,按照api返回的json資料自動生成一張表,並且表的ID為xxxxxxxxxx,

提供一個通用的介面:save_date_to_table_id,這個介面應該是自動判斷存在的列的並集,然後按照id做insert操作或者是update操作。

拉去亞馬遜Listing表現資料介面程式碼如下:

import datetime
import json
import time

import requests

import op_data_web_hook as webhook

app_key = "xxxxx"
app_secret = "xxxxxx"

def get_ecc_listing_so(page, seed):
    url = "http://home.eccang.com/openApi/api/unity"
    star_time = datetime.datetime.now() + datetime.timedelta(days=-732 * seed)
    end_time = datetime.datetime.now() + datetime.timedelta(days=-732 * (seed - 1))
    data_dict = {
        "page": page,
        "page_size": 100,
        'start_time': star_time.strftime('%Y-%m-%d %H:%M:%S'),
        'end_time': end_time.strftime('%Y-%m-%d %H:%M:%S')
    }
    payload = str(json.dumps(data_dict))
    params = {
        "app_key": app_key,
        "biz_content": payload,
        "charset": 'UTF-8',
        "interface_method": "ListingPerformance",
        "nonce_str": webhook.get_uuid(),
        "service_id": "ETTIS6",
        "sign_type": "MD5",
        "timestamp": int(time.time() * 1000),
        "version": "V1.0.0"
    }
    md5key_list = []
    for key, val in params.items():
        md5key_list.append(str(key) + '=' + str(val))
    md5key_str = str('&'.join(md5key_list)) + str(app_secret)
    params["sign"] = webhook.get_md5(md5key_str)
    ps = json.dumps(params)
    headers = {
        'Content-Type': 'application/json'
    }
    response = requests.request("POST", url, headers=headers, data=ps)
    res = response.text
    res = res.replace("0000-00-00", "2000-01-01")
    dict = json.loads(res)
    dict = dict["biz_content"]
    dict = json.loads(dict)
    data = dict["data"]
    if len(data) == 0:
        return 0
    for x in data:
        print(x)
        id = str(x["id"])
        x["shop_order_amount"] = str(x["shop_order_amount"])
        x["listing_shop_sale_account_total"] = str(x["listing_shop_sale_account_total"])
        x["sub_depart_name"] = json.dumps(x["sub_depart_name"], ensure_ascii=False)
        x["tags"] = json.dumps(x["tags"], ensure_ascii=False)
        x["fulfillment_type"] = json.dumps(x["fulfillment_type"], ensure_ascii=False)
        x["compare_data"] = json.dumps(x["compare_data"], ensure_ascii=False)
        x["keywords"] = json.dumps(x["keywords"], ensure_ascii=False)
        x["principal_names"] = json.dumps(["principal_names"], ensure_ascii=False)
        if "bsr_category_ary" in x:
            bsr_category_ary = x["bsr_category_ary"]
            for bsr in bsr_category_ary:
                bsr["id"] = webhook.get_md5(id + str(bsr["bsr_num"]))
                bsr["pid"] = id
            webhook.post_data_to_db(bsr_category_ary, "ddac0125a0184039b65824572bc3bc34")
            del x["bsr_category_ary"]
        if "product_sku_json" in x:
            product_sku_json = x["product_sku_json"]
            for p in product_sku_json:
                p["id"] = webhook.get_md5(id + str(p["id"]))
                p["pid"] = id
            webhook.post_data_to_db(product_sku_json, "a67db211e9154a62b659073697517e52")
            del x["product_sku_json"]
    webhook.post_data_to_db(data, "438fd1d9258f4c12ad83998981811d80")
    return 1

# https://open.eccang.com/#/documentCenter?docId=1297&catId=0-225-225,0-177%23%2FdocumentCenter%3FdocId%3D1297&catId=0-225-225,0-177
if __name__ == '__main__':
    print("run at:" + webhook.time_to_str(datetime.datetime.now()))
    x = 1
    while True:
        if x > 100:
            break
        print(x)
        if get_ecc_listing_so(x, 1) == 0:
            break
        x = x + 1
    print("run done at:" + webhook.time_to_str(datetime.datetime.now()))

  

done

相關文章