Updating Datasets using the Xibo API


#1

Hello, I would like to use a script or some software to automatically update info that is shown on my displays. The source of this information could be some text or CSV files, and I would like to update this information into datasets, which would be shown in a region using a dataset ticker.

I know there is a Xibo API and have some experience coding in Python, but I could not find many examples besides the one used to replace a video on the library.

Are there any API documentation or examples available elsewhere?

Thanks is advance


#2

As far as I am aware, there is no support yet in the API for the updating of datasets.

I did start working on a script to write the data directly to the database but stopped work on it due to it being a bad idea to skip the CMS.


#3

I thought there was at least support for uploading CSV data in to a Dataset via the API but I could be wrong


#4

Yep - Dataset methods all appear to be implemented in the current git tree:

https://github.com/xibosignage/xibo-cms/blob/develop/lib/service/rest.class.php#L1225

You’d call the API in the same way as you would in the worked example on the Xibo website. The parameters to use are documented in the source code at the link above.


#5

Hello,

did you find a solution yet? i dont have any skills in phyton but i search also an way to update (csv) an existing dataset to create a room planning system on the xibo windows client.

Greetings from Germany :slight_smile:


#6

I’ve written some test python code below to use the API to upload .csv file to a test dataset and it works for me. I hope the code can help people who is new to this API.

For more information, I suggested to have a look at the documentation.

Tour of the API

Xibo API
http://xibo.org.uk/manual/en/api.html

P.S. I am very new to Python as well.


#7
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Script to upload a csv file to the Xibo dataset and replace


# Imports
import os
from xml.dom import minidom
import XiboAPI

api = XiboAPI.XiboAPI()


#################################
# START LIBRARY FUNCTIONS
#################################

def uploadFile(api,path,chunksize=256000,fileid='0',offset=0,size=0):
    # Uploads file at path (full path)
    # Returns a file upload ID which can be used to add the media
    # to the library
    #
    # eg uploadFile(api,'/home/user/1.jpg')
    #
    # Optional Parameters:
    #  chunksize - Int: How many bytes to upload at each pass
    #  fileid - String: The fileUploadID to use. Useful if you're resuming an interupted upload.
    #  offset - Long: How many bytes to skip over before beginning upload. Useful for resuming an interupted upload.
    #  size - Long: How many bytes to upload. 0 = the whole file. Useful to break an upload part way through for testing.
    #              Must be smaller than the size of the file in bytes.

    # First check the test file exists
    if not os.path.isfile(path):
        print "Error: File does not exist %s " % path
        exit(1)

    checksum = ''
    data = ''
    if size == 0:
        size = os.path.getsize(path)
    chunkOK = False

    # If the file is smaller than the chunksize, send one chunk
    # of the correct size.
    if chunksize > size:
        chunksize = size
 
    # Open the file for binary read
    fh = open(path,'rb')

    while offset < size:
        attempts = 0
        chunkOK = False
        while attempts < 3 and not chunkOK:
            attempts += 1

            # Read chunksize bytes of the file
            fh.seek(offset)
            data = fh.read(chunksize)

            # Data needs to be base64 encoded to be sent
            data, checksum = api.b64encode(data)

            params = [('fileId',fileid),
                      ('offset',offset),
                      ('checksum',checksum),
                      ('payload',data)
                     ]

            response, status, code, message, content = api.callMethod('LibraryMediaFileUpload', params)

            # If the chunk went up OK then move to the next chunk
            if status == 'OK':
                chunkOK = True
            else:
                print 'Uploading chunk failed. Error %s: %s' % (code,message)

        if not chunkOK:
            # We did three tries and the chunk still failed.
            print 'Uploading chunk failed after three attempts. File: %s Id: %s Offset: %s Attempt: %s' % (path,fileid,offset,attempts)
            exit(1)

        # Store the fileID so we can reuse it
        fileid = api.parseID(content,'file','id')

        # Get the offset the server has already (to support resuming uploads)
        offset = api.parseID(content,'file','offset')

        # Make sure we don't upload past the end of the file!
        if offset + chunksize > size:
            chunksize = size - offset

    # All chunks uploaded
    # Close the file handle
    fh.close()

    return fileid
def layoutRegionMediaDelete(api,layoutid,regionid,mediaid,lkid):
    params = [('mediaId',mediaid),
              ('regionId',regionid),
              ('layoutId',int(layoutid)),
              ('lkId',int(lkid))
             ]
    
    response, status, code, message, content = api.callMethod('LayoutRegionMediaDelete', params)

def libraryMediaDelete(api,mediaid):
    params = [('mediaId',mediaid)]

    response, status, code, message, content = api.callMethod('LibraryMediaDelete', params)
#################################
# END LIBRARY FUNCTIONS
#################################	
	
fileToUpload = 't.csv'
mediaName = 'Test CSV'
dataSetId = 10
uploadId = uploadFile(api,fileToUpload)
spreadSheetMapping = '{"0":"34","1":"35"}'
print "upload ID %d" % uploadId

params = [('dataSetId',dataSetId),('fileId',uploadId),('spreadSheetMapping',spreadSheetMapping),('overwrite',1),('ignoreFirstRow',0)]

response, status, code, message, content = api.callMethod('DataSetImportCsv', params)
print message
print content

#8

Can this be accomplished using .NET, instead of python? Thank you.


#9

I break down a solution to this here: