datasette-ics 作者 simonw

星标

Screenshot of simonw/datasette-ics

README 源代码

datasette-ics

PyPI Changelog Tests License

一个 Datasette 插件,支持使用 SQL 查询结果生成 iCalendar .ics 文件

安装

将此插件安装到与 Datasette 相同的环境中,以启用 .ics 输出扩展。

$ pip install datasette-ics

用法

要创建 iCalendar 文件,您需要定义一个自定义 SQL 查询,该查询返回一组必需的列

  • event_name - 事件的简称
  • event_dtstart - 事件开始时间

以下列是可选的

  • event_dtend - 事件结束时间
  • event_duration - 事件持续时间(取代 dtend 使用)
  • event_description - 事件的详细描述
  • event_uid - 此事件的全局唯一标识符
  • event_tzid - 事件的时区,例如 America/Chicago

返回这些列的查询可以通过添加 .ics 扩展名来作为 ics feed 返回。

演示

这个 SQL 查询计算了加利福尼亚州半月湾(Half Moon Bay)Pillar Point 每天的最低潮汐。

由于此查询返回 event_nameevent_dtstartevent_tzid 列,因此它可以生成 此 ICS feed。如果您在 Apple 日历等日历应用程序中订阅此 feed,您将看到类似这样的结果:

Apple Calendar showing low tides at Pillar Point during a week

使用预设查询

Datasette 的预设查询机制可用于配置日历。如果预设查询定义包含 title 字段,该字段将用作日历的标题。

这是一个示例,使用 metadata.yaml 文件定义:

databases:
  mydatabase:
    queries:
      calendar:
        title: My Calendar
        sql: |-
          select
            title as event_name,
            start as event_dtstart,
            description as event_description
          from
            events
          order by
            start
          limit
            100

这将生成一个位于 http://localhost:8001/mydatabase/calendar.ics 的日历 feed。