Skip to main content

douyin_cli/
cli.rs

1use clap::{Args, CommandFactory, Parser, Subcommand};
2use serde_json::{Value, json};
3
4use crate::{api, auth, comments, crawler, mcp, obscura, settings, subtitles};
5
6const VERSION: &str = env!("CARGO_PKG_VERSION");
7
8#[derive(Debug, Parser)]
9#[command(name = "douyin", version, about = "抖音 CLI(Rust)")]
10#[command(long_about = "通用抖音命令行工具的 Rust 实现。更多命令见:douyin COMMAND --help")]
11struct Cli {
12    #[command(flatten)]
13    crawl: crawler::CrawlArgs,
14    #[command(subcommand)]
15    command: Option<Command>,
16}
17
18#[derive(Debug, Subcommand)]
19enum Command {
20    /// 调用抖音开放平台官方 OpenAPI
21    Api(api::ApiArgs),
22    /// 管理授权
23    Auth(auth::AuthArgs),
24    /// 抓取作品评论区
25    #[command(hide = true)]
26    Comment(comments::CommentArgs),
27    /// 通过 stdio 启动抖音 MCP 服务器
28    Mcp,
29    /// Obscura 集成辅助命令
30    Obscura(ObscuraArgs),
31    /// 从本地视频/音频生成字幕
32    Subtitle(subtitles::SubtitleArgs),
33}
34
35#[derive(Debug, Args)]
36struct ObscuraArgs {
37    #[command(subcommand)]
38    command: ObscuraCommand,
39}
40
41#[derive(Debug, Subcommand)]
42enum ObscuraCommand {
43    /// 输出 Obscura 集成 manifest
44    Manifest,
45    /// 检查本地 Obscura 集成状态
46    Status {
47        /// Obscura 可执行文件名
48        #[arg(long, default_value = "obscura")]
49        binary: String,
50    },
51}
52
53pub fn run() -> Result<(), String> {
54    let cli = Cli::parse();
55    match cli.command {
56        Some(Command::Api(args)) => api::run(args),
57        Some(Command::Auth(args)) => auth::run(args),
58        Some(Command::Comment(args)) => comments::run(args),
59        Some(Command::Mcp) => mcp::run_stdio(),
60        Some(Command::Obscura(args)) => run_obscura(args.command),
61        Some(Command::Subtitle(args)) => subtitles::run(args),
62        None if cli.crawl.should_run() => crawler::run(cli.crawl),
63        None => {
64            Cli::command()
65                .print_long_help()
66                .map_err(|error| error.to_string())?;
67            println!();
68            Ok(())
69        }
70    }
71}
72
73fn run_obscura(command: ObscuraCommand) -> Result<(), String> {
74    match command {
75        ObscuraCommand::Manifest => {
76            print_json(&obscura::manifest(VERSION, &settings::settings_file()))
77        }
78        ObscuraCommand::Status { binary } => {
79            let data = settings::load().map_err(|error| error.to_string())?;
80            let openapi = settings::openapi(&data);
81            let authorized = !string_value(&openapi, "accessToken").is_empty()
82                && !string_value(&openapi, "openId").is_empty();
83            print_json(&json!({
84                "douyin": {
85                    "version": VERSION,
86                    "entrypoint": "douyin",
87                    "configFile": settings::settings_file(),
88                    "authorized": authorized
89                },
90                "obscura": obscura::status(&binary),
91                "next": {
92                    "auth": ["douyin", "auth", "login"],
93                    "machineStatus": ["douyin", "auth", "status", "--json"],
94                    "manifest": ["douyin", "obscura", "manifest"]
95                }
96            }))
97        }
98    }
99}
100
101fn string_value<'a>(values: &'a serde_json::Map<String, Value>, key: &str) -> &'a str {
102    values.get(key).and_then(Value::as_str).unwrap_or("")
103}
104
105fn print_json(value: &Value) -> Result<(), String> {
106    println!(
107        "{}",
108        serde_json::to_string_pretty(value).map_err(|error| error.to_string())?
109    );
110    Ok(())
111}