カレンダーのDavCalをコマンドラインに出力したい

何らかの日程を決めるときに未来の日程を見ながら決めたいことがあります.
Webベースでいいかと思ったのですが,特に低解像度だとタイトルの一部しか表示されずよくわかりません.コピーペーストしてEtherPadなどに貼るといいですが時間がかかるし面倒です.
DavCalなどの情報を範囲指定して表示出来ると便利そうだなと思いました.

はじめはコマンドを叩いて範囲指定して表示するようなものがないかと思ったのですが見つかりませんでした.少し面倒ですが, vdirsyncer でリモートのカレンダーをローカルにコピーして khal コマンドで表示するようにしてみました.

Vdirsyncer の導入と設定

Vdirsyncer はカレンダーとコンタクトをローカルと同期できるソフトウェアです.これでリモートのDavCal の情報をローカルに同期します.

$ sudo apt install vdirsyncer (1)
$ mkdir ~/.config/vdirsyncer
$ vi ~/.config/vdirsyncer/config (2)
$ cat ~/.config/vdirsyncer/config
[general]
status_path = "~/.vdirsyncer/status/"

[pair my_calendars]
a = "my_calendars_local"
b = "my_calendars_remote"
collections = null
metadata = ["color"]

[storage my_calendars_local]
type = "filesystem"
path = "~/.calendars/"
fileext = ".ics"

[storage my_calendars_remote]
type = "caldav"
url = "https://files.matoken.org/remote.php/dav/public-calendars/YW5gc6gMG8tC63AH?export"
  1. Vdirsyncer の導入
  2. 設定ファイル作成

これでとりあえず Vdirsyncer を導入して,リモートの CalDav 形式のカレンダーをローカルのストレージに同期できます.
設定ファイルは以下のページを参考にしました.相互同期はせず,リモートのデータをローカルに同期するだけの動作のつもりです.

$ vdirsyncer discover my_calendars (1)
Discovering collections for pair my_calendars
my_calendars_local:
my_calendars_remote:
  - "YW5gc6gMG8tC63AH" ("onlin_event (matoken)")
warning: No collection "YW5gc6gMG8tC63AH" found for storage my_calendars_local.
Should vdirsyncer attempt to create it? [y/N]: y
Saved for my_calendars: collections = ["YW5gc6gMG8tC63AH"]
$ vdirsyncer sync (2)
Syncing my_calendars/YW5gc6gMG8tC63AH
Copying (uploading) item 05f764a4-8c30-46ed-a0fe-62c67d3975a8 to my_calendars_local/YW5gc6gMG8tC63AH
Copying (uploading) item 1acc3691-a12b-4534-a4e2-6ee6213ecff1 to my_calendars_local/YW5gc6gMG8tC63AH
Copying (uploading) item 202e727b-0431-40b5-8008-9ada1269c7c8 to my_calendars_local/YW5gc6gMG8tC63AH
Copying (uploading) item 2312a403-b2e6-4716-9c6f-45aa94519654 to my_calendars_local/YW5gc6gMG8tC63AH
  :
$ ls ~/.calendars/YW5gc6gMG8tC63AH/ | head (3)
05f764a4-8c30-46ed-a0fe-62c67d3975a8.ics
1acc3691-a12b-4534-a4e2-6ee6213ecff1.ics
202e727b-0431-40b5-8008-9ada1269c7c8.ics
2312a403-b2e6-4716-9c6f-45aa94519654.ics
263c2df9-ad9b-40d5-a946-614af7bc10ae.ics
29cbecc8-0b88-41da-b7d0-0c71062eb6b4.ics
2c66d40f-f5bd-4a67-bfb9-9bbb0258a966.ics
2e81f5bd-1aad-435a-bfa4-4a3ebe75e489.ics
30d41952-4dc3-4f10-bb6d-3311593f1d25.ics
3335be8d-9dd6-4b5e-81e9-a292d56fa17e.ics
  1. 設定を探索して初期化.設定を作ったときに実行.
  2. カレンダーを同期.定期的に実行.
  3. 同期されたデータが .ics ファイルとして作られているのを確認.

カレンダーがローカルに同期されました :)
vdirsyncer sync は定期的に実行して同期するようにします.

$ crontab -l | grep 'vdirsyncer sync'
4 4 * * *       chronic vdirsyncer sync

crontab に設定して1日1回同期するようにしました.
chronic を挟んでいますが,これはmoreutils 🏛️ のコマンドの1つで,正常終了時にはメッセージを出力しないというものです.エラー時だけメールが飛ぶはず.

khalの導入と設定

カレンダーの同期は出来ましたが,icsファイルがあるだけなので閲覧が不便です.閲覧するために khal 🏛️ コマンドを利用します.

$ sudo sudo apt install khal
$ mkdir ~/.config/khal
$ vi ~/.config/khal/config
$ cat ~/.config/khal/config
[calendars]
  [[event]]

    path = ~/.calendars/YW5gc6gMG8tC63AH/
    readonly = True

[locale]
timeformat = %H:%M
dateformat = %Y-%m-%d
longdateformat = %Y-%m-%d
datetimeformat = %Y-%m-%d %H:%M
longdatetimeformat = %Y-%m-%d %H:%M

khalを導入して設定を書いてみました.pathは Vdirsyncer で同期された場所を指定します.
この状態でkhalを叩くとこんな感じで表示されました.

$ khal
    Mo Tu We Th Fr Sa Su     Tomorrow, 2021-03-31
Mar  1  2  3  4  5  6  7     20:00-21:30 TechLION vol.37 〜TechLION出演者に聞く、この1年とこの10年。そして未来〜 :: # 視聴方法  本イベントはYouTubeにて配信します  視聴URLは決まり次第掲載します  #
     8  9 10 11 12 13 14     プログラム  2011年3月に旗揚げしたTechLIONが10周年を迎えるにあたり、記念試合をオンラインイベントとして行います。   ト
ークセッションでは、これまでTechLIONに出演してくださった皆さんに、IT業界の最近の状況、
    15 16 17 18 19 20 21     10年間の変化、今後の予想などをお聞きします。事前に実施したアンケート結果を見ながらMCの2名が進行しますが、回答によっ
ては出演者の方もトークにご参加いただきます。
    22 23 24 25 26 27 28     TechLION旗揚げ記念日でもある3月31日の夜、お酒を片手にお楽しみください!    * 出演:これまでのTechLIONにご出演の皆さ
ん   * MC:法林浩之、馮富久  # 開催概要    * 名称:TechLION
Apr 29 30 31  1  2  3  4     vol.37 〜TechLION出演者に聞く、この1年とこの10年。そして未来〜   * 主催:チームTechLION   * 協賛:USP研究所、技術評
論社   * 後援:日本UNIXユーザ会   *
     5  6  7  8  9 10 11     日時:2021年3月31日(水) 20:00開演、21:30終了予定   * 場所:オンライン (YouTubeにて配信)   * 料金:無料   * 公式タグ
:techlion   * 問い合わせ先:チームTechLION
    12 13 14 15 16 17 18     (E-mail:staff[at]techlion.jp)  # イベント詳細  https://techlion.jp/vol37
    19 20 21 22 23 24 25     https://techlion.connpass.com/event/205623/
May 26 27 28 29 30  1  2
     3  4  5  6  7  8  9
    10 11 12 13 14 15 16
    17 18 19 20 21 22 23
    24 25 26 27 28 29 30
Jun 31  1  2  3  4  5  6

ikhalコマンドを使うとダンプではなく,tuiのhalが起動してインタラクティブに操作できます.

ikhal

ちょっと冗長なので --format "title}" でタイトルだけ表示するようにしてみました.

$ khal list --format "{title}"
Tomorrow, 2021-03-31
TechLION vol.37 〜TechLION出演者に聞く、この1年とこの10年。そして未来〜

今日だけや,今日から7日間など範囲指定も出来ます.

$ khal list --format "{title}" today today (1)
$ khal list --format "{title}" today 7days (2)
$ khal list --format "{title}" 2021-03-01 2021-03-31 (3)
$ faketime 2021-03-03 khal list --format "{title}" today (4)
  1. 今日から今日まで表示
  2. 今日から7日間表示
  3. 03/01〜03/31まで表示
  4. faketimeで03/03としてkhalを実行

URLも表示したいけど,あっても.icsのDiscriptionの中などなのでちょっと面倒そうなので今回は諦めました.必要なときはカレンダーを参照することにします.

複数のカレンダーの設定

2つ目のカレンダーを登録してみます.次は日本の休日カレンダーを登録してみます.

~/.config/vdirsyncer/config に以下の設定を追記します.

[pair holiday_calendars]
a = "holiday_calendars_local"
b = "holiday_calendars_remote"
collections = null
metadata = ["color"]

[storage holiday_calendars_local]
type = "filesystem"
path = "~/.calendars/holiday_calendars/"
fileext = ".ics"

[storage holiday_calendars_remote]
type = "http"
url = "https://www.google.com/calendar/ical/ja.japanese%23holiday%40group.v.calendar.google.com/public/basic.ics"

設定反映してカレンダーを同期します.

$ vdirsyncer discover holiday_calendars
$ vdirsyncer sync holiday_calendars

次にkhalの設定です.
~/.config/khal/config[calendars] セクションに以下の設定を追記します.

  [[holiday]]
    path = ~/.calendars/holiday_calendars/
    readonly = True

この状態でkhalを実行してみると祝日も表示されるようになりました :)

$ khal list --format "{title}" 2021-03-20 1days
Saturday, 2021-03-20
春分の日
NBUG 2021年3月例会[第250回例会]
Kernel/VM探検隊online part2

ということで当初思っていたより少し面倒でしたが,とりあえずやりたいことが出来るようになりました :)

環境

$ dpkg-query -W vdirsyncer khal faketime
faketime        0.9.7-3
khal    1:0.9.10-1.1
vdirsyncer      0.16.7-2
$ lsb_release -dr
Description:    Debian GNU/Linux 10 (buster)
Release:        10
$ uname -m
x86_64

One thought to “カレンダーのDavCalをコマンドラインに出力したい”

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)