Skip to content

Instantly share code, notes, and snippets.

@zoharbabin
Created November 7, 2023 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoharbabin/9792508b2bfcf0c2628415cd42c239e4 to your computer and use it in GitHub Desktop.
Save zoharbabin/9792508b2bfcf0c2628415cd42c239e4 to your computer and use it in GitHub Desktop.
Generate Custom Metadata XML Template from the Kaltura stored XSD to always get an up to date XML template
"""
This script is designed to work with the Kaltura API to fetch the XSD (XML Schema Definition) of a custom metadata profile and generate a formatted XML template that can be used to add or update metadata items on entries within the Kaltura platform.
How it works:
1. The script starts by establishing a Kaltura session using provided user credentials and partner ID.
2. It accepts a metadata profile ID as a command-line argument.
3. The script then fetches the XSD of the specified metadata profile by calling Kaltura's metadata profile service.
4. Using the retrieved XSD, it generates an XML template. Each 'element' found within the XSD is transformed into the corresponding XML tags in the template.
5. The generated XML template is output in a 'pretty-printed' format with proper indentation for readability.
Usage:
Execute the script from the command line, passing the metadata profile ID as the argument:
$ python metadata_template_gen.py <metadata_profile_id>
Example:
$ python metadata_template_gen.py 21828632
Replace 'metadata_template_gen.py' with the actual name of your script and '<metadata_profile_id>' with the numeric ID of your metadata profile.
Requirements:
- A Kaltura account with an associated partner ID.
- Your Kaltura admin secret key for authentication.
- The KalturaClient Python library and its dependencies installed in your Python environment.
Make sure to replace the empty string in the 'client.session.start' method with your actual admin secret before running the script.
Notes:
- The script will terminate with a usage message if no command-line argument is given.
- Only simple XML structures are supported for pretty-printing. Complex XML structures may require a more advanced XML processing library.
"""
from KalturaClient import *
from KalturaClient.Plugins.Core import *
from KalturaClient.Plugins.Metadata import *
from KalturaClient.exceptions import *
import xml.etree.ElementTree as ET
from xml.dom import minidom
import sys
# Check for command line argument
if len(sys.argv) < 2:
print("Usage: python script.py <metadata_profile_id>")
sys.exit(1)
metadata_profile_id = int(sys.argv[1]) # Convert command line argument to integer
# Configuration and authentication
partner_id = 5580612
config = KalturaConfiguration(partner_id)
config.serviceUrl = "https://cdnapi-ev.kaltura.com/"
client = KalturaClient(config)
ks = client.session.start(
"",
"metadata-tester",
KalturaSessionType.ADMIN,
partner_id
)
client.setKs(ks)
def get_metadata_xsd(metadata_profile_id):
try:
# Fetch the Metadata Profile XSD
metadata_profile = client.metadata.metadataProfile.get(metadata_profile_id)
xsd = metadata_profile.xsd
return xsd
except KalturaException as e:
print("Failed to fetch metadata XSD: %s" % e.message)
return None
def generate_metadata_template(xsd):
# Parse the XSD and create an XML template
root = ET.Element("metadata")
xsd_tree = ET.fromstring(xsd)
for element in xsd_tree.findall(".//{http://www.w3.org/2001/XMLSchema}element"):
name = element.get('name')
ET.SubElement(root, name)
# Convert to string
xmlstr = ET.tostring(root, encoding='utf-8', method='xml')
# Pretty print
parsed = minidom.parseString(xmlstr)
return parsed.toprettyxml(indent=" ")
def main():
xsd = get_metadata_xsd(metadata_profile_id)
if xsd:
template = generate_metadata_template(xsd)
print(template)
else:
print("Could not generate XML template.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment