Datasette 插件,支持根据 SQL 查询结果生成 Atom feed。
在 Datasette 所在的相同环境中安装此插件,以启用 .atom
输出扩展名。
$ pip install datasette-atom
要创建 Atom feed,您需要定义一个自定义 SQL 查询,该查询返回一组必需的列
atom_id
- 每行的唯一 ID。本文提供了关于如何创建这些 ID 的建议。atom_title
- 该行的标题。atom_updated
- 表示条目最后一次重大修改的 RFC 3339 时间戳。这通常可以是创建该行的时间。
以下列是可选的
atom_content
- 应在 feed 中显示的内容。这将作为普通字符串处理,因此任何嵌入的 HTML 标签在显示时将被转义。atom_content_html
- 应在 feed 中显示的内容。这将作为 HTML 字符串处理,并使用 Bleach 进行清理,以确保在作为
Atom 元素的一部分返回之前,其中不包含任何恶意代码。如果同时提供了这两列,则将使用此列代替atom_content
。atom_link
- 应用作 feed 条目指向的链接的 URL。atom_author_name
- 条目的作者姓名。如果您提供此列,您还可以提供atom_author_uri
和atom_author_email
,其中包含该作者的 URL 和电子邮件地址。
返回这些列的查询可以通过添加 .atom
扩展名作为 Atom feed 返回。
这里是一个示例 SQL 查询,它为 www.niche-museums.com 上的新条目生成 Atom feed
' || description || '
' as atom_content_html from museums order by created desc limit 15">select
'tag:niche-museums.com,' || substr(created, 0, 11) || ':' || id as atom_id,
name as atom_title,
created as atom_updated,
'https://www.niche-museums.com/browse/museums/' || id as atom_link,
coalesce(
'
' || photo_url || '?w=800&h=400&fit=crop&auto=compress">',
''
) || '' || description || '
' as atom_content_html
from
museums
order by
created desc
limit
15
您可以通过在此处粘贴此查询进行尝试 - 然后单击 .atom
链接将其视为 Atom feed。
Datasette 的预设查询机制是一种配置 feed 的有用方法。如果预设查询定义中包含 title
,则该值将用作 Atom feed 的标题。
这是一个使用 metadata.yaml
文件定义的示例
' || description || '
' as atom_content_html from museums order by created desc limit 15">databases:
browse:
queries:
feed:
title: Niche Museums
sql: |-
select
'tag:niche-museums.com,' || substr(created, 0, 11) || ':' || id as atom_id,
name as atom_title,
created as atom_updated,
'https://www.niche-museums.com/browse/museums/' || id as atom_link,
coalesce(
'
',
''
) || '' || description || '
' as atom_content_html
from
museums
order by
created desc
limit
15
Bleach 用于 atom_content_html
列的 HTML 允许列表可以在 datasette_atom/init.py 文件底部的 clean(html)
函数中找到。
您可以完全禁用 Bleach 用于通过预设查询生成的 Atom feed。仅当您确定该值中不会包含用户提供的 HTML 时,才应执行此操作。
以下是如何在 metadata.json
中进行设置
{
"plugins": {
"datasette-atom": {
"allow_unsafe_html_in_canned_queries": true
}
}
}
将此设置为 true
将禁用跨所有数据库的所有预设查询的 Bleach 过滤。
您可以仅针对特定预设查询列表禁用 Bleach 过滤,如下所示
{
"plugins": {
"datasette-atom": {
"allow_unsafe_html_in_canned_queries": {
"museums": ["latest", "moderation"]
}
}
}
}
这将仅禁用 museums.db
数据库中名为 latest
和 moderation
的预设查询的 Bleach 过滤。