Advertisement
Guest User

CreateProducts

a guest
May 30th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.56 KB | None | 0 0
  1. import requests
  2. import xml
  3. import xml.etree.ElementTree as xml_tree
  4.  
  5. api_key     = "__API_KEY__"
  6. api_secret  = "__API_SECRET__"
  7. user_id     = "__USER_ID__"
  8. shop_id     = "__SHOP_ID__"
  9.  
  10. design_path = "__PATH_TO_PNG__"
  11. design_name = "__DESIGN_NAME__"
  12. design_desc = "__DESIGN_DESCRIPTION__"
  13.  
  14. xlink = "{http://www.w3.org/1999/xlink}"
  15. shop_url = "https://api.spreadshirt.net/api/v1/shops/" + shop_id
  16. api_end = "?apiKey=" + api_key
  17.  
  18. def http_error(error):
  19.     return requests.status_codes._codes[error][0]
  20. def response_debug(response):
  21.     print("    method:     {}".format(response.request.method))
  22.     print("    url:        {}".format(response.url))
  23.     print("    statuscode: {} [{}]".format(http_error(response.status_code), response.status_code))
  24.     if(response.text.__len__() != 0):
  25.         print("    response:   '" + response.text + "' [" + response.text.__len__().__str__() + " chars]")
  26.     return
  27. def access_xml_child(root, child_name):
  28.     # split attribute if there is one
  29.     attrib = None
  30.     if(child_name.find("@") != -1):
  31.         # split child name
  32.         attrib     = child_name.split("@")[1]
  33.         child_name = child_name.split("@")[0]
  34.         # split attrib name
  35.         attrib_name  = attrib.split("=")[0]
  36.         attrib_value = attrib.split("=")[1]
  37.     # iterate over children
  38.     for child in root.getchildren():
  39.         if(child.tag.find(child_name) != -1):
  40.             if(attrib == None or child.attrib[attrib_name] == attrib_value):
  41.                 return child
  42.     return None
  43. def access_xml_path(root, path):
  44.     # split path into children
  45.     children = path.split("/")
  46.     # filter out empty ones (and skip first one, since we already are at the 'root')
  47.     children = list(filter(None, children))[1:]
  48.     # access children after each other
  49.     current = root
  50.     for child in children:
  51.         current = access_xml_child(current, child)
  52.         if(current == None):
  53.             return None
  54.     return current
  55.  
  56. # create design
  57. print("creating design")
  58. # setup payload + url
  59. payload = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> \
  60. <design xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://api.spreadshirt.net'> \
  61.        <name>" + design_name + "</name> \
  62.        <description>" + design_desc + "</description> \
  63. </design>"
  64. url = shop_url + "/designs" + api_end
  65. # do requests
  66. post_design = requests.post(url, data=payload)
  67. # debug output
  68. response_debug(post_design)
  69. # extract design_url
  70. root = xml_tree.fromstring(post_design.text)
  71. design_id = root.attrib["id"]
  72. print("    extracted:  " + design_id)
  73. print("")
  74.  
  75. # fetch full xml data
  76. print("fetching full xml data")
  77. # setup url
  78. url = shop_url + "/designs/" + design_id + api_end
  79. # do requests
  80. get_xml = requests.get(url)
  81. # debug output
  82. response_debug(get_xml)
  83. # extract design_url
  84. root = xml_tree.fromstring(get_xml.text)
  85. upload_url = access_xml_path(root, "/design/resources/resource@type=montage").attrib[xlink + "href"]
  86. print("    extracted:  " + upload_url)
  87. print("")
  88.  
  89. # upload the pixel design
  90. print("uploading the pixel design")
  91. # setup file + url
  92. file_handle  = open(design_path, "rb")
  93. file_content = file_handle.read()
  94. url = upload_url + api_end
  95. # do request
  96. put_design = requests.put(url,
  97.                           data=file_content,
  98.                           headers={"content-type": "image/png"}
  99. )
  100. # debug output
  101. response_debug(put_design)
  102. print("")
  103.  
  104. # create the product
  105. print("creating the product")
  106. # setup payload + url
  107. payload = "<product xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://api.spreadshirt.net'> \
  108.    <productType id='6'/> \
  109.    <appearance id='1'/> \
  110.    <restrictions> \
  111.        <freeColorSelection>false</freeColorSelection> \
  112.        <example>false</example> \
  113.    </restrictions> \
  114.    <configurations> \
  115.        <configuration type='design'> \
  116.            <printArea id='4'/> \
  117.            <printType id='1'/> \
  118.            <offset unit='mm'> \
  119.                <x>60.0</x> \
  120.                <y>70.0</y> \
  121.            </offset> \
  122.            <content dpi='25.4' unit='mm'> \
  123.                <svg> \
  124.                    <image transform='' width='200.025' height='194.945' printColorIds='1' designId='" + design_id + "'/> \
  125.                </svg> \
  126.            </content> \
  127.            <restrictions> \
  128.                <changeable>false</changeable> \
  129.            </restrictions> \
  130.        </configuration> \
  131.    </configurations> \
  132. </product>"
  133. url = shop_url + "/products" + api_end
  134. # do requests
  135. post_prodct = requests.post(url, data=payload)
  136. # debug output
  137. response_debug(post_prodct)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement