znovel-harvest

extract random text files to consumable format

znovel-harvest

A fast, zero-dependency Crystal shard and CLI for extracting chapter boundaries, body formatting, ad/watermark diagnostics, and clean metadata from raw Chinese web novels and ebooks (.txt).

It uses a hybrid 4-stage pipeline:

  1. Encoding & Body Format Detection: Pure-stdlib UTF-8, UTF-16 (with or without BOM), and GB18030 decoding; deterministic indent (fullwidth_2, ascii_spaces, none) and paragraph spacing analysis.
  2. Pure-Code Chapter Detection → AI Fallback: Runs 9 battle-tested PCRE2 rules (第X章, ===第X章===, 1章:..., 正文 第001 ..., 001 ..., Chapter/Part N, 一、..., 【...】, 分卷阅读N, unindented bare numbers) with distribution sanity checks. Only falls back to an OpenAI-compatible LLM when code rules don't find a healthy split.
  3. AI Title Verification: Flags suspicious candidates (SUSPECT_TOC, SUSPECT_DUP, SUSPECT_SHORT, SUSPECT_LONG, SUSPECT_PUNCT, OUTLIER_LONG) and prunes Table of Contents blocks, duplicate headers, and narrative false positives while conservatively preserving standalone author notices (请假, 公告, 番外, 楔子, 后记).
  4. Ad Diagnostics & Metadata Extraction: Reports preamble, in-title, and repeating in-body ads/watermarks (AdReport) and extracts clean title, author, and synopsis from filename + preamble (stripping pirate site tags, group numbers, and URLs).

Installation

As a Crystal Shard

Add to your shard.yml:

dependencies:
  znovel-harvest:
    github: your-org/znovel-harvest # or path: /hub/cvrun/znovel-harvest

Then run shards install.

Build the CLI

git clone ... && cd znovel-harvest
shards build --release
# Binary is at ./bin/znovel-harvest

Configuration

znovel-harvest connects to any OpenAI-compatible /chat/completions endpoint and defaults to DeepSeek (https://api.deepseek.com, model deepseek-flash).

Copy config.example.yml to config.yml (or ~/.config/znovel-harvest/config.yml):

llm:
  base_url: 'https://api.deepseek.com'
  api_key: '' # Or export DEEPSEEK_API_KEY / ZNOVEL_API_KEY
  model: 'deepseek-flash'
  temperature: 0.1
  max_tokens: 4096
  reasoning_effort: null # Optional global override for all stages
  reasoning:
    detect_chapters: "low"
    verify_titles: "none"
    extract_metadata: "none"
  timeout_seconds: 120
  retries: 3
  extra_headers: {}

cache:
  enabled: true
  dir: '.cache/znovel-harvest'

pipeline:
  verify_titles: true
  prompts_dir: null

Config Resolution & Environment Overrides

Config lookup order: --config PATH → $ZNOVEL_CONFIG → ./config.yml → ~/.config/znovel-harvest/config.yml → built-in defaults.

Environment variables override values loaded from config.yml, and CLI flags override everything:

Environment Variable Overrides Notes
ZNOVEL_CONFIG Config file path Checked if --config not passed
ZNOVEL_BASE_URL / OPENAI_BASE_URL llm.base_url ZNOVEL_BASE_URL takes precedence
ZNOVEL_API_KEY / DEEPSEEK_API_KEY / OPENAI_API_KEY llm.api_key Checked in that order
ZNOVEL_MODEL llm.model e.g. deepseek-flash, gpt-4o-mini
ZNOVEL_TEMPERATURE llm.temperature Float
ZNOVEL_MAX_TOKENS llm.max_tokens Integer
ZNOVEL_REASONING_EFFORT llm.reasoning_effort none clears it
ZNOVEL_TIMEOUT llm.timeout_seconds Integer
ZNOVEL_RETRIES llm.retries Integer
ZNOVEL_CACHE_ENABLED cache.enabled true/1 or false/0
ZNOVEL_CACHE_DIR cache.dir Local SHA256 cache folder
ZNOVEL_VERIFY_TITLES pipeline.verify_titles true/false
ZNOVEL_PROMPTS_DIR pipeline.prompts_dir Folder with custom .md prompts

CLI Usage (One File Per Invocation)

# Default human-readable report (using DEEPSEEK_API_KEY)
DEEPSEEK_API_KEY=sk-... ./bin/znovel-harvest /path/to/novel.txt

# JSON output with full chapter byte offsets
./bin/znovel-harvest --json --all-chapters /path/to/novel.txt > info.json

# Fast pure-code mode (0 LLM calls)
./bin/znovel-harvest --code-only --summary /path/to/novel.txt

# OpenRouter or local Ollama override via CLI flags
./bin/znovel-harvest -b https://openrouter.ai/api/v1 -k $OPENROUTER_KEY -m deepseek/deepseek-chat novel.txt
./bin/znovel-harvest -b http://localhost:11434/v1 -m qwen2.5:14b novel.txt

# Parallel batch processing across a folder tree
find /path/to/novels -type f -iname '*.txt' -print0 | \
  xargs -0 -P 8 -I {} ./bin/znovel-harvest --json {} >> results.jsonl

Library Usage

require "znovel-harvest"

config = ZnovelHarvest.load_config("config.yml")
result = ZnovelHarvest.extract("/path/to/novel.txt", config: config, include_chapters: true)

puts "Title: #{result.metadata.title} | Author: #{result.metadata.author}"
puts "Chapters: #{result.chapter_count} (rule: #{result.chapter_rule.source}:#{result.chapter_rule.rule_name})"
puts "Body ads: #{result.ad_report.body_ad_line_count}"

result.chapters.try &.each do |ch|
  puts "#{ch.index + 1}. #{ch.title} (@#{ch.byte_start}, #{ch.char_len} chars)"
end
Repository

znovel-harvest

Owner
Statistic
  • 0
  • 0
  • 0
  • 0
  • 0
  • about 3 hours ago
  • November 26, 2026
License

MIT License

Links
Synced at

Thu, 26 Nov 2026 14:56:13 GMT

Languages