Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import xml
- import xml.etree.ElementTree as xml_tree
- api_key = "__API_KEY__"
- api_secret = "__API_SECRET__"
- user_id = "__USER_ID__"
- shop_id = "__SHOP_ID__"
- design_path = "__PATH_TO_PNG__"
- design_name = "__DESIGN_NAME__"
- design_desc = "__DESIGN_DESCRIPTION__"
- xlink = "{http://www.w3.org/1999/xlink}"
- shop_url = "https://api.spreadshirt.net/api/v1/shops/" + shop_id
- api_end = "?apiKey=" + api_key
- def http_error(error):
- return requests.status_codes._codes[error][0]
- def response_debug(response):
- print(" method: {}".format(response.request.method))
- print(" url: {}".format(response.url))
- print(" statuscode: {} [{}]".format(http_error(response.status_code), response.status_code))
- if(response.text.__len__() != 0):
- print(" response: '" + response.text + "' [" + response.text.__len__().__str__() + " chars]")
- return
- def access_xml_child(root, child_name):
- # split attribute if there is one
- attrib = None
- if(child_name.find("@") != -1):
- # split child name
- attrib = child_name.split("@")[1]
- child_name = child_name.split("@")[0]
- # split attrib name
- attrib_name = attrib.split("=")[0]
- attrib_value = attrib.split("=")[1]
- # iterate over children
- for child in root.getchildren():
- if(child.tag.find(child_name) != -1):
- if(attrib == None or child.attrib[attrib_name] == attrib_value):
- return child
- return None
- def access_xml_path(root, path):
- # split path into children
- children = path.split("/")
- # filter out empty ones (and skip first one, since we already are at the 'root')
- children = list(filter(None, children))[1:]
- # access children after each other
- current = root
- for child in children:
- current = access_xml_child(current, child)
- if(current == None):
- return None
- return current
- # create design
- print("creating design")
- # setup payload + url
- payload = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?> \
- <design xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://api.spreadshirt.net'> \
- <name>" + design_name + "</name> \
- <description>" + design_desc + "</description> \
- </design>"
- url = shop_url + "/designs" + api_end
- # do requests
- post_design = requests.post(url, data=payload)
- # debug output
- response_debug(post_design)
- # extract design_url
- root = xml_tree.fromstring(post_design.text)
- design_id = root.attrib["id"]
- print(" extracted: " + design_id)
- print("")
- # fetch full xml data
- print("fetching full xml data")
- # setup url
- url = shop_url + "/designs/" + design_id + api_end
- # do requests
- get_xml = requests.get(url)
- # debug output
- response_debug(get_xml)
- # extract design_url
- root = xml_tree.fromstring(get_xml.text)
- upload_url = access_xml_path(root, "/design/resources/resource@type=montage").attrib[xlink + "href"]
- print(" extracted: " + upload_url)
- print("")
- # upload the pixel design
- print("uploading the pixel design")
- # setup file + url
- file_handle = open(design_path, "rb")
- file_content = file_handle.read()
- url = upload_url + api_end
- # do request
- put_design = requests.put(url,
- data=file_content,
- headers={"content-type": "image/png"}
- )
- # debug output
- response_debug(put_design)
- print("")
- # create the product
- print("creating the product")
- # setup payload + url
- payload = "<product xmlns:xlink='http://www.w3.org/1999/xlink' xmlns='http://api.spreadshirt.net'> \
- <productType id='6'/> \
- <appearance id='1'/> \
- <restrictions> \
- <freeColorSelection>false</freeColorSelection> \
- <example>false</example> \
- </restrictions> \
- <configurations> \
- <configuration type='design'> \
- <printArea id='4'/> \
- <printType id='1'/> \
- <offset unit='mm'> \
- <x>60.0</x> \
- <y>70.0</y> \
- </offset> \
- <content dpi='25.4' unit='mm'> \
- <svg> \
- <image transform='' width='200.025' height='194.945' printColorIds='1' designId='" + design_id + "'/> \
- </svg> \
- </content> \
- <restrictions> \
- <changeable>false</changeable> \
- </restrictions> \
- </configuration> \
- </configurations> \
- </product>"
- url = shop_url + "/products" + api_end
- # do requests
- post_prodct = requests.post(url, data=payload)
- # debug output
- response_debug(post_prodct)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement