extract

package
v0.20.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 17 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package extract is the parse stage: one extractor per media type, each turning an item's bytes into units, such as a thread's post and comments or a book's pages. A registry keyed by media type picks the extractor, so an item parses the same whatever source sent it.

An extractor registers by joining builtin. One written in Go sets Extract. One whose library is not Go is a Program from the worker image, run through isolatedprocess. Each output is keyed by item, extractor and version, so a changed extractor takes a new version and its outputs write beside the old.

Index

Constants

View Source
const MediaTypePDF = "application/pdf"

MediaTypePDF is a PDF's media type, as a folder connector lands one.

View Source
const ProgramDir = "/usr/local/libexec/ingestion/extractors"

ProgramDir is where the worker image installs each extractor program.

View Source
const ProgramTimeout = 30 * time.Minute

ProgramTimeout is a program extractor's Timeout unless its registration sets another.

View Source
const PyMuPDFVersion = "1.28.2"

PyMuPDFVersion is the PyMuPDF that release/python-requirements.txt pins, and that the worker image installs for the PDF programs.

Variables

View Source
var PDFText = func() Extractor {
	e := Program("pdf-text", "1+pymupdf-"+PyMuPDFVersion, MediaTypePDF, ProgramDir+"/pdf-text")
	e.Positional = true
	return e
}()

PDFText reads a PDF's text with PyMuPDF, one unit per PDF page, so unit n is PDF page n, a blank page included. A unit's label is the page's printed label where the PDF declares page labels, and null where it declares none. Its version names PyMuPDF's, so a library upgrade writes its pages beside the old ones.

View Source
var ThreadText = Extractor{Name: "thread-text", Version: "1", MediaType: redditexport.MediaTypeSave, Extract: threadText}

ThreadText parses a Reddit save: its post, then each comment of its thread depth first, in the order Reddit sorted them. A comment that its author deleted or a moderator removed is left out, and its replies are kept. Each unit's label is its author.

Functions

func Capability

func Capability(e Extractor) string

Capability is the capability of e's parse tasks, so a worker claims only the tasks of extractors it runs.

Types

type Extractor

type Extractor struct {
	// Name names the extractor in each output it writes, such as thread-text.
	Name string
	// Version is written beside each output, so a changed extractor takes a
	// new one.
	Version   string
	MediaType string
	// Program is the absolute path of the extractor's program, and empty for
	// one written in Go. A worker runs it only where the program is installed.
	Program string
	// Extract reads body and answers its units in order.
	Extract func(ctx context.Context, body io.Reader) ([]Unit, error)
	// Timeout bounds one run of Extract, and none where it is zero. A run
	// past it fails its task, so a program that hangs on one item's bytes
	// frees its worker slot and the item waits in review.
	Timeout time.Duration
	// Positional keeps a unit with no text in its place, so unit n is the
	// artifact's own part n, such as a book's PDF page n. Otherwise such a
	// unit is dropped and the rest are numbered again.
	Positional bool
}

Extractor turns the bytes of one media type into a parse's units.

func Program

func Program(name, version, mediaType, path string, args ...string) Extractor

Program is an extractor that runs the program at path with args, such as one whose library is Python. The program reads the item's bytes on stdin, and writes one JSON line per unit on stdout, in order:

{"label": "Chapter 1", "text": "It was a bright cold day…"}

label may be null or left out, and any other field is refused. A run that exits non-zero fails its task, with the end of its stderr kept, and so does a run past ProgramTimeout. The program runs through isolatedprocess, so it holds none of the worker's credentials.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry picks the extractor for an item by its media type.

func Builtin

func Builtin() *Registry

Builtin is the registry of every extractor that ingestion holds.

func NewRegistry

func NewRegistry(extractors ...Extractor) (*Registry, error)

NewRegistry registers extractors. It refuses one with no name, version, media type or way to run, and two of one name or one media type.

func (*Registry) For

func (r *Registry) For(mediaType string) (Extractor, bool)

For is the extractor that reads mediaType, and false where none does.

func (*Registry) Handlers

func (r *Registry) Handlers(client *worker.Client, log *slog.Logger) []worker.Handler

Handlers is a worker handler for each extractor of r that runs here: each written in Go, and each whose program is installed. A worker claims only these extractors' tasks.

func (*Registry) QueueParse

func (r *Registry) QueueParse(ctx context.Context, tx *store.Tx, item store.Item) error

QueueParse queues a parse task for item where an extractor reads its media type. Landing calls it in the write that adds the item, so every item lands with its parse queued.

func (*Registry) Replayable

func (r *Registry) Replayable() server.Replayable

Replayable is the parse stage as a replay runs it again: a task for each item whose media type an extractor reads.

type Unit

type Unit struct {
	// Label is the unit's own heading, such as a comment's author or a
	// chapter's title.
	Label *string
	Text  string
}

Unit is one part of a parse, as an extractor writes it. The parse numbers its units from 1 in their order.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL