rass_meta_create.py

#!/usr/bin/python
#
# Copyright (C) 2008 rambla.be
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##
#  This sample demonstrates how to set meta elements on meta resources. 

import time
import rawsc
import rawsc.service
from rawsc import rass
import rawsc.rass.service
from rawsc.rass import Tag, Meta
try:
  from xml.etree import cElementTree as ElementTree
except ImportError:
  try:
    import cElementTree as ElementTree
  except ImportError:
    from elementtree import ElementTree

# user credentials for authentication to RASS
USER = "XXX"
PWD = "XXX"
SERVER = "rass.cdn0XXX.rambla.be"

# path to a local file that may be uploaded
LOCAL_FILE = "/path/to/local/file.mp4"

# name to be given to the local file on the CDN
TGT_FILENAME = "white_noise.mp4"

try:
    # Create a Rass instance, passing it your login credentials and the base service uri
    client = rawsc.rass.service.RassService(username=USER, password=PWD, server=SERVER)
    
    # upload some files, so we can tag them
    item_entry = client.postItem(dirpath = "/tutorial10/", filename = TGT_FILENAME, local_path = LOCAL_FILE)
    print "\nCreated new item resource: %s" % item_entry.id.text
    
    # set meta elements on the meta entry
    meta_entry = client.createMeta(path = item_entry.path,  metas = [Meta(vocab = "dc", name = "title", text="Cloud watching"),Meta(vocab = "dc", name = "contributor", text="Rambla"),])
    print "\nSet meta element on the meta resource: %s" % meta_entry.id.text
    for t in meta_entry.content.params.meta:
        print "\nMeta element :"
        print "* vocab:" + t.vocab
        print "* name: " + t.name
        if t.text:
            print "* value: " + t.text
        for k,v in t.extension_attributes.items():
            print "* attrib: " + k + " = " + v
            
        # change title from 'Cloud watching' to 'Cloud Streaming'
        if t.vocab == "dc" and t.name == "title":
            t.text = "Cloud Streaming"
    # also append another meta element (<meta vocab='media' name='price' type='rent' price='19.99' currency='EUR' />)
    meta_entry.content.params.meta.append(Meta(vocab="media",name='price',extension_attributes={"type":"rent", "price":"19.99", "currency":"EUR",}))
    # update meta resource with local changes
    meta_entry = client.updateMeta(meta_entry)
    print "\nUpdated meta resource: %s" % meta_entry.id.text
    for t in meta_entry.content.params.meta:
        print "\nMeta element :"
        print "* vocab:" + t.vocab
        print "* name: " + t.name
        if t.text:
            print "* value: " + t.text
        for k,v in t.extension_attributes.items():
            print "* attrib: " + k + " = " + v
 
    # reset meta elements on the meta item -> existing elements will be removed
    meta_entry = client.createMeta(path = meta_entry.path, metas = [Meta(vocab = "myvocab", name = "animal", text = "lion",
                                                                    extension_attributes={"description":"sleeping at night",}),
                                                                    Meta(vocab = "myvocab", name = "like", 
                                                                    extension_attributes={"likes":"1520", "dislikes":"2",})
                                                                   ])
    print "\nReplacing meta elements for entry : %s" % meta_entry.id.text
    for t in meta_entry.content.params.meta:
        print "\nMeta element :"
        print "* vocab:" + t.vocab
        print "* name: " + t.name
        if t.text:
            print "* value: " + t.text
        for k,v in t.extension_attributes.items():
            print "* attrib: " + k + " = " + v
    
    # # delete all manual metadata (= tags and meta elements) - doesn't delete the file (= item resource)
    # client.delete(meta_entry.id.text)
    # 
    # # delete the uploaded file
    # client.delete(item_entry.id.text)

except rawsc.service.Error, e:
    print "Error Response from server, contents = %s.\n" % str(e)
    error_dict = e[0] # error dict inside the first tuple elem
    print "status code : " + str(error_dict["status"]) + "\n"
    if error_dict.has_key("body"):
        print "error body : " + error_dict["body"] + "\n"
except rawsc.RawscException, e:
    print "RawscException caught, reason = %s.\n" % str(e)
except StandardError, e:
    print "StandardError caught, reason = %s.\n" % str(e)
except:
    print "unhandled exception caught\n"


Generated on Mon Mar 28 15:03:38 2011 for rawsc by  doxygen 1.5.3