Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import xml.etree.ElementTree as ET
import json
import zlib
import base64
import urllib.parse
import os
import uuid
for icon_xml_file in os.listdir(".\libs\\integration"):
if icon_xml_file.endswith(".xml"):
print(f'Processing Icon XML File Called: {icon_xml_file}')
raw_drawio_data = ET.parse(f'.\libs\\integration\\{icon_xml_file}')
raw_drawio_data_root = raw_drawio_data.getroot()
drawio_json_representation = json.loads(raw_drawio_data_root.text)
for icon in drawio_json_representation:
if("xml" in icon.keys()):
# parsing Draw.io Files https://softwarerecs.stackexchange.com/questions/42394/is-there-an-xml-renderer-for-draw-io-graphs
# #test recipe https://gchq.github.io/CyberChef/#recipe=From_Base64('A-Za-z0-9%2B/%3D',true)Raw_Inflate(0,0,'',false,true)URL_Decode()XML_Beautify('%5C%5Ct')Syntax_highlighter('xml')
# refer to https://stackoverflow.com/questions/1089662/python-inflate-and-deflate-implementations
base64_decoded_data = base64.b64decode(icon["xml"])
deflated_data = zlib.decompress( base64_decoded_data , -15)
url_decoded_data = urllib.parse.unquote(deflated_data.decode())
mxgraph_model_parsed_xml = ET.fromstring(url_decoded_data)
mxcell3_value_as_string = mxgraph_model_parsed_xml[0][2].attrib["style"]
mxcell3_value_as_list = mxcell3_value_as_string.split(";")
for mxcell3_invidiual_item in mxcell3_value_as_list:
if(mxcell3_invidiual_item.startswith("image=data:image/svg+xml,")):
svg_base64_encoded = mxcell3_invidiual_item.replace("image=data:image/svg+xml,","")
svg_as_xml_string = base64.b64decode(svg_base64_encoded).decode(encoding="UTF-8")
if("title" in icon.keys()):
icon["title"] = icon["title"].replace("/","")
print(f'\t Processing Icon Called: {icon["title"]} \t Create SVG File Called {icon["title"]}.svg')
else:
icon["title"] = uuid.uuid4()
print(f'\t Icon Does Not Have a Name! Creating Random Icon Name : {icon["title"]} \t Create SVG File Called {icon["title"]}.svg')
try:
with open(f'{icon["title"]}-{uuid.uuid4()}.svg',"w") as svg_file_to_write:
svg_file_to_write.write(svg_as_xml_string)
except:
print(f'\t\t Unable to Commit File. Funny Characters in XML. Raw SVG XML is: {svg_as_xml_string}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment