Logging within scripts

Create an asset

def execute() {  
 
  def assetService = flexSdkClient.assetService;
  
  NewAssetPlaceholder asset = NewAssetPlaceholder.builder()
        .type('media-asset')
        .name('demo asset 1')
        .build();
  
  assetService.createAsset(asset);
  
}

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;
  
  NewAssetPlaceholder assetPh = NewAssetPlaceholder.builder()
        .type('media-asset')
        .name('demo asset 1')
        .variant("v1")
        .build();
  
  def asset = assetService.createAsset(assetPh);
  
  def metadata = assetService.getAssetMetadata(asset.getId());
  
  def field = metadata.getField('string-field')
  field.setValue('Example String')
  assetService.setAssetMetadata(asset.id, metadata)  
  
}

Setting workflow and job variables

def execute() {
    
  def asset = context.asset
  // Workflow variables
  context.setStringVariable('assetTitle', asset.name)
  context.setDateVariable('lastModified', asset.lastModified)
  
  // Job variables
  context.setJobStringVariable('referenceName', asset.referenceName)
  context.setJobVariable('publishedDate', asset.publishedDate)


}

Find asset by metadata value

We assume that a metadata definition exists with ID 214, and that is has a searchable string field called actor-name.

def execute() {
    
  def assetService = flexSdkClient.assetService;

  String metadata = 'actor-name:Bernard Reeves'
  def query = new AssetApiQuery(
    metadataDefinitionId: 214,
    metadata: [metadata]
  )

  def results = assetService.getAssets(query)
  context.logInfo("${results.totalCount} results") 
}

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 = '**REDCATED**'
def BASE_URL = 'https://api.themoviedb.org/3'

GroovyScriptContext cxt;
FlexSdkClient sdkClient;

Object execute() {

    cxt = (GroovyScriptContext) context;
    sdkClient = (FlexSdkClient) flexSdkClient;

    Asset asset = cxt.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")
}

def get(String url) {
    def json = new URL(url).getText()
    return new groovy.json.JsonSlurper().parseText(json)
}

def setFilmMetadata(Asset asset, filmDetails) {

    AssetService assetService = sdkClient.getAssetService();
    Field metadata = assetService.getAssetMetadata(asset)

    Date releaseDate = new java.text.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
    TagService tagService = sdkClient.tagService
    TagCollection tagCollection = tagService.getTagCollection('genre')
    if (tagService.getTag(tagCollection, genre.name) == null) {
        tagService.addTag(tagCollection, genre.name)
    }

    metadata.getField('genre').setValue(genre.name)

    assetService.setAssetMetadata(asset, metadata)

}

Asset createFilmImagePlaceholder(Asset asset, String imagePath) {

    AssetService assetService = sdkClient.getAssetService();

    NewAssetPlaceholder imageAssetPlaceholder = new NewAssetPlaceholder(
            name: asset.name,
            type: 'image-asset',
            variant: 'poster',
            workspaceId: cxt.job.workspace.id,
            parentId: asset.getId()
    )
    Asset imageAsset = assetService.createAsset(imageAssetPlaceholder)

    Field 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. This manual transactional handling is covered in depth in Transactions within Scripting.