diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-07-23 13:13:27 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-07-23 13:13:27 +0200 |
| commit | 8a401a84af52c94f991c74443b7f4397e30cad4a (patch) | |
| tree | 257728d158c2676fe4643af89ab2bbee9476dd60 /internal/liturgy/fetch.go | |
| parent | a8170d4032c6afa175d887b414e6799f088f070c (diff) | |
| download | lectio-8a401a84af52c94f991c74443b7f4397e30cad4a.tar.gz lectio-8a401a84af52c94f991c74443b7f4397e30cad4a.zip | |
liturgy: sigla harvest + offline load
Diffstat (limited to 'internal/liturgy/fetch.go')
| -rw-r--r-- | internal/liturgy/fetch.go | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/internal/liturgy/fetch.go b/internal/liturgy/fetch.go index 1b1458f..d6cc4b6 100644 --- a/internal/liturgy/fetch.go +++ b/internal/liturgy/fetch.go @@ -32,9 +32,8 @@ type Options struct { Date string // Refresh bypasses both cache layers and re-fetches from the network. Refresh bool - // Offline restricts Load to previously cached/harvested data, never - // hitting the network. Its behavior is added in a later task; for now - // it is a no-op on the modern-source path. + // Offline restricts Load to previously cached/harvested data (see + // LoadOffline), never hitting the network. Offline bool } @@ -55,12 +54,20 @@ func cacheDir() string { // Load returns the day's reading sections, from the modern (niedziela.pl) // source, preferring cache over network: // +// 0. If Offline, skip straight to LoadOffline (the harvested sigla TSV). // 1. Unless Refresh, the parsed JSON cache ({date}.json). // 2. Unless Refresh, the raw HTML cache ({date}.html) -- parsed, and the // result written to the JSON cache for next time. // 3. Otherwise, fetch the page over the network, parse it, and -- only if -// the page is fully published -- write both cache layers. +// the page is fully published -- write both cache layers. A fetch error +// here (e.g. no network) falls back to LoadOffline for this date if it +// has been harvested, and only surfaces the original fetch error if +// that fallback also fails. func Load(opts Options) ([]Section, error) { + if opts.Offline { + return LoadOffline(opts.Date) + } + dir := cacheDir() jsonPath := filepath.Join(dir, opts.Date+".json") htmlPath := filepath.Join(dir, opts.Date+".html") @@ -81,6 +88,11 @@ func Load(opts Options) ([]Section, error) { page, err := fetch(opts.Date) if err != nil { + // Network is unreachable: fall back to a prior harvest of this date + // if there is one, rather than failing outright. + if secs, offErr := LoadOffline(opts.Date); offErr == nil { + return secs, nil + } return nil, err } |
