record

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: MIT Imports: 15 Imported by: 0 modules, 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 record 持有 CLI 与 MCP 共用的 Pixiv 实体 JSON 记录投影,以及 CLI 的记录流与 列表输出。

记录保留源对象与未知字段,只固定顶层 id、type、url;解析时会拒绝额外的 JSON 值并 删除版本类元数据键。

包的组成:Record 是记录值,ParseRecordJSON 解析一行 NDJSON, ConsumeNDJSONRecords 逐行消费管道并写诊断;RunArtworks、RunNovels、RunUsers 与 RunPages 是 CLI 列表输出,JSONArraySpool 暂存完整 JSON 文档。

Index

Constants

View Source
const (
	// RecordFailureSkip 让失败记录只写诊断,然后继续处理后续记录。
	RecordFailureSkip = "skip"
	// RecordFailureFailFast 遇到第一条失败记录就停止并返回非零退出状态。
	RecordFailureFailFast = "fail-fast"
)

Variables

This section is empty.

Functions

func AppendJSONArray

func AppendJSONArray[T any](s *JSONArraySpool, items []T) error

AppendJSONArray 编码并追加一批数组元素。

func ConsumeNDJSONRecords

func ConsumeNDJSONRecords(ctx context.Context, in io.Reader, errOut io.Writer, operation string, failFast bool, consume func(context.Context, Record) error) error

ConsumeNDJSONRecords 逐行解析 NDJSON 并顺序处理记录。它不使用 Scanner,避免 合法的大记录受隐藏 token 限制;每次回调结束后才读取下一行,以便取消后不启动新任务。

func FatalRecordPipeline

func FatalRecordPipeline(err error) error

FatalRecordPipeline 标记无法继续读取记录流的失败,例如 stdout/stderr I/O 错误。 这类错误由调用方原样返回,不会被写成一条记录动作失败。

func ListValues

func ListValues[T any](items []T) ([]map[string]any, error)

ListValues 把非实体列表投影为 JSON 对象;有 id 时归一为字符串,保留其余字段。

func NewRecordActionError

func NewRecordActionError(code string, err error) error

NewRecordActionError 给一条记录动作失败附上稳定 code;err 为 nil 时返回 nil。

func PageMetadata

PageMetadata 根据分页计划保留显式 limit 与实际返回条数。

func RecordFailureStrategy

func RecordFailureStrategy(value string) (bool, error)

RecordFailureStrategy 把 --on-error 取值转换成是否遇错即停;非法取值返回错误。

func RequiredRecordID

func RequiredRecordID(value Record) (int64, error)

RequiredRecordID 取出记录的正整数 ID;缺失、非数字或非正数都返回错误。

func RunArtworks

func RunArtworks(ctx context.Context, runner session.Runner, request session.ProxyRequest, plan pagination.PagePlan, format Format, errOut io.Writer, list ArtworkList) error

RunArtworks 在账号池重放边界内执行一次分页作品读取。NDJSON 逐条提交,JSON 在完整 文档写入 stdout 后提交,文本在第一批可见后提交;因此 429 只会在结果暴露前换账号。

func RunNovels

func RunNovels(ctx context.Context, runner session.Runner, request session.ProxyRequest, plan pagination.PagePlan, format Format, errOut io.Writer, list NovelList) error

RunNovels 在同一个重放边界内执行一次分页小说读取。

func RunPages

func RunPages[T any](ctx context.Context, out, errOut io.Writer, plan pagination.PagePlan, jsonOut, ndjson bool, printText func([]T) error, toJSON func(T) any, fetch func(context.Context, sdk.Cursor) (sdk.Page[T], error)) error

RunPages 统一 FANBOX 列表命令的 JSON / NDJSON / 文本输出与分页。JSON 只有完整文档 写入临时文件成功后才交给 stdout。

func RunUsers

func RunUsers(ctx context.Context, runner session.Runner, request session.ProxyRequest, plan pagination.PagePlan, format Format, errOut io.Writer, list UserList) error

RunUsers 执行一次分页用户预览读取。

func WriteJSONLine

func WriteJSONLine(out io.Writer, value any) error

WriteJSONLine 输出一个紧凑 JSON 值并追加换行。

func WriteJSONValue

func WriteJSONValue(out io.Writer, value any) error

WriteJSONValue 输出一个两空格缩进的 JSON 值并追加换行。

Types

type ArtworkList

type ArtworkList struct {
	Fetch func(*pixiv.Client, context.Context, sdk.Cursor) ([]pixiv.Artwork, sdk.Cursor, error)
	// PostFilter 只在已解析的窗口内本地筛选,不会继续向上游多取数据。
	PostFilter func([]pixiv.Artwork) []pixiv.Artwork
	Print      func(items []pixiv.Artwork, start int) error
}

ArtworkList 描述一次作品列表读取及输出方式。

type Format

type Format int

Format 指定列表命令的输出格式,由 --json/--ndjson 决定;两个 flag 互斥。

const (
	// FormatText 输出面向终端的逐行文本。
	FormatText Format = iota
	// FormatJSON 输出一个完整 JSON 文档。
	FormatJSON
	// FormatNDJSON 每条记录输出一行 JSON。
	FormatNDJSON
)

func FormatOf

func FormatOf(jsonOut, ndjson bool) Format

FormatOf 把两个输出 flag 映射成一种列表格式,--ndjson 优先于 --json。

type JSONArraySpool

type JSONArraySpool struct {
	*Spool
	// contains filtered or unexported fields
}

JSONArraySpool 逐条暂存数组,避免把全部详情或列表保存在内存中。

func NewJSONArraySpool

func NewJSONArraySpool() (*JSONArraySpool, error)

NewJSONArraySpool 创建一个尚未结束的 JSON 数组。

func (*JSONArraySpool) Close

func (s *JSONArraySpool) Close()

Close 清理尚未提交或已经提交的数组暂存。

func (*JSONArraySpool) Commit

func (s *JSONArraySpool) Commit(out io.Writer) error

Commit 结束数组后复制到 out。

func (*JSONArraySpool) CommitList

func (s *JSONArraySpool) CommitList(out io.Writer, page pagination.PaginationOut) error

CommitList 结束数组,并写出与 NDJSON 共用元素的 records/pagination 文档。

func (*JSONArraySpool) Write

func (s *JSONArraySpool) Write(value []byte) (int, error)

Write 追加一个已编码的 JSON 值,并补上数组分隔符。

type List

type List[T any] struct {
	// Print 输出一批文本结果;offset 是本批在整体结果中的起点。
	Print func(items []T, offset int) error
	// RecordValue 把一项转成 JSON/NDJSON 共用记录。
	RecordValue func(T) (any, error)
}

List 描述一次分页列表的输出方式;JSON 与 NDJSON 共用记录投影。

type ListOutput

type ListOutput[T any] struct {
	Records    []T                      `json:"records"`
	Pagination pagination.PaginationOut `json:"pagination"`
}

ListOutput 写出列表文档;空列表编码为 []。

func NewListOutput

func NewListOutput[T any](items []T, plan pagination.PagePlan, result pagination.PageResult) ListOutput[T]

NewListOutput 把分页结果和已投影的记录组成列表文档。

type NovelList

type NovelList struct {
	Fetch func(*pixiv.Client, context.Context, sdk.Cursor) ([]pixiv.Novel, sdk.Cursor, error)
	Print func(items []pixiv.Novel) error
}

NovelList 描述一次小说列表读取及输出方式。

type PipelineDiagnosticError

type PipelineDiagnosticError struct{}

PipelineDiagnosticError 表示错误已经按管道契约写入 stderr。它让根命令保留 非零退出状态,同时不再附加普通的 "error:" 文本破坏逐行诊断。

func (*PipelineDiagnosticError) Error

Error 返回固定文本;逐行诊断已经写入 stderr。

type Record

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

Record 保留可在 CLI 管道和 MCP 之间共享的 Pixiv 实体 JSON 记录。 它保留源对象和外部程序提供的未知字段,同时固定顶层 id、type、url。 零值尚未初始化;读取身份字段前应通过构造或 JSON 解析入口建立记录。

func FromArtworks

func FromArtworks(items []pixiv.Artwork) ([]Record, error)

FromArtworks 批量构造 artwork records。

func FromNovels

func FromNovels(items []pixiv.Novel) ([]Record, error)

FromNovels 批量构造 novel records。

func FromUserPreviews

func FromUserPreviews(items []pixiv.UserPreview) ([]Record, error)

FromUserPreviews 批量构造 user records。

func NewIdentityRecord

func NewIdentityRecord(id int64, recordType string) (Record, error)

NewIdentityRecord 构造只包含 Pixiv canonical identity 的管道记录。 artwork 表示尚无法可靠判断 illust/manga/ugoira 子类型的作品身份。

func ParseRecordJSON

func ParseRecordJSON(line []byte) (Record, error)

ParseRecordJSON 解析一行 NDJSON 中的单个对象,并保留未知字段。

func RecordFromArtwork

func RecordFromArtwork(artwork pixiv.Artwork) (Record, error)

RecordFromArtwork 将 public artwork 映射为管道记录。

func RecordFromNovel

func RecordFromNovel(novel pixiv.Novel) (Record, error)

RecordFromNovel 将 public novel 映射为管道记录。

func RecordFromNovelContent

func RecordFromNovelContent(content pixiv.NovelContent) (Record, error)

RecordFromNovelContent 将结构化小说正文映射为可继续管道处理的小说记录。

func RecordFromUserDetail

func RecordFromUserDetail(detail pixiv.UserDetail) (Record, error)

RecordFromUserDetail 将用户详情映射为管道记录。

func RecordFromUserPreview

func RecordFromUserPreview(preview pixiv.UserPreview) (Record, error)

RecordFromUserPreview 将用户预览映射为管道记录。

func (Record) ID

func (r Record) ID() string

ID 返回记录的稳定字符串标识。

func (Record) MarshalJSON

func (r Record) MarshalJSON() ([]byte, error)

MarshalJSON 保留记录中的未知字段,并始终输出一个 JSON 对象。

func (Record) Type

func (r Record) Type() string

Type 返回记录的实体类型。

func (Record) URL

func (r Record) URL() string

URL 返回记录的规范 Pixiv URL。

func (*Record) UnmarshalJSON

func (r *Record) UnmarshalJSON(data []byte) error

UnmarshalJSON 让 MCP structured content 等 JSON 消费者可以把 records 重新解码为 Record,同时复用 NDJSON 输入的完整校验、ID 归一与版本元数据清理规则。

type Spool

type Spool struct{ *os.File }

Spool 暂存完整输出;成功后 Commit 才把内容交给 stdout。

func NewSpool

func NewSpool() (*Spool, error)

NewSpool 创建权限为 0600 的临时文档。

func (*Spool) Close

func (s *Spool) Close()

Close 关闭并删除临时文档。

func (*Spool) Commit

func (s *Spool) Commit(out io.Writer) error

Commit 从头复制暂存内容到 out。

type UserList

type UserList struct {
	Fetch func(*pixiv.Client, context.Context, sdk.Cursor) ([]pixiv.UserPreview, sdk.Cursor, error)
	Print func(items []pixiv.UserPreview) error
}

UserList 描述一次用户预览列表读取及输出方式。

Jump to

Keyboard shortcuts

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