pagesift 1.2.4

A single-binary, Firecrawl-compatible web scraping engine that turns pages into clean Markdown.
Documentation
#![forbid(unsafe_code)]

#[macro_use]
mod output;
mod args;
mod bench;
mod cli;
mod commands;
mod util;

use std::io::Read;
use std::path::PathBuf;
use std::process::ExitCode;
use std::sync::Arc;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use pagesift_content::{Options, process};
use tracing_subscriber::EnvFilter;
use url::Url;

use args::*;
use util::*;

/// 小块(≤512 字节)走按 NUMA 节点分片的线程缓存,大块交给系统分配器。
#[global_allocator]
static ALLOC: pagesift_sys::SmallCache = pagesift_sys::SmallCache;

#[derive(Parser)]
#[command(
    name = "pagesift",
    version,
    about = "Turn web pages into clean Markdown for LLMs"
)]
struct Cli {
    /// Config file (default: $PAGESIFT_CONFIG, else the platform config path; empty disables config files)
    #[arg(long, global = true)]
    config: Option<PathBuf>,
    /// Append one JSON line per fetch (URL, engine, status, timings, warnings) to this file, and keep HTML snapshots of suspicious results next to it
    #[arg(long, global = true, value_name = "PATH")]
    audit_log: Option<PathBuf>,
    /// Turn off per-site rate limiting (request interval, per-site concurrency, Crawl-delay); only the global concurrency limit applies. Same as `[policy] politeness = false`
    #[arg(long, global = true)]
    no_politeness: bool,
    /// Turn off per-site backoff after 429 / 503 with Retry-After and repeated failures. Same as `[policy] backoff = false`
    #[arg(long, global = true)]
    no_backoff: bool,
    /// Ignore robots.txt for every request. Same as `[policy] robots = false`
    #[arg(long, global = true)]
    no_robots: bool,
    /// Send HTTP fetches with this browser's TLS, HTTP/2 and request headers: chrome or chrome_156. Sites then see that browser, not pagesift; whether that meets a site's terms is up to you. robots.txt and rate limits still apply. Same as `[fetch] browser_fingerprint`
    #[arg(long, global = true, value_name = "PROFILE")]
    browser_fingerprint: Option<String>,
    /// Turn on OCR for scanned PDF pages; models are downloaded the first time they are needed. Same as `[ocr] enabled = true`
    #[arg(long, global = true)]
    ocr: bool,
    /// Also recognize display formulas as LaTeX (downloads 860 MB more; seconds per formula). Same as `[ocr] formulas = true`
    #[arg(long, global = true)]
    ocr_formulas: bool,
    /// GPU for OCR: auto, off, cuda or cuda:N. Same as `[gpu] mode`
    #[arg(long, global = true, value_name = "MODE")]
    gpu: Option<pagesift::config::GpuMode>,
    /// Directory holding the OCR models. Same as `[ocr] models_dir`
    #[arg(long, global = true, value_name = "PATH")]
    models_dir: Option<PathBuf>,
    #[command(subcommand)]
    command: Command,
}

fn main() -> ExitCode {
    pagesift_sys::tune_allocator();
    let matches = <Cli as clap::CommandFactory>::command().get_matches();
    let cli = match <Cli as clap::FromArgMatches>::from_arg_matches(&matches) {
        Ok(c) => c,
        Err(e) => e.exit(),
    };
    let mut cfg = match pagesift::config::Config::load(cli.config.as_deref()) {
        Ok(c) => c,
        Err(e) => {
            eprintln!("error: {e:#}");
            return ExitCode::FAILURE;
        }
    };
    if cli.audit_log.is_some() {
        cfg.audit.path = cli.audit_log;
    }
    cfg.audit.source = matches.subcommand_name().map(String::from);
    if cli.browser_fingerprint.is_some() {
        cfg.fetch.browser_fingerprint = cli.browser_fingerprint;
    }
    cfg.ocr.enabled |= cli.ocr;
    cfg.ocr.formulas |= cli.ocr_formulas;
    if let Some(mode) = cli.gpu {
        cfg.gpu.mode = mode;
    }
    if cli.models_dir.is_some() {
        cfg.ocr.models_dir = cli.models_dir;
    }
    for (flag, switch) in [
        (cli.no_politeness, &mut cfg.policy.politeness),
        (cli.no_backoff, &mut cfg.policy.backoff),
        (cli.no_robots, &mut cfg.policy.robots),
    ] {
        if flag {
            *switch = Some(false);
        }
    }
    let off = cfg.policy.disabled_switches();
    if !off.is_empty() {
        eprintln!("note: crawl policy switched off: {}", off.join(", "));
    }
    // 结构化日志:PAGESIFT_LOG 定级别,PAGESIFT_LOG_FORMAT 或 [log] format 选 text / json
    let json = match cfg.log_json() {
        Ok(j) => j,
        Err(e) => {
            eprintln!("error: {e:#}");
            return ExitCode::FAILURE;
        }
    };
    let logs = tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_env("PAGESIFT_LOG"))
        .with_writer(std::io::stderr);
    if json {
        logs.json().flatten_event(true).init();
    } else {
        logs.init();
    }
    match run(cli.command, cfg) {
        Ok(code) => code,
        Err(e) => {
            eprintln!("error: {e:#}");
            ExitCode::FAILURE
        }
    }
}

fn run(command: Command, cfg: pagesift::config::Config) -> Result<ExitCode> {
    match command {
        command @ (Command::Inspect { .. }
        | Command::Relocate { .. }
        | Command::DiscoverJs { .. }
        | Command::MediaExtract { .. }
        | Command::MediaArchive { .. }
        | Command::StreamPlan { .. }
        | Command::StreamDownload { .. }
        | Command::SessionFetch { .. }
        | Command::Download { .. }) => commands::assets::run(command, cfg),
        command @ (Command::Collect { .. }
        | Command::Convert { .. }
        | Command::Bench { .. }
        | Command::BenchRender { .. }
        | Command::Scrape { .. }
        | Command::Crawl { .. }
        | Command::Map { .. }) => commands::pages::run(command, cfg),
        command @ (Command::Serve { .. }
        | Command::Diagnose { .. }
        | Command::Tune { .. }
        | Command::Doctor
        | Command::Mcp { .. }) => commands::service::run(command, cfg),
        Command::Models { action } => commands::models::run(action, cfg),
        Command::Agent {
            prompt,
            urls,
            schema,
            effort,
            strict_constrain_to_urls,
            max_credits,
            allowed_hosts,
            include_evidence,
            allow,
        } => commands::agent::run(
            commands::agent::AgentArgs {
                prompt,
                urls,
                schema,
                effort,
                strict_constrain_to_urls,
                max_credits,
                allowed_hosts,
                include_evidence,
                allow,
            },
            cfg,
        ),
        Command::Similar {
            url,
            selector,
            engine,
            ignore_robots,
            allow,
        } => commands::extract::run_similar(
            commands::extract::SimilarArgs {
                url,
                selector,
                engine,
                ignore_robots,
                allow,
            },
            cfg,
        ),
        Command::Extract {
            urls,
            schema,
            engine,
            ignore_robots,
            concurrency,
            allow,
        } => commands::extract::run(
            commands::extract::ExtractArgs {
                urls,
                schema,
                engine,
                ignore_robots,
                concurrency,
                allow,
            },
            cfg,
        ),
    }
}