Created
March 12, 2020 20:28
-
-
Save MatthewPatience/e04e63af07cf003d858089aa3e55a0c4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script will parse a json response from randomuser.me and convert it to the xml | |
# format required to import into Salesforce Demandware | |
# | |
# It fetches 500 random users from Canada. Just modify the URL parameters to modify these conditions. | |
import json | |
import os, sys | |
import codecs | |
from datetime import datetime | |
from xml.etree.ElementTree import ElementTree, Element, SubElement, Comment, tostring | |
from xml.etree import ElementTree | |
from xml.dom import minidom | |
import urllib.request, json | |
import random | |
import string | |
UTF8Writer = codecs.getwriter('utf8') | |
def randomPassword(stringLength=12): | |
letters = string.ascii_lowercase | |
return ''.join(random.choice(letters) for i in range(stringLength)) | |
def prettify(elem): | |
rough_string = ElementTree.tostring(elem, 'utf-8') | |
reparsed = minidom.parseString(rough_string) | |
return reparsed.toprettyxml(indent=" ") | |
with urllib.request.urlopen("https://randomuser.me/api/?format=json&nat=ca&exc=cell,registered&noinfo&results=500") as url: | |
data = json.loads(url.read().decode()) | |
results = data['results'] | |
currentId = 1 | |
customersRoot = Element('customers', {'xmlns':'http://www.demandware.com/xml/impex/customer/2006-10-31'}) | |
for customer in results: | |
customerElement = SubElement(customersRoot, 'customer', {'customer-no':str(currentId)}) | |
credentialElement = SubElement(customerElement, 'credentials') | |
loginElement = SubElement(credentialElement, 'login') | |
loginElement.text = customer['email'] | |
passwordElement = SubElement(credentialElement, 'password', {'encrypted': 'false'}) | |
passwordElement.text = randomPassword() | |
loginEnabledElement = SubElement(credentialElement, 'enabled-flag') | |
loginEnabledElement.text = 'true' | |
pwQuestionElement = SubElement(credentialElement, 'password-question') | |
pwwAnswerElement = SubElement(credentialElement, 'password-answer') | |
profileElement = SubElement(customerElement, 'profile') | |
firstNameElement = SubElement(profileElement, 'first-name') | |
firstNameElement.text = customer['name']['first'] | |
lastNameElement = SubElement(profileElement, 'last-name') | |
lastNameElement.text = customer['name']['last'] | |
emailElement = SubElement(profileElement, 'email') | |
emailElement.text = customer['email'] | |
phoneElement = SubElement(profileElement, 'phone-home') | |
phoneElement.text = customer['phone'] | |
genderElement = SubElement(profileElement, 'gender') | |
if customer['gender'] == 'male': | |
genderElement.text = '1' | |
else: | |
genderElement.text = '2' | |
# | |
# Uncomment these fields if you want the customers to have a profile photo. | |
# This will require you to add a custom field to your SFCC Profile object. | |
# | |
# customAttributesElement = SubElement(profileElement, 'custom-attributes') | |
# photoElement = SubElement(customAttributesElement, 'custom-attribute', {'attribute-id':'photo'}) | |
# photoElement.text = customer['picture']['large'] | |
addressesElement = SubElement(customerElement, 'addresses') | |
addressElement = SubElement(addressesElement, 'address', {'address-id':'home', 'preferred':'true'}) | |
firstNameAddressElement = SubElement(addressElement, 'first-name') | |
firstNameAddressElement.text = customer['name']['first'] | |
lastNameAddressElement = SubElement(addressElement, 'last-name') | |
lastNameAddressElement.text = customer['name']['last'] | |
address1Element = SubElement(addressElement, 'address1') | |
address1Element.text = str(customer['location']['street']['number']) + ' ' + customer['location']['street']['name'] | |
cityElement = SubElement(addressElement, 'city') | |
cityElement.text = customer['location']['city'] | |
postalElement = SubElement(addressElement, 'postal-code') | |
postalElement.text = customer['location']['postcode'] | |
stateElement = SubElement(addressElement, 'state-code') | |
stateElement.text = customer['location']['state'] | |
countryElement = SubElement(addressElement, 'country-code') | |
if customer['location']['country'] == 'United States': | |
countryElement.text = 'US' | |
else: | |
countryElement.text = 'CA' | |
currentId += 1 | |
f = open("customers-import-{}.xml".format(datetime.now()), mode="w", encoding="utf-8") | |
f.write(prettify(customersRoot)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shell version