douyin-cli 2026.8.23

A Rust CLI for Douyin OpenAPI and web workflows
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
use std::path::PathBuf;
use std::thread;
use std::time::Duration;

use clap::{Args, ValueEnum};
use reqwest::blocking::Client;
use serde_json::{Map, Value, json};

use crate::err;
use crate::net::{self, sign};
use crate::{fs_utils, settings};

const BASE_URL: &str = "https://www.douyin.com";
const COMMENT_LIST: &str = "/aweme/v1/web/comment/list/";
const COMMENT_REPLIES: &str = "/aweme/v1/web/comment/list/reply/";

#[derive(Debug, Args)]
pub struct CommentArgs {
    /// 作品 ID、视频 URL 或图文 URL
    target: String,
    /// 最多抓取一级评论数,0 表示不限制
    #[arg(short, long, default_value_t = 100)]
    limit: usize,
    /// 每页请求数量(抖音网页接口上限为 20)
    #[arg(long, default_value_t = 20, value_parser = parse_comment_count)]
    count: usize,
    /// 同时抓取评论楼中楼回复
    #[arg(long)]
    with_replies: bool,
    /// 每条评论最多抓取回复数,0 表示不限制
    #[arg(long, default_value_t = 20)]
    reply_limit: usize,
    /// 分页请求间隔秒数
    #[arg(long = "sleep", visible_alias = "sleep-seconds", default_value_t = 0.8, value_parser = parse_non_negative_f64)]
    sleep_seconds: f64,
    /// 输出文件;不传则输出到 stdout
    #[arg(short, long)]
    output: Option<PathBuf>,
    /// 输出格式
    #[arg(long = "format", value_enum, default_value_t = OutputFormat::Raw)]
    output_format: OutputFormat,
    #[arg(long, default_value = "user")]
    comment_role: String,
    #[arg(long, default_value = "assistant")]
    reply_role: String,
    #[arg(long, default_value_t = 0)]
    min_comment_digg: i64,
    #[arg(long, default_value_t = 0)]
    min_reply_digg: i64,
    #[arg(long)]
    include_single_comments: bool,
    /// 本次运行使用的 Cookie;默认读取保存的 Cookie
    #[arg(short, long, env = "DOUYIN_COOKIE")]
    cookie: Option<String>,
}

#[derive(Clone, Debug, ValueEnum)]
enum OutputFormat {
    Raw,
    ChatmlJsonl,
    ChatmlJson,
}

pub fn run(args: CommentArgs) -> Result<(), String> {
    let saved = settings::load().map_err(err)?;
    let (cookie_value, user_agent) = net::credentials(&saved, args.cookie.as_deref())?;
    let aweme_id = extract_aweme_id(&args.target)?;
    let crawler = CommentCrawler::new(&cookie_value, user_agent)?;
    let data = crawler.crawl(&aweme_id, &args)?;
    let output = match args.output_format {
        OutputFormat::Raw => serde_json::to_string_pretty(&data).map_err(err)?,
        OutputFormat::ChatmlJson => {
            serde_json::to_string_pretty(&format_chatml(&data, &args)).map_err(err)?
        }
        OutputFormat::ChatmlJsonl => format_chatml(&data, &args)
            .iter()
            .map(serde_json::to_string)
            .collect::<Result<Vec<_>, _>>()
            .map_err(err)?
            .join("\n"),
    };
    fs_utils::write_output(&output, args.output.as_deref())?;
    if let Some(path) = args.output {
        eprintln!("评论已保存: {}", path.display());
    }
    Ok(())
}

struct CommentCrawler {
    client: Client,
    user_agent: String,
    common_params: Vec<(&'static str, String)>,
}

impl CommentCrawler {
    fn new(cookie: &str, user_agent: &str) -> Result<Self, String> {
        Ok(Self {
            client: net::web_client(cookie, user_agent, 30)?,
            user_agent: user_agent.to_owned(),
            common_params: net::web_query_params(cookie),
        })
    }

    fn crawl(&self, aweme_id: &str, args: &CommentArgs) -> Result<Value, String> {
        let comments = self.crawl_pages(
            COMMENT_LIST,
            vec![("aweme_id", aweme_id.to_owned())],
            args.limit,
            args,
            |raw| {
                let mut comment = normalize_comment(raw);
                if args.with_replies {
                    let comment_id = comment.get("id").and_then(Value::as_str).unwrap_or("");
                    comment["replies"] =
                        Value::Array(self.crawl_replies(aweme_id, comment_id, args)?);
                }
                Ok(comment)
            },
        )?;
        Ok(json!({"aweme_id": aweme_id, "comments": comments}))
    }

    fn crawl_replies(
        &self,
        aweme_id: &str,
        comment_id: &str,
        args: &CommentArgs,
    ) -> Result<Vec<Value>, String> {
        self.crawl_pages(
            COMMENT_REPLIES,
            vec![
                ("item_id", aweme_id.to_owned()),
                ("comment_id", comment_id.to_owned()),
            ],
            args.reply_limit,
            args,
            |raw| Ok(normalize_comment(raw)),
        )
    }

    /// Pages through a comment endpoint, applying `normalize` to every raw comment
    /// until the endpoint reports no more data or `limit` is reached.
    fn crawl_pages(
        &self,
        path: &str,
        base_params: Vec<(&'static str, String)>,
        limit: usize,
        args: &CommentArgs,
        mut normalize: impl FnMut(&Value) -> Result<Value, String>,
    ) -> Result<Vec<Value>, String> {
        let mut items = Vec::new();
        let mut cursor = 0_i64;
        let mut has_more = true;
        while has_more && !net::limit_reached(items.len(), limit) {
            let mut params = base_params.clone();
            params.extend([
                ("cursor", cursor.to_string()),
                ("count", args.count.to_string()),
                ("item_type", "0".to_owned()),
                ("insert_ids", String::new()),
                ("whale_cut_token", String::new()),
                ("cut_version", "1".to_owned()),
                ("rcFT", String::new()),
            ]);
            let page = self.fetch_page(path, params)?;
            let values = page
                .get("comments")
                .and_then(Value::as_array)
                .cloned()
                .unwrap_or_default();
            if values.is_empty() {
                break;
            }
            for raw in &values {
                items.push(normalize(raw)?);
                if net::limit_reached(items.len(), limit) {
                    break;
                }
            }
            let next_cursor = page
                .get("cursor")
                .and_then(net::value_i64)
                .unwrap_or(cursor);
            has_more = net::truthy(page.get("has_more"));
            if has_more && next_cursor == cursor {
                return Err("评论分页游标没有推进,已停止以避免重复请求".to_owned());
            }
            cursor = next_cursor;
            pause(has_more, args.sleep_seconds);
        }
        Ok(items)
    }

    fn fetch_page(&self, path: &str, mut params: Vec<(&str, String)>) -> Result<Value, String> {
        params.extend(self.common_params.clone());
        let query = net::encode_query(&params);
        let sign_function = if path.contains("reply") {
            "sign_reply"
        } else {
            "sign_datail"
        };
        let signature = sign(sign_function, &query, &self.user_agent)?;
        params.push(("a_bogus", signature));
        let response = self
            .client
            .get(format!("{BASE_URL}{path}"))
            .query(&params)
            .send()
            .map_err(err)?;
        let status = response.status();
        let text = response.text().map_err(err)?;
        if !status.is_success() {
            return Err(format!("评论请求失败: {status} {text}"));
        }
        if text.is_empty() {
            return Err("响应体为空,Cookie 可能已失效".to_owned());
        }
        let data: Value =
            serde_json::from_str(&text).map_err(|error| format!("评论响应不是 JSON: {error}"))?;
        if net::contains_verify_check(&data) {
            return Err("触发验证码,请完成验证后再继续".to_owned());
        }
        if data
            .get("status_code")
            .and_then(net::value_i64)
            .unwrap_or(0)
            != 0
        {
            return Err(format!("评论接口返回失败状态: {text}"));
        }
        Ok(data)
    }
}

fn parse_comment_count(value: &str) -> Result<usize, String> {
    let value = value
        .parse::<usize>()
        .map_err(|error| format!("无效页大小: {error}"))?;
    if !(1..=20).contains(&value) {
        return Err("每页请求数量必须在 1..=20 范围内".to_owned());
    }
    Ok(value)
}

fn parse_non_negative_f64(value: &str) -> Result<f64, String> {
    let value = value
        .parse::<f64>()
        .map_err(|error| format!("无效秒数: {error}"))?;
    if !value.is_finite() || value < 0.0 {
        return Err("秒数必须是有限的非负数".to_owned());
    }
    Ok(value)
}

pub fn extract_aweme_id(target: &str) -> Result<String, String> {
    let target = target.trim();
    if target.chars().all(|value| value.is_ascii_digit()) && !target.is_empty() {
        return Ok(target.to_owned());
    }
    let mut url = reqwest::Url::parse(target).map_err(|_| format!("无法识别作品 ID: {target}"))?;
    if url.host_str() == Some("v.douyin.com") {
        url = Client::builder()
            .timeout(Duration::from_secs(15))
            .build()
            .map_err(err)?
            .get(url)
            .send()
            .map_err(err)?
            .url()
            .clone();
    }
    let parts: Vec<_> = url
        .path_segments()
        .into_iter()
        .flatten()
        .filter(|value| !value.is_empty())
        .collect();
    for marker in ["video", "note"] {
        if let Some(index) = parts.iter().position(|value| *value == marker)
            && let Some(value) = parts
                .get(index + 1)
                .filter(|value| value.chars().all(|c| c.is_ascii_digit()))
        {
            return Ok((*value).to_owned());
        }
    }
    parts
        .last()
        .filter(|value| value.chars().all(|c| c.is_ascii_digit()))
        .map(|value| (*value).to_owned())
        .ok_or_else(|| format!("无法识别作品 ID: {target}"))
}

pub fn normalize_comment(comment: &Value) -> Value {
    let user = comment.get("user").and_then(Value::as_object);
    json!({
        "id": first_string(comment, &["cid", "comment_id"]),
        "text": first_string(comment, &["text"]),
        "create_time": comment.get("create_time").cloned().unwrap_or(Value::Null),
        "digg_count": comment.get("digg_count").cloned().unwrap_or_else(|| json!(0)),
        "reply_comment_total": comment.get("reply_comment_total").cloned().unwrap_or_else(|| json!(0)),
        "ip_label": first_string(comment, &["ip_label"]),
        "user": {
            "uid": object_string(user, "uid"), "sec_uid": object_string(user, "sec_uid"),
            "nickname": object_string(user, "nickname"), "unique_id": object_string(user, "unique_id")
        }
    })
}

fn format_chatml(data: &Value, args: &CommentArgs) -> Vec<Value> {
    let aweme_id = data.get("aweme_id").and_then(Value::as_str).unwrap_or("");
    let mut records = Vec::new();
    for comment in data
        .get("comments")
        .and_then(Value::as_array)
        .into_iter()
        .flatten()
    {
        let text = comment
            .get("text")
            .and_then(Value::as_str)
            .unwrap_or("")
            .trim();
        if text.is_empty() || digg(comment) < args.min_comment_digg {
            continue;
        }
        let replies = comment
            .get("replies")
            .and_then(Value::as_array)
            .cloned()
            .unwrap_or_default();
        if replies.is_empty() && args.include_single_comments {
            records.push(json!({
                "messages":[{"role":args.comment_role,"content":text}],
                "metadata": metadata(aweme_id, comment, None)
            }));
        } else {
            for reply in replies {
                let reply_text = reply
                    .get("text")
                    .and_then(Value::as_str)
                    .unwrap_or("")
                    .trim();
                if reply_text.is_empty() || digg(&reply) < args.min_reply_digg {
                    continue;
                }
                records.push(json!({
                    "messages":[{"role":args.comment_role,"content":text},{"role":args.reply_role,"content":reply_text}],
                    "metadata": metadata(aweme_id, comment, Some(&reply))
                }));
            }
        }
    }
    records
}

fn metadata(aweme_id: &str, comment: &Value, reply: Option<&Value>) -> Value {
    let mut result = Map::from_iter([
        (
            "source".to_owned(),
            json!(if reply.is_some() {
                "douyin_comment_reply"
            } else {
                "douyin_comment"
            }),
        ),
        ("aweme_id".to_owned(), json!(aweme_id)),
        (
            "comment_id".to_owned(),
            json!(first_string(comment, &["id"])),
        ),
        ("comment_digg_count".to_owned(), json!(digg(comment))),
        (
            "comment_create_time".to_owned(),
            comment.get("create_time").cloned().unwrap_or(Value::Null),
        ),
        (
            "comment_user".to_owned(),
            user_metadata(comment.get("user")),
        ),
        (
            "quality_score".to_owned(),
            json!(digg(comment) + reply.map_or(0, digg)),
        ),
    ]);
    if let Some(reply) = reply {
        result.extend([
            ("reply_id".to_owned(), json!(first_string(reply, &["id"]))),
            ("reply_digg_count".to_owned(), json!(digg(reply))),
            (
                "reply_create_time".to_owned(),
                reply.get("create_time").cloned().unwrap_or(Value::Null),
            ),
            ("reply_user".to_owned(), user_metadata(reply.get("user"))),
        ]);
    }
    Value::Object(result)
}

fn user_metadata(user: Option<&Value>) -> Value {
    let object = user.and_then(Value::as_object);
    json!({"uid":object_string(object,"uid"),"sec_uid":object_string(object,"sec_uid"),"nickname":object_string(object,"nickname"),"unique_id":object_string(object,"unique_id")})
}

fn first_string(value: &Value, keys: &[&str]) -> String {
    keys.iter()
        .find_map(|key| {
            value
                .get(key)
                .and_then(Value::as_str)
                .filter(|value| !value.is_empty())
        })
        .unwrap_or("")
        .to_owned()
}

fn object_string(object: Option<&Map<String, Value>>, key: &str) -> String {
    object
        .and_then(|value| value.get(key))
        .and_then(Value::as_str)
        .unwrap_or("")
        .to_owned()
}

fn digg(value: &Value) -> i64 {
    value
        .get("digg_count")
        .and_then(|value| value.as_i64().or_else(|| value.as_str()?.parse().ok()))
        .unwrap_or(0)
}

fn pause(has_more: bool, seconds: f64) {
    if has_more && seconds > 0.0 {
        thread::sleep(Duration::from_secs_f64(seconds));
    }
}

#[cfg(test)]
mod tests {
    use super::{
        CommentArgs, OutputFormat, extract_aweme_id, format_chatml, normalize_comment,
        parse_comment_count, parse_non_negative_f64,
    };
    use crate::test_support::must;
    use serde_json::json;

    #[test]
    fn validates_page_size_and_sleep_values() {
        assert_eq!(must(parse_comment_count("20")), 20);
        assert!(parse_comment_count("0").is_err());
        assert!(parse_comment_count("21").is_err());
        assert_eq!(must(parse_non_negative_f64("0.5")), 0.5);
        assert!(parse_non_negative_f64("NaN").is_err());
        assert!(parse_non_negative_f64("-1").is_err());
    }

    #[test]
    fn extracts_raw_and_url_aweme_ids() {
        assert_eq!(
            must(extract_aweme_id("7380000000000000000")),
            "7380000000000000000"
        );
        assert_eq!(
            must(extract_aweme_id(
                "https://www.douyin.com/video/7380000000000000000?x=1"
            )),
            "7380000000000000000"
        );
        assert_eq!(
            must(extract_aweme_id(
                "https://www.douyin.com/note/7380000000000000000"
            )),
            "7380000000000000000"
        );
    }

    #[test]
    fn normalizes_comment_fields() {
        let value = normalize_comment(&json!({
            "cid":"1","text":"你好","create_time":1_710_000_000,"digg_count":3,"reply_comment_total":2,"ip_label":"上海",
            "user":{"uid":"u1","sec_uid":"sec","nickname":"用户","unique_id":"unique"}
        }));
        assert_eq!(value["id"], "1");
        assert_eq!(value["user"]["nickname"], "用户");
        assert_eq!(value["digg_count"], 3);
    }

    #[test]
    fn chatml_pairs_comments_and_replies() {
        let args = CommentArgs {
            target: String::new(),
            limit: 100,
            count: 20,
            with_replies: true,
            reply_limit: 20,
            sleep_seconds: 0.0,
            output: None,
            output_format: OutputFormat::ChatmlJsonl,
            comment_role: "user".to_owned(),
            reply_role: "assistant".to_owned(),
            min_comment_digg: 0,
            min_reply_digg: 0,
            include_single_comments: false,
            cookie: None,
        };
        let records = format_chatml(
            &json!({
                "aweme_id":"7380000000000000000",
                "comments":[{"id":"c1","text":"这车能买吗?","digg_count":8,"user":{},"replies":[
                    {"id":"r1","text":"先查维保和事故。","digg_count":12,"user":{}}
                ]}]
            }),
            &args,
        );
        assert_eq!(records[0]["messages"][0]["role"], "user");
        assert_eq!(records[0]["messages"][1]["content"], "先查维保和事故。");
        assert_eq!(records[0]["metadata"]["quality_score"], 20);
    }
}