Skip to content

Instantly share code, notes, and snippets.

@zoharbabin
Created October 22, 2023 07:23
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/c60cf930eaf20d2098cf9495eed73873 to your computer and use it in GitHub Desktop.
Save zoharbabin/c60cf930eaf20d2098cf9495eed73873 to your computer and use it in GitHub Desktop.
Create Custom Metadata Instance in Kaltura
from KalturaClient import KalturaClient
from KalturaClient.Base import KalturaConfiguration
from KalturaClient.Plugins.Metadata import KalturaMetadataObjectType, KalturaMetadata
from KalturaClient.Plugins.Core import KalturaMediaEntry, KalturaMediaType
import xml.etree.ElementTree as ET
# Initialize Kaltura configuration and client
config = KalturaConfiguration()
config.serviceUrl = "https://cdnapi-ev.kaltura.com/"
client = KalturaClient(config)
# Set ks (Kaltura session)
ks = "FILL VALID KALTURA SESSION HERE"
client.setKs(ks)
metadataProfileId = 000000 # the custom metadata profile id to add the metadata to (from https://kmc.kaltura.com/index.php/kmcng/settings/metadata)
uploaderId = "YOUR_UPLOADER_ID" # Replace with your uploader ID
def create_media_entry():
media_entry = KalturaMediaEntry()
media_entry.name = "Test Media Entry"
media_entry.mediaType = KalturaMediaType.VIDEO
# Create the media entry
created_entry = client.media.add(media_entry)
return created_entry
def generate_xml_data(metadata):
root = ET.Element("metadata")
# Add each metadata field
ET.SubElement(root, "Email").text = "NA"
ET.SubElement(root, "Phone").text = metadata['phone']
ET.SubElement(root, "Format").text = metadata['format']
for cat in metadata['categories']:
ET.SubElement(root, "Categories").text = cat
ET.SubElement(root, "CaptureLocation").text = metadata['location']
ET.SubElement(root, "Longitude").text = metadata['longitude']
ET.SubElement(root, "Latitude").text = metadata['latitude']
ET.SubElement(root, "UploaderId").text = uploaderId
ET.SubElement(root, "UploaderFullName").text = metadata['name']
return ET.tostring(root).decode()
def add_custom_metadata(metadata, entry_id):
xml_data = generate_xml_data(metadata)
# Add custom metadata
added_metadata = client.metadata.metadata.add(metadataProfileId, KalturaMetadataObjectType.ENTRY, entry_id, xml_data)
return added_metadata
# Create a new media entry
new_entry = create_media_entry()
print("New Media Entry ID:", new_entry.id)
# Example metadata
example_metadata = {
'phone': '123-456-7890',
'format': 'Dashcam', # Choose an allowed value
'categories': ['Testimonials', 'Eye-witness'], # Choose allowed values
'location': 'some_location',
'longitude': '12.34',
'latitude': '56.78',
'name': 'John Doe'
}
# Add custom metadata to the new media entry
added_metadata = add_custom_metadata(example_metadata, new_entry.id)
print("Added Metadata: ", vars(added_metadata))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment