Create an asset
def execute() {
def assetPlaceholder = new NewAssetPlaceholder()
assetPlaceholder.setType('media-asset')
assetPlaceholder.setName('demo asset 1')
flexSdkClient.assetService.createAsset(assetPlaceholder)
}
Create an asset with metadata
We assume that the variant v1 exists and has a default metadata definition set for media assets.
def execute() {
def assetService = flexSdkClient.assetService
def assetPlaceholder = NewAssetPlaceholder.builder()
.type('media-asset')
.name('demo asset 1')
.variant('v1')
.build()
def asset = assetService.createAsset(assetPlaceholder)
def metadata = assetService.getAssetMetadata(asset.getId())
metadata.getField('asset-director').setValue('Christopher Nolan')
assetService.setAssetMetadata(asset.id, metadata)
}
Setting workflow and job variables
def execute() {
def asset = context.asset
// Workflow variables
context.setWorkflowStringVariable('assetTitle', asset.name)
context.setWorkflowDateVariable('lastModified', asset.lastModified)
// Job variables
context.setJobStringVariable('referenceName', asset.referenceName)
context.setJobDateVariable('publishedDate', asset.publishedDate)
}
Find asset by metadata value
We assume that a metadata definition exists with name 'Asset metadata', and that is has a searchable string field called actor-name.
def execute() {
def assetService = flexSdkClient.assetService
def query = new AssetApiQuery()
// avoid using metadata definition IDs in the code, as the IDs might not be the same between Dev / staging & and Production env.
query.setMetadataDefinitionId(getMetadataDefId('Asset metadata'))
query.setMetadata(['actor-name:Bernard Reeves'])
def results = assetService.getAssets(query)
context.logInfo(results.totalCount + ' results')
}
def getMetadataDefId(String name) {
def query = MetadataDefinitionApiQuery.builder()
.name(name)
.exactNameMatch(true)
.build()
return flexSdkClient.metadataDefinitionService.getMetadataDefinitions(query).metadataDefinitions.first().id
}
Lookup asset metadata from IMDB
In this more thorough example, this scripts looks up a film's metadata from IMDB. We are assuming:
- that this script is executing against a media asset (with a title such as "Toy Story")
- the asset has a metadata definition that has date release-date and tag genre fields
- there is variant called poster and its default metadata definition has string hostname and path fields
def API_KEY = '**REDACTED**'
def BASE_URL = 'https://api.themoviedb.org/3'
def execute() {
def asset = context.asset
def film = lookupFilmOnImdb(asset.name)
def filmDetails = lookupFilmDetailsOnImdb(film)
setFilmMetadata(asset, filmDetails)
createFilmImagePlaceholder(asset, filmDetails.poster_path)
}
def lookupFilmOnImdb(String query) {
def urlQuery = URLEncoder.encode(query, 'utf-8')
def data = get("$BASE_URL/search/movie?api_key=${API_KEY}&language=en-US&query=$urlQuery&page=1")
return data.get('results').get(0)
}
def lookupFilmDetailsOnImdb(movie) {
return get("$BASE_URL/movie/$movie.id?api_key=${API_KEY}&language=en-GB&append_to_response=release_dates")
}
static def get(String url) {
def json = new URL(url).getText()
return new JsonSlurper().parseText(json)
}
def setFilmMetadata(Asset asset, filmDetails) {
def assetService = flexSdkClient.assetService
def metadata = assetService.getAssetMetadata(asset)
def releaseDate = new SimpleDateFormat('yyyy-MM-dd').parse(filmDetails.release_date)
metadata.getField('release-date').setValue(releaseDate)
def genre = filmDetails.genres.get(0) // Create a tag for the genre if one doesn't exist
def tagService = flexSdkClient.tagService
def tagCollection = tagService.getTagCollection('genre')
if (tagService.getTag(tagCollection, genre.name.toString()) == null) {
tagService.addTag(tagCollection, genre.name)
}
metadata.getField('genre').setValue(genre.name)
assetService.setAssetMetadata(asset, metadata)
}
def createFilmImagePlaceholder(Asset asset, String imagePath) {
def assetService = flexSdkClient.assetService
def imageAssetPlaceholder = NewAssetPlaceholder.builder()
.name(asset.name)
.type('image-asset')
.variant('poster')
.workspaceId(context.job.workspace.id)
.parentId(asset.id)
.build()
def imageAsset = assetService.createAsset(imageAssetPlaceholder)
def metadata = assetService.getAssetMetadata(imageAsset)
metadata.getField('hostname').setValue('image.tmdb.org')
metadata.getField('path').setValue("/t/p/original${imagePath}" as String)
// If we fail to set the metadata, then we want to delete the image asset
try {
assetService.setAssetMetadata(imageAsset, metadata)
} catch (Throwable t) {
assetService.destroy(imageAsset.id)
throw t
}
return imageAsset
}
Note
Notice that we roll back the creation of the child image asset if setting the metadata fails. This means that we know we will not have an image asset on the system that is incomplete.
Manual transactional handling is covered in depth in Flex Scripting: Transactions within Scripting.
Manipulating collections
This script creates a new collection, set its sharing properties and then updates its metadata. We are assuming the variant c1 exists and has a metadata definition named md1 assigned to it.
def execute() {
def collectionService = flexSdkClient.collectionService
def variantId = getVariantId('c1')
def metadataDefinitionId = getMetadataDefinitionId('md1') // Create collection
def collectionRequest = CreateCollectionRequest.builder()
.name('my-collection')
.variantId(variantId)
.metadataDefinitionEntityId(metadataDefinitionId)
.build()
def collection = collectionService.createCollection(collectionRequest)
try {
// Set sharing permissions
def sharing = Sharing.builder()
.readAcl([2227L])
.build()
def updateCollection = UpdateCollection.builder()
.sharing(sharing)
.build()
collection = collectionService.updateCollection(collection, updateCollection) // Update metadata
def metadata = collectionService.getCollectionMetadata(collection)
metadata.getField('new-string-1').setValue('my value')
collectionService.setCollectionMetadata(collection, metadata)
context.logInfo('Created and updated collection {}', collection.getUuid())
} catch (Exception e) {
// Rollback in case post-creation updates fail
collectionService.deleteCollection(collection)
throw e
}
}
def getMetadataDefinitionId(String name) {
def query = MetadataDefinitionApiQuery.builder()
.name(name)
.exactNameMatch(true)
.build()
return flexSdkClient.metadataDefinitionService.getMetadataDefinitions(query).metadataDefinitions.first().id
}
def getVariantId(String name) {
def query = VariantApiQuery.builder()
.name(name)
.exactNameMatch(true)
.build()
return flexSdkClient.variantService.getVariants(query).variants.first().id
}
Comments
0 comments
Please sign in to leave a comment.