数据类型
每个客户端方法返回不同的数据类型。
ContentSet
contentset_list方法返回ContentSet对象的列表。
ContentSet对象有以下属性
contentset_id
name
description
is_default
请参考API数据类型文档以获取有关这些字段的更多信息。
ArticleNode
contentset(contentset_id)方法返回一个单个ArticleNode。此节点表示一个Articles树根。
ArticleNode具有以下属性
- article
与该节点关联的Article实例
- children
一个列表,包含作为当前节点父节点的ArticleNode实例。
可以遍历文章节点。这将按深度优先顺序遍历整个树。例如,要获取树中所有文章的扁平列表,可以这样做
tree = client.contentset(1)
articles = [node.article for node in tree if node.article]
要递归遍历树,请使用children属性
def visit(node, parent=None):
"""
Visit each node in the tree.
"""
# do something with node, then recurse
for child in node.children:
visit(child, node)
tree = client.contentset(1)
visit(tree)
Article
article(external_reference)方法返回一个单个Article实例。获取Article对象的另一种方法是访问ArticleNode的article属性。
Article具有以下属性
external_reference
short_title
title
article_text
parent_reference
position
last_change_date
canonicaltag
tags
请参考API数据类型文档以获取有关这些字段的更多信息。
除了这些字段之外,Article对象还提供以下属性
- path
文章的URL。这与canonicaltag相同,但去除了schema://domain前缀。
- slug
路径的最后一个元素。即如果path是'/foo/bar/',则slug将是'bar'。
Image
image(image_id)方法返回一个单个Image实例。
image_id
data
content_type
name
creation_date
请参考API数据类型文档以获取有关这些字段的更多信息。
将图像数据转换为二进制
Image对象还提供一个as_binary方法。
此方法将data属性的基础64编码值转换为二进制。此方法返回的值可以用于在文件系统中存储图像。
文章实用工具
contentset方法返回的Articles的article_text可能包含多个特殊占位符字符串。
opvoeden_api.article_utils提供函数来处理这些占位符。
替换JGZ占位符
要使用replace_jgz将JGZ占位符替换为适当的字符串,请使用replace_jgz。
默认情况下,这些是替换
占位符 |
替换 |
jgz |
centrum voor Jeugd en Gezin (CJG) |
Jgz |
Centrum voor Jeugd en Gezin (CJG) |
jgzs |
CJG’s |
Jgzs |
CJG’s |
de jgzs |
de CJG’s |
De jgzs |
De CJG’s |
het jgz |
het Centrum voor Jeugd en Gezin (CJG) |
Het jgz |
Het Centrum voor Jeugd en Gezin (CJG) |
要覆盖任何替换,请使用replace_jgz的substitutions参数,例如
replace_jgz(article_text, substitutions={
'jgz': 'centrum voor Jeugd en Gezin'
})
替换内部链接占位符
要使用替换回调使用replace_links替换内部链接占位符,请使用replace_links。
替换回调函数会使用文章文本中的每个占位符的 external_id 和 link_text 调用。
如果替换回调函数返回的不是 None,则链接会被替换为其返回值。
例如
external_id_to_href = {
'1': '/example/',
'2': '/example/more/'
}
def get_link(external_id, link_text):
"""
Get the url for an article and return an HTML snippet
that links to this url with the given text.
"""
href = external_id_to_href.get(external_id, None)
if href:
return '<a href="{}">{}</a>'.format(href, link_text)
replace_links(article_text, get_link)
替换图片占位符
要替换 图片占位符,请使用 replace_images 与替换回调函数。
替换回调函数会使用文章文本中每个占位符的 image_id 调用。
如果替换回调函数返回的不是 None,则占位符会被替换为其返回值。
例如
image_id_to_src = {
'1': '/media/1.gif',
'2': '/media/2.gif'
}
def get_image_tag(image_id):
src = image_id_to_src.get(image_id, None)
if src:
return '<img src="{}">'.format(src)
替换视频占位符
要替换 YouTube 视频占位符,请使用 replace_videos 与替换回调函数。
替换回调函数会使用文章文本中每个占位符的 video_id、embed_url 和 external_url 调用。
如果替换回调函数返回的不是 None,则占位符会被替换为其返回值。
一些示例
def get_video_embed(video_id, embed_url, external_url):
"""Create an iframe to embed the video"""
return '<iframe src="{}">'.format(embed_url)
def get_video_link(video_id, embed_url, external_url):
"""Create a link to the video player on opvoeden.nl"""
return '<a href="{}" target="_blank">Watch the video</a>'.format(
external_url)