Skip to content

Instantly share code, notes, and snippets.

@zoharbabin
Last active February 2, 2022 15:36
Show Gist options
  • Save zoharbabin/24566209259b984f2957 to your computer and use it in GitHub Desktop.
Save zoharbabin/24566209259b984f2957 to your computer and use it in GitHub Desktop.
Kaltura API Python Script for adding tags to all entries in the account
# This script loops through all media entries in a given Kaltura account
# and sets tags to all entries (it also checks if a list of tags is not already set on the entry before adding)
# this script requires the Kaltura Python 2.7 client library: http://www.kaltura.com/api_v3/testme/client-libs.php
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from KalturaClient import *
from KalturaClient.Base import *
from KalturaClient.Plugins.Core import *
# set the tags (comma seperated string) and list of tags to check (if exist on the entry, will not add the tagsToAdd)
tagsToAdd = "captionasr"
tagsToCheck = [tagsToAdd,'processing','caption complete']
# Kaltura Account credentials (make sure to replace partner_id and admin_secret)
# from: http://kmc.kaltura.com/index.php/kmc/kmc4#account|integration
PARTNER_ID = 000000
ADMIN_SECRET = "your-Kaltura-API-Admin-Secret"
SERVICE_URL = "http://www.kaltura.com"
USER_NAME = "taggerUser"
PAGE_SIZE = 100
METADATA_PROFILE_COUNT = 1
# client setup
class KalturaLogger(IKalturaLogger):
def log(self, msg):
pass
def GetConfig():
config = KalturaConfiguration(PARTNER_ID)
config.serviceUrl = SERVICE_URL
config.setLogger(KalturaLogger())
return config
# create session
client = KalturaClient(GetConfig())
ks = client.generateSession(ADMIN_SECRET, USER_NAME, KalturaSessionType.ADMIN, PARTNER_ID, 86400, "")
client.setKs(ks)
# initialize state
lastUpdatedAt = None
lastEntryIds = []
# template entry to update with:
templateMediaEntry = KalturaMediaEntry()
while True:
# build the filter
filter = KalturaMediaEntryFilter(
orderBy=KalturaMediaEntryOrderBy.UPDATED_AT_DESC)
if lastUpdatedAt != None:
filter.setUpdatedAtLessThanOrEqual(lastUpdatedAt)
if lastEntryIds != []:
filter.setIdNotIn(','.join(lastEntryIds))
# get the entries
results = client.media.list(filter, KalturaFilterPager(pageSize=PAGE_SIZE))
print 'got %d entries' % len(results.objects)
# update state (prepare all updates in one multirequest batch)
client.startMultiRequest()
for curEntry in results.objects:
if max(map(lambda x: curEntry.tags.find(x), tagsToCheck)) < 0:
templateMediaEntry.tags = curEntry.tags + ',' + tagsToAdd
updatedEntry = client.media.update(curEntry.id, templateMediaEntry)
print 'updated entry: %s with tags: %s' % (curEntry.id, templateMediaEntry.tags)
if lastUpdatedAt != curEntry.getUpdatedAt():
lastEntryIds = [] # clear the last ids, the entries will not be returned anyway due to the updatedAt<= condition
lastEntryIds.append(curEntry.id)
lastUpdatedAt = curEntry.getUpdatedAt()
# execute the update multirequest batch
updatedEntries = client.doMultiRequest()
if len(results.objects) < PAGE_SIZE:
break
@kenpeter
Copy link

I think it will get hit the 10k marks by using pagination. You cannot go beyond by using pager, right?

@lsloan
Copy link

lsloan commented Apr 22, 2020

So, the way to get the next page of results is to build a list of object IDs that were already seen, then do the same search with those IDs excluded?

Doesn't Kaltura offer something like a results.getNextPage() method‽

If not, wouldn't it be better to just update the pageIndex property of an instance of KalturaFilterPager to get the next page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment