Snippets

Lucas Correia API Example - Download Note components

Created by Lucas Correia
# Get URL where component can be downloaded
server_location = session.get('Location', ftrack_api.symbol.SERVER_LOCATION_ID)
note_component = session.query('NoteComponent').first()
component = note_component['component']
url = server_location.get_url(note_component['component'])

# Or, download the file using disk accessor.
def download_component(target_directory, component):
    '''Download component to *target_directory*. 

    Note: will overwrite data if a file with the same name already exists within the directory.
    '''
    import functools
    disk_accessor = ftrack_api.accessor.disk.DiskAccessor(target_directory)
    target_file_name = u'{}{}'.format(component['name'], component['file_type'])
    server_location = session.get(
        'Location', ftrack_api.symbol.SERVER_LOCATION_ID
    )
    source_data = server_location.accessor.open(
        server_location.get_resource_identifier(component), 'rb'
    )
    target_data = disk_accessor.open(target_file_name, 'wb')
    chunked_read = functools.partial(
        source_data.read, ftrack_api.symbol.CHUNK_SIZE
    )
    for chunk in iter(chunked_read, ''):
        target_data.write(chunk)
    target_data.close()
    source_data.close()


# Change to destination folder
target_directory = '/path/to/backup/directory'

# Query 10 first note components
note_components = session.query(
    'select component.name, component.file_type from NoteComponent limit 10'
)
for note_component in note_components:
    component = note_component['component']
    download_component(target_directory, component)

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.