uta 0.1.2

Command-line music search and downloader for QQ Music and NetEase Cloud Music, lossless first, shipped as a single static binary. For learning and research only; non-commercial use.
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
//! 网易云取下载直链:无 key 的第三方(tmetu → chksz → jfjt)优先,官方 eapi `player/url/v1` 兜底。
//!
//! 实测 2026-10-09:官方接口免登录时,付费歌(fee=1)返回 code=-110 无链接,
//! 免费歌(fee=0/8)最高给 exhigh(MP3 320k)。第三方对付费歌也能给出无损及以上,
//! 链接都是网易官方 CDN(m*.music.126.net),支持 Range/ETag:
//! - tmetu:全部档位(含 jymaster);上游账号被风控时返回 code=500"请完成验证操作",本次运行熔断。
//! - chksz:全部档位;该档不可用时 HTTP 404,降档重试。
//! - jfjt:最快(~0.4s),自动降到可用档位(实测 jymaster → jyeffect)。

use rand::RngExt;
use serde_json::{Value, json};
use tracing::debug;

use super::Session;
use super::crypto::eapi_params;
use crate::model::{
    Level, ProviderCheck, ResolveOptions, Resolved, Song, below_320k, check_provider,
};
use crate::probe::{ProbeResult, probe};

const PLAYER_URL: &str = "https://interface3.music.163.com/eapi/song/enhance/player/url/v1";

/// 网易品质档:(接口 level, 说明, 档位)。
const LEVELS: &[(&str, &str, Level)] = &[
    ("jymaster", "超清母带", Level::Master),
    ("sky", "沉浸环绕声", Level::Atmos),
    ("dolby", "杜比全景声", Level::Atmos),
    ("jyeffect", "高清臻音", Level::Hires),
    ("hires", "Hi-Res", Level::Hires),
    ("lossless", "无损", Level::Sq),
    ("exhigh", "极高 320k", Level::Hq),
    ("higher", "较高 192k", Level::Std),
    ("standard", "标准", Level::Std),
];

/// 第三方依次尝试的档位(从高到低)。不请求 dolby:实测是 mp4 封装的杜比音轨,通用播放器支持差。
const THIRD_PARTY_TRY: &[&str] = &[
    "jymaster", "sky", "jyeffect", "hires", "lossless", "exhigh", "standard",
];

/// 网易的无 key 第三方接口。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Provider {
    Tmetu,
    Chksz,
    Jfjt,
}

impl Provider {
    pub const ALL: [Provider; 3] = [Provider::Tmetu, Provider::Chksz, Provider::Jfjt];

    pub fn name(self) -> &'static str {
        match self {
            Provider::Tmetu => "tmetu",
            Provider::Chksz => "chksz",
            Provider::Jfjt => "jfjt",
        }
    }

    fn base(self, session: &Session) -> &str {
        let c = &session.config;
        match self {
            Provider::Tmetu => &c.tmetu.url,
            Provider::Chksz => &c.chksz.url,
            Provider::Jfjt => &c.jfjt.url,
        }
        .trim()
    }

    fn request(
        self,
        session: &Session,
        base: &str,
        id: &str,
        level: &str,
    ) -> reqwest::RequestBuilder {
        let c = &session.client;
        match self {
            Provider::Tmetu => c
                .get(base)
                .header("Referer", "https://music.tmetu.cn/")
                .query(&[
                    ("miss", "songAll"),
                    ("id", id),
                    ("level", level),
                    ("withLyric", "false"),
                ]),
            Provider::Chksz => c
                .get(base)
                .header("Referer", "https://cp.chksz.top/")
                .header("Origin", "https://cp.chksz.top")
                .query(&[("id", id), ("level", level)]),
            Provider::Jfjt => c
                .post(base)
                .header("Referer", "https://dm.jfjt.cc/")
                .form(&[("url", id), ("level", level), ("type", "json")]),
        }
    }
}

/// 第三方单次请求的结果。
#[derive(Debug, PartialEq, Eq)]
pub enum Reply {
    /// (直链, 实际档位)
    Got(String, String),
    /// 这一档没有,可以降档再试
    Lower,
    /// 歌曲不存在或接口不可用,不再请求该接口
    Stop,
    /// 接口上游被风控,本次运行不再使用
    Broken,
}

/// 解析第三方响应(HTTP 状态码 + JSON)。
pub fn parse_reply(p: Provider, status: u16, v: &Value) -> Reply {
    let s = |ptr: &str| {
        v.pointer(ptr)
            .and_then(Value::as_str)
            .filter(|x| !x.is_empty())
    };
    let code = |k: &str| v.get(k).and_then(Value::as_i64);
    match p {
        Provider::Tmetu => {
            if code("code") == Some(200) {
                return match s("/data/audioUrl") {
                    Some(u) => Reply::Got(u.into(), s("/data/level").unwrap_or_default().into()),
                    None => Reply::Lower,
                };
            }
            // 实测:{"code":500,"message":"获取歌曲详情失败: 请完成验证操作","extra":{"needVerify":true}}
            let verify = v.pointer("/extra/needVerify").and_then(Value::as_bool) == Some(true)
                || s("/message").is_some_and(|m| m.contains("验证"));
            if verify { Reply::Broken } else { Reply::Stop }
        }
        Provider::Chksz => match (status, s("/data/url")) {
            (200, Some(u)) => Reply::Got(u.into(), s("/data/level").unwrap_or_default().into()),
            // 实测该档不可用:HTTP 404 "song may be unavailable at this quality level"
            (404, _) => Reply::Lower,
            _ => Reply::Stop,
        },
        Provider::Jfjt => match (code("status"), s("/data/url")) {
            (Some(200), Some(u)) => {
                Reply::Got(u.into(), s("/data/level").unwrap_or_default().into())
            }
            // 实测不存在的歌:{"status":404,"success":false,"message":"未找到歌曲信息"}
            _ => Reply::Stop,
        },
    }
}

/// 第三方拿到这些格式时视为足够,不再走官方接口。
const SUFFICIENT_EXTS: &[&str] = &["flac"];

fn level_info(level: &str) -> Option<(&'static str, Level)> {
    LEVELS
        .iter()
        .find(|(l, ..)| *l == level)
        .map(|&(_, label, lv)| (label, lv))
}

fn resolved(via: &'static str, label: &str, level: Level, probe: ProbeResult) -> Resolved {
    Resolved {
        via,
        quality: label.to_string(),
        level,
        probe,
        // 实测网易 CDN 不校验 Referer
        referer: None,
        fallback: None,
    }
}

async fn accept(
    session: &Session,
    song: &Song,
    url: &str,
    third_party: bool,
) -> Option<ProbeResult> {
    if !url.starts_with("http") {
        return None;
    }
    let p = probe(&session.client, url).await?;
    if third_party && below_320k(p.size, song.interval) {
        debug!(id = %song.id, size = p.size, "低于 320kbps,丢弃");
        return None;
    }
    Some(p)
}

async fn via_provider(
    session: &Session,
    p: Provider,
    song: &Song,
    max_level: Level,
) -> Option<Resolved> {
    let base = p.base(session);
    let mut tried_got = std::collections::HashSet::new();
    for &want in THIRD_PARTY_TRY
        .iter()
        .filter(|l| level_info(l).is_some_and(|(_, lv)| lv <= max_level))
    {
        if session.is_broken(p) {
            return None;
        }
        let (status, v) = match crate::net::send(p.request(session, base, &song.id, want)).await {
            Ok(r) => {
                let status = r.status().as_u16();
                match r.json::<Value>().await {
                    Ok(v) => (status, v),
                    Err(e) => {
                        debug!(id = %song.id, via = p.name(), "响应不是 JSON: {e}");
                        return None;
                    }
                }
            }
            Err(e) => {
                debug!(id = %song.id, via = p.name(), "请求失败: {e}");
                return None;
            }
        };
        let (url, got) = match parse_reply(p, status, &v) {
            Reply::Got(u, g) => (u, if g.is_empty() { want.to_string() } else { g }),
            Reply::Lower => {
                debug!(id = %song.id, via = p.name(), want, "该档无链接,降档");
                continue;
            }
            Reply::Stop => {
                debug!(id = %song.id, via = p.name(), %status, "无此歌曲或接口不可用");
                return None;
            }
            Reply::Broken => {
                session.mark_broken(p);
                return None;
            }
        };
        // 自动降档的接口会对多个请求档返回同一档,不重复验证
        if !tried_got.insert(got.clone()) {
            continue;
        }
        let Some((label, level)) = level_info(&got).filter(|(_, lv)| *lv <= max_level) else {
            debug!(id = %song.id, via = p.name(), got, "返回的档位不可用");
            continue;
        };
        if let Some(pr) = accept(session, song, &url, true).await {
            return Some(resolved(p.name(), label, level, pr));
        }
    }
    None
}

/// 第三方:tmetu → chksz → jfjt。拿到无损及以上即停;否则继续问下一个,取档位最高的。
async fn via_third_party(session: &Session, song: &Song, max_level: Level) -> Option<Resolved> {
    let mut best: Option<Resolved> = None;
    for p in Provider::ALL {
        if best
            .as_ref()
            .is_some_and(|b| b.level >= Level::Sq.min(max_level))
        {
            break;
        }
        if p.base(session).is_empty() {
            continue;
        }
        if let Some(r) = via_provider(session, p, song, max_level).await
            && best.as_ref().is_none_or(|b| r.level > b.level)
        {
            best = Some(r);
        }
    }
    best
}

/// 官方 eapi 请求的 level:免登录最高只给 exhigh,但仍按上限请求,由服务端降档。
fn official_level(max_level: Level) -> &'static str {
    match max_level {
        Level::Master | Level::Atmos | Level::Hires => "hires",
        Level::Sq => "lossless",
        Level::Hq => "exhigh",
        Level::Std => "standard",
    }
}

/// eapi 请求体 JSON(`header` 字段本身是 JSON 字符串,与 Python 版一致)。
pub fn player_payload(id: u64, level: &str, request_id: u32) -> String {
    let header = json!({"os": "pc", "appver": "", "osver": "", "deviceId": "pyncm!", "requestId": request_id.to_string()});
    json!({"ids": [id], "level": level, "encodeType": "flac", "header": header.to_string()})
        .to_string()
}

async fn via_official(session: &Session, song: &Song, max_level: Level) -> Option<Resolved> {
    let id: u64 = song.id.parse().ok()?;
    let request_id = rand::rng().random_range(20_000_000..30_000_000);
    let payload = player_payload(id, official_level(max_level), request_id);
    let v: Value = match crate::net::send(
        session
            .client
            .post(PLAYER_URL)
            .header("Cookie", "os=pc; appver=; osver=; deviceId=pyncm!")
            .form(&[("params", eapi_params(PLAYER_URL, &payload))]),
    )
    .await
    {
        Ok(r) => r.json().await.ok()?,
        Err(e) => {
            debug!(id = %song.id, "网易官方取链失败: {e}");
            return None;
        }
    };
    let d = v.pointer("/data/0")?;
    // 付费歌免登录:code=-110、url 为空;有 freeTrialInfo 的是试听片段,不能用
    if !d.get("freeTrialInfo").is_none_or(Value::is_null) {
        debug!(id = %song.id, "官方只给试听片段,丢弃");
        return None;
    }
    let url = d
        .get("url")
        .and_then(Value::as_str)
        .filter(|u| !u.is_empty())?;
    let got = d.get("level").and_then(Value::as_str).unwrap_or("standard");
    let (label, level) = level_info(got)?;
    if level > max_level {
        return None;
    }
    let p = accept(session, song, url, false).await?;
    Some(resolved("官方", label, level, p))
}

/// 逐个检查取链接口(`uta doctor`):第三方用付费歌 `paid`,官方用免费歌 `free`(免登录拿不到付费歌)。
pub async fn check(session: &Session, paid: &Song, free: &Song) -> Vec<ProviderCheck> {
    let max = Level::Master;
    let third = futures::future::join_all(Provider::ALL.map(|p| {
        check_provider(
            p.name(),
            !p.base(session).is_empty(),
            via_provider(session, p, paid, max),
        )
    }));
    let (third, official) = tokio::join!(
        third,
        check_provider("官方", true, via_official(session, free, max))
    );
    let mut out: Vec<ProviderCheck> = third;
    for (c, p) in out.iter_mut().zip(Provider::ALL) {
        if c.result.is_none() && session.is_broken(p) {
            c.note = Some("上游需要验证(被风控)".into());
        }
    }
    out.push(official);
    out
}

/// 解析一首歌的下载直链。`lossless_only` 时第三方拿不到无损就直接放弃。
pub async fn resolve(session: &Session, song: &Song, opts: ResolveOptions) -> Option<Resolved> {
    let l1 = via_third_party(session, song, opts.max_level).await;
    if opts.lossless_only {
        return l1.filter(|r| r.probe.is_lossless());
    }
    if let Some(r) = &l1
        && SUFFICIENT_EXTS.contains(&r.probe.ext.as_str())
    {
        return l1;
    }
    let official = via_official(session, song, opts.max_level).await;
    match (l1, official) {
        (Some(a), Some(b)) => Some(if a.probe.size > b.probe.size { a } else { b }),
        (a, b) => a.or(b),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn levels() {
        assert_eq!(level_info("jymaster"), Some(("超清母带", Level::Master)));
        assert_eq!(level_info("exhigh").unwrap().1, Level::Hq);
        assert_eq!(level_info("nope"), None);
        // 尝试顺序必须按档位从高到低
        let lv: Vec<Level> = THIRD_PARTY_TRY
            .iter()
            .map(|l| level_info(l).unwrap().1)
            .collect();
        assert!(lv.windows(2).all(|w| w[0] >= w[1]));
        assert_eq!(official_level(Level::Master), "hires");
        assert_eq!(official_level(Level::Hq), "exhigh");
    }

    #[test]
    fn replies() {
        use Provider::*;
        // 均为实测响应(2026-10-09,节选)
        let got = |u: &str, l: &str| Reply::Got(u.into(), l.into());
        let tm_ok = json!({"code": 200, "data": {"audioUrl": "https://m8.music.126.net/a.flac", "level": "jymaster"}});
        assert_eq!(
            parse_reply(Tmetu, 200, &tm_ok),
            got("https://m8.music.126.net/a.flac", "jymaster")
        );
        let tm_404 =
            json!({"status": "error", "code": 404, "message": "未找到歌曲信息", "data": null});
        assert_eq!(parse_reply(Tmetu, 200, &tm_404), Reply::Stop);
        let tm_verify = json!({"status": "error", "code": 500, "message": "获取歌曲详情失败: 请完成验证操作",
            "data": null, "extra": {"source": "netease", "needVerify": true}});
        assert_eq!(parse_reply(Tmetu, 200, &tm_verify), Reply::Broken);
        let ck_ok = json!({"code": 200, "msg": "success", "data": {"url": "https://m7.music.126.net/b", "level": "jymaster", "size": "170686944"}});
        assert_eq!(
            parse_reply(Chksz, 200, &ck_ok),
            got("https://m7.music.126.net/b", "jymaster")
        );
        let ck_404 = json!({"code": 404, "msg": "Music URL not found, song may be unavailable at this quality level"});
        assert_eq!(parse_reply(Chksz, 404, &ck_404), Reply::Lower);
        assert_eq!(parse_reply(Chksz, 500, &json!({})), Reply::Stop);
        let jf_ok = json!({"status": 200, "success": true, "data": {"url": "http://m7.music.126.net/c", "level": "jyeffect", "size": "95.25MB"}});
        assert_eq!(
            parse_reply(Jfjt, 200, &jf_ok),
            got("http://m7.music.126.net/c", "jyeffect")
        );
        let jf_404 = json!({"status": 404, "success": false, "message": "未找到歌曲信息"});
        assert_eq!(parse_reply(Jfjt, 200, &jf_404), Reply::Stop);
    }

    #[test]
    fn payload_shape() {
        let p: Value =
            serde_json::from_str(&player_payload(1481929839, "lossless", 23456789)).unwrap();
        assert_eq!(p["ids"], json!([1481929839u64]));
        assert_eq!(p["level"], "lossless");
        let h: Value = serde_json::from_str(p["header"].as_str().unwrap()).unwrap();
        assert_eq!(
            (
                h["os"].as_str(),
                h["deviceId"].as_str(),
                h["requestId"].as_str()
            ),
            (Some("pc"), Some("pyncm!"), Some("23456789"))
        );
    }
}

#[cfg(test)]
mod net_tests {
    use super::*;
    use crate::config::{EndpointConfig, NeteaseConfig};
    use std::time::Duration;

    fn session(tmetu: &str) -> Session {
        let client = reqwest::Client::builder()
            .connect_timeout(Duration::from_secs(5))
            .timeout(Duration::from_secs(15))
            .build()
            .unwrap();
        Session::new(
            client,
            NeteaseConfig {
                tmetu: EndpointConfig { url: tmetu.into() },
                ..NeteaseConfig::default()
            },
        )
    }

    fn song(id: &str, interval: u64) -> Song {
        Song {
            source: crate::model::SourceKind::Netease,
            id: id.into(),
            interval,
            ..Song::default()
        }
    }

    #[tokio::test]
    #[ignore = "需要网络"]
    async fn official_free_song_320k() {
        // 《刻在我心底的名字 (Live)》fee=0:免登录给 exhigh
        let opts = ResolveOptions {
            lossless_only: false,
            max_level: Level::Master,
        };
        let r = via_official(&session(""), &song("1498983530", 314), opts.max_level)
            .await
            .unwrap();
        assert_eq!(
            (r.via, r.level, r.probe.ext.as_str()),
            ("官方", Level::Hq, "mp3")
        );
        // 付费歌《无人知晓》fee=1:免登录拿不到
        assert!(
            via_official(&session(""), &song("1481929839", 288), opts.max_level)
                .await
                .is_none()
        );
    }
}