全部产品

媒资分类

本篇文档提供了Python SDK媒资分类模块相关功能的的API调用示例,包含创建分类、删除分类、修改分类、查寻分类及其子分类。

初始化客户端

使用前请先初始化客户端,请参见初始化

创建分类

调用AddCategory接口,完成创建分类功能。

接口参数和返回字段请参见AddCategory。调用示例如下:

from aliyunsdkvod.request.v20170321 import AddCategoryRequest
def add_category(clt, cateName, parentId=-1):
    request = AddCategoryRequest.AddCategoryRequest()
    request.set_CateName(cateName)
    request.set_ParentId(parentId)
    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    addRes = add_category(clt, 'Category Name')
    print(json.dumps(addRes, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

修改分类

调用UpdateCategory接口,完成修改分类功能。

接口参数和返回字段请参见UpdateCategory。调用示例如下:

from aliyunsdkvod.request.v20170321 import UpdateCategoryRequest
def update_category(clt, cateId, cateName):
    request = UpdateCategoryRequest.UpdateCategoryRequest()
    request.set_CateId(cateId)
    request.set_CateName(cateName)
    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    updateRes = update_category(clt, '<cateId>', 'New Category Name')
    print(json.dumps(updateRes, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

删除分类

调用DeleteCategory接口,完成删除分类功能。

接口参数和返回字段请参见DeleteCategory。调用示例如下:

from aliyunsdkvod.request.v20170321 import UpdateCategoryRequest
# 删除视频分类,同时会删除其下级分类(包括二级分类和三级分类),请慎重操作
def delete_category(clt, cateId):
    request = DeleteCategoryRequest.DeleteCategoryRequest()
    request.set_CateId(cateId)
    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    delRes = delete_category(clt, '<cateId>')
    print(json.dumps(delRes, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

查询分类及其子分类

调用GetCategories接口,完成查询分类及其子分类功能。

接口参数和返回字段请参见GetCategories。调用示例如下:

from aliyunsdkvod.request.v20170321 import GetCategoriesRequest
def get_categories(clt, cateId=-1, pageNo=1, pageSize=10):
    request = GetCategoriesRequest.GetCategoriesRequest()
    request.set_CateId(cateId)
    request.set_PageNo(pageNo)
    request.set_PageSize(pageSize)
    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    getRes = get_categories(clt, '<cateId>')
    print(json.dumps(getRes, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())