Files
knox/internal/cmd/brevier.go
T
david e7b65c1fea
build / test (push) Has been cancelled
build / image (push) Has been cancelled
feat: ingest Brevier reading copies and phone notes (#48)
Brevier keeps a copy of every page read and exports notes from the phone, and
none of it reached knox. This adds a source for both.

Archive copies are Markdown with title/source/read front matter, written as
<archive>/<host>/<date>-<slug>.md.lz4 or .md depending on the reader's setting.
Both forms are read: the setting only changes how new copies are written, and
the older ones stay compressed. Entries are keyed by the source URL, so a page
read on several days updates one entry and the content hash carries the
version, instead of the archive's one-file-per-day turning into duplicate
entries.

The phone is a different problem. Its archive is app-private and never leaves
the device (upstream leaves bulk export undecided), so what can arrive is the
notes export and per-page saves. Those are dropped into an inbox directory and
read in both shapes Brevier produces: one file with front matter, a zip with one
page per file, and a single file with '## [title](url)' sections. A saved
article carries no address, so it is keyed by its content; re-saving an
unchanged page dedupes.

Also checks the Flatpak sandbox path for the archive, since that is where a
desktop install actually writes.
2026-11-08 16:31:11 -08:00

79 lines
2.3 KiB
Go

package cmd
import (
"fmt"
"log"
"github.com/david/knox/internal/db"
"github.com/david/knox/internal/ingest"
"github.com/spf13/cobra"
)
// NewBrevierCmd ingests the local Brevier archive and any notes exported from
// the phone into the knowledge index.
func NewBrevierCmd(kdb *db.KnoxDB) *cobra.Command {
cmd := &cobra.Command{
Use: "brevier",
Short: "Ingest Brevier reading copies and phone note exports",
Long: `Reads the Brevier archive (BREVIER_ARCHIVE, default
~/.local/share/brevier/archive) and a drop directory for notes exported from
the phone (BREVIER_INBOX, default ~/.local/share/knox/inbox/brevier).
Archive copies are keyed by their source URL, so a page read again on another
day updates its entry instead of adding a second one.`,
RunE: func(c *cobra.Command, args []string) error {
ing := ingest.NewBrevierIngester()
if ing.Archive == "" && ing.Inbox == "" {
return fmt.Errorf("brevier not configured: set BREVIER_ARCHIVE and BREVIER_INBOX")
}
log.Printf("[knox] ingesting Brevier from %s and %s...", ing.Archive, ing.Inbox)
results, err := ing.IngestAll()
if err != nil {
return fmt.Errorf("brevier ingest: %w", err)
}
if len(results) == 0 {
fmt.Println("No Brevier copies or note exports found.")
return nil
}
var newCount, copies, notes int
for _, result := range results {
switch result.ContentType {
case "reading/copy":
copies++
case "reading/notes":
notes++
}
_, isNew, err := kdb.RecordObservation(db.ObservationRecord{
Fingerprint: result.Fingerprint,
SourceID: result.SourceID,
SourcePath: result.SourcePath,
Project: result.Project,
ContentType: result.ContentType,
Title: result.Title,
Summary: result.Summary,
Content: result.Content,
CreatedAt: result.CreatedAt,
Confidence: result.Confidence,
IngesterVersion: result.IngesterVersion,
Trigger: "brevier_ingest",
Provenance: ingest.ProvenanceJSON(result.Provenance),
ContentHash: result.ContentHash,
})
if err != nil {
log.Printf("[knox] db error: %v", err)
continue
}
if isNew {
newCount++
}
}
fmt.Printf("Brevier: %d copies, %d note export(s), %d new\n", copies, notes, newCount)
return nil
},
}
return cmd
}