From b93de95dd24ff2a1d3126e6d7d00cd77530a03bf Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Fri, 24 Jul 2026 09:15:40 +0200 Subject: tradlit: offline caching + read; update pre-caches traditional; --clean prunes it; v0.2.0 --- internal/cli/cli.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'internal/cli') diff --git a/internal/cli/cli.go b/internal/cli/cli.go index c514607..130683c 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -18,6 +18,7 @@ import ( "github.com/lukaszkasprzak/lectio/internal/liturgy" "github.com/lukaszkasprzak/lectio/internal/readings" "github.com/lukaszkasprzak/lectio/internal/render" + "github.com/lukaszkasprzak/lectio/internal/tradlit" ) const helpText = `lectio — daily Catholic liturgy readings (Polish + 4 versions) @@ -241,18 +242,62 @@ func normalizeLectionary(lectionary string) (string, error) { } // runHarvest handles -u/--update: harvest sigla maximally (to the -// unpublished horizon) from date, printing the outcome or -- on a genuine -// interruption (see liturgy.Harvest) -- the error. +// unpublished horizon) from date for the modern lectionary, printing the +// outcome or -- on a genuine interruption (see liturgy.Harvest) -- the +// error. On a successful harvest it also best-effort pre-caches the +// traditional lectionary's propers (internal/tradlit) for every date in +// that same [date, furthest] window, so 'lectio update' prepares both +// lectionaries for offline use in one run. The 1962 calendar has no +// "unpublished horizon" (every date has propers), so this is a plain +// date-range fetch; a per-date failure is not fatal here -- it never +// prevented the traditional lectionary from working live before, and the +// modern-harvest outcome is still reported either way. func runHarvest(date string, stdout, stderr io.Writer) int { added, furthest, err := liturgy.Harvest(date, 0) if err != nil { fmt.Fprintln(stderr, "lectio:", err) return 1 } - fmt.Fprintf(stdout, "harvested %d day(s), furthest %s\n", added, furthest) + + lang := config.Default().TraditionalLang + cachedTrad := 0 + if furthest != "" { + if cfg, cfgErr := config.Load(); cfgErr == nil { + lang = cfg.TraditionalLang + cachedTrad = cacheTraditionalRange(date, furthest, lang) + } + } + + fmt.Fprintf(stdout, "harvested %d day(s), furthest %s; cached traditional propers (%s) for %d day(s)\n", + added, furthest, lang, cachedTrad) return 0 } +// cacheTraditionalRange best-effort pre-caches tradlit's traditional propers +// for lang for every date in [from, to] inclusive (walking forward a day at +// a time), returning how many dates succeeded. A per-date failure (e.g. a +// transient network hiccup) is ignored -- the traditional 1962 calendar has +// propers for every date, so there is no "unpublished horizon" to stop at +// the way liturgy.Harvest has for the modern lectionary. +func cacheTraditionalRange(from, to, lang string) int { + start, err := time.Parse("2006-01-02", from) + if err != nil { + return 0 + } + end, err := time.Parse("2006-01-02", to) + if err != nil { + return 0 + } + + cached := 0 + for d := start; !d.After(end); d = d.AddDate(0, 0, 1) { + if _, err := tradlit.Load(d.Format("2006-01-02"), lang, false); err == nil { + cached++ + } + } + return cached +} + // runClean handles -C/--clean: prune cached readings older than one year // (relative to today()) and print a human-readable summary of what was // removed. Like -u/--update, this is a maintenance mode -- it ignores DATE -- cgit v1.3