Python3 Demo App using API IAI IdoSell

Przykładowa Aplikacja w Python3 korzystająca z API IAI IdoSell IdoSell IAI IAIShop Łączenie sie do API i generowanie system_key Pobieranie stanów magazynowych dla towarów o podanych w liście ID wymagany plik .env ze zdefiniowanymi: PANEL_LOGIN_MS = "" PANEL_PASS_MS = "" DOMENA_MS = ""
 avatar
MrozyHipis
python
2 years ago
3.6 kB
23
Indexable
from __future__ import print_function
import os, sys
import json, requests
import csv
import hashlib
from datetime import datetime
from datetime import date
import markdown
import re
import logging
import copy
from pprint import pprint
from dotenv import load_dotenv
from pip._internal.utils.logging import setup_logging

load_dotenv()
logging.basicConfig(filename="debug_log.log",
                    filemode='a',
                    format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
                    datefmt='%H:%M:%S',
                    level=logging.DEBUG)

iai_username = os.getenv("PANEL_LOGIN_MS")
iai_password = os.getenv("PANEL_PASS_MS")
iai_url = os.getenv("DOMENA_MS")

api_gates = {"productsstocks": "https://" + iai_url + '/api/?gate=productsstocks/get/180/json'}

def get_iai_system_key():
    pass_hash = hashlib.sha1(iai_password.encode()).hexdigest()
    datestr = datetime.now().strftime("%Y%m%d")
    str2hash = datestr + pass_hash
    str2hash = str2hash.encode()
    return hashlib.sha1(str2hash).hexdigest()


def get_product_stocks(ids=[]):

    if not ids:
        return

    request = {
        'authenticate': {'system_login': iai_username, 'system_key': get_iai_system_key()},
        'params': {'products': []}
    }
    for iai_id in ids:
        request["params"]["products"].append({'identType': "id", 'identValue': iai_id})

    req_dump = copy.deepcopy(request)
    return requests.post(api_gates["productsstocks"], json=request)


def print_stocks(json_response):
    for product in json_response.json()["results"]:
        ALLSTOCKS = 0
        OUTSIDESTOCK = 0
        OWNSTOCK = 0
        for size in product["quantities"]["dispositions"]["sales"]:
            ALLSTOCKS = ALLSTOCKS + size["allstocks"]
            OUTSIDESTOCK = OUTSIDESTOCK + size["outsidestock"]
            OWNSTOCK = OWNSTOCK + size["ownstock"]
            #print("• " + size["size_id"] + ": allstocks : " + str(size["allstocks"]))
            #print("• " + size["size_id"] + ": outsidestock : " + str(size["outsidestock"]))
            #print("• " + size["size_id"] + ": ownstock " + str(size["ownstock"]))

        ZN = 0
        if product["quantities"]["orders_unfinished"]:
            for stock in product["quantities"]["orders_unfinished"]:
                #print("M" + str(stock["stock_id"]))
                for size in stock["sizes"]:
                    #print("• " + size["size_id"] + " ZN : " + str(size["quantity"]))
                    ZN = ZN + int(size["quantity"])

        M = []
        for stock in product["quantities"]["stocks"]:
            #print("M"+str(stock["stock_id"]))
            QUANTITY = 0
            for size in stock["sizes"]:
                #print("• " + size["size_id"] + ": " + str(size["quantity"]))
                QUANTITY = QUANTITY + size["quantity"]
            M.append({"M"+str(stock["stock_id"]): QUANTITY})

        print(product["ident"]["identType"]+": " + product["ident"]["identValue"])
        print("ZN: " + str(ZN))
        print("ALLSTOCKS: " + str(ALLSTOCKS))
        print("OUTSIDESTOCK: " + str(OUTSIDESTOCK))
        print("OWNSTOCK: " + str(OWNSTOCK))
        pprint(M)


def main():
    global iai_username
    global iai_password
    global iai_url
    logging.info('Użytkownik: %s. URL sklepu: %s', iai_username, iai_url)
    response = get_product_stocks(["2241"])
    #pprint(response.json())
    print_stocks(response)


if __name__ == "__main__":
    sys.exit(main())
Editor is loading...