feat: ingest BookOrbit highlights as chapter entries (#50)
feat: ingest BookOrbit highlights as chapter entries (#50) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #50.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/david/knox/internal/db"
|
||||
"github.com/david/knox/internal/ingest"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// NewBookOrbitCmd ingests reading highlights from a self-hosted BookOrbit
|
||||
// instance into the knowledge index.
|
||||
func NewBookOrbitCmd(kdb *db.KnoxDB) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "bookorbit",
|
||||
Short: "Ingest BookOrbit highlights as per-chapter entries",
|
||||
Long: `Reads the annotation export from the BookOrbit REST API
|
||||
(BOOKORBIT_URL, BOOKORBIT_USERNAME, BOOKORBIT_PASSWORD) and records one entry
|
||||
per book chapter. Fingerprints by book and chapter, so added highlights update
|
||||
the chapter instead of creating a second copy of it.`,
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
ing := ingest.NewBookOrbitIngester()
|
||||
if ing.BaseURL == "" || ing.Username == "" || ing.Password == "" {
|
||||
return fmt.Errorf("bookorbit not configured: set BOOKORBIT_URL, BOOKORBIT_USERNAME and BOOKORBIT_PASSWORD")
|
||||
}
|
||||
log.Printf("[knox] ingesting BookOrbit highlights from %s...", ing.BaseURL)
|
||||
|
||||
results, err := ing.IngestAll()
|
||||
if err != nil {
|
||||
return fmt.Errorf("bookorbit ingest: %w", err)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
fmt.Println("No BookOrbit annotations found.")
|
||||
return nil
|
||||
}
|
||||
|
||||
var newCount int
|
||||
for _, result := range results {
|
||||
_, 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: "bookorbit_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("BookOrbit: %d chapter(s), %d new\n", len(results), newCount)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
return cmd
|
||||
}
|
||||
@@ -125,6 +125,14 @@ func NewIngestCmd(kdb *db.KnoxDB) *cobra.Command {
|
||||
record(r)
|
||||
}
|
||||
}
|
||||
log.Printf("[knox] ingesting bookorbit...")
|
||||
if results, err := ingest.NewBookOrbitIngester().IngestAll(); err == nil {
|
||||
for _, r := range results {
|
||||
record(r)
|
||||
}
|
||||
} else {
|
||||
log.Printf("[knox] bookorbit ingest failed: %v", err)
|
||||
}
|
||||
log.Printf("[knox] ingesting opencode.db...")
|
||||
if results, err := ingest.NewOpenCodeDBIngester().IngestAll(); err == nil {
|
||||
for _, r := range results {
|
||||
|
||||
Reference in New Issue
Block a user