Files

83 lines
3.9 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* probe_track_all.c — 真机抓 /api/playlist/track/all 原始响应 + 用现有解析器解析。
* 目的:确认「Load Failed」根因(eapi 路由 / 响应字段 / code 非 200 / 数组路径)。
* 请求量:1(track/all for 一个小歌单)。
* 用法: probe_track_all <data_dir> <cacert> <playlist_id> <out_path> [path] [crypto]
* data_dir 默认 /usr/data/mnt/sd_0/MeowData/dev.player.ncm/data
* cacert 默认 /usr/data/mnt/sd_0/MeowAPPs/dev.player.ncm/cacert.pem
* playlist_id 默认 18206253175(Docu/1 首)
* out_path 默认 /tmp/track_all_raw.json
* path 默认 /api/playlist/track/all(eapi 已证 404,可改 /api/v6/playlist/track/all)
* crypto 0=eapi(默认) 1=weapi
*/
#include "ncm.h"
#include "ncm_api.h"
#include "ncm_common.h"
#include "ncm_http.h"
#include "ncm_req.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static ncm_ctx_t ncm_ctx;
int main(int argc, char **argv)
{
const char *data_dir = argc > 1 ? argv[1] : "/usr/data/mnt/sd_0/MeowData/dev.player.ncm/data";
const char *cacert = argc > 2 ? argv[2] : "/usr/data/mnt/sd_0/MeowAPPs/dev.player.ncm/cacert.pem";
const char *pid = argc > 3 ? argv[3] : "18206253175";
const char *out_path = argc > 4 ? argv[4] : "/tmp/track_all_raw.json";
const char *path = argc > 5 ? argv[5] : "/api/playlist/track/all";
int crypto_kind = (argc > 6 && atoi(argv[6]) == 1) ? NCM_CRYPT_WEAPI : NCM_CRYPT_EAPI;
ncm_ctx_init(&ncm_ctx, data_dir);
printf("device_id=%s cookie_len=%zu\n", ncm_ctx.device_id, strlen(ncm_ctx.cookie));
ncm_http_set_verify_default(1, cacert);
/* 直接构造 track/all 请求,抓原始 body(不经过解析,避免解析失败掩盖真相)。 */
printf("testing path=%s crypto=%s\n", path, crypto_kind == NCM_CRYPT_EAPI ? "eapi" : "weapi");
char json[256];
snprintf(json, sizeof json, "{\"id\":\"%s\",\"offset\":0,\"limit\":50,\"n\":50,\"s\":8}", pid);
char body[NCM_REQ_BODY_MAX], hdrs[NCM_REQ_HDRS_MAX], url[NCM_REQ_URL_MAX];
int rc = ncm_req_build(ncm_ctx.device_id, ncm_ctx.cookie, path, json,
crypto_kind, body, sizeof body, hdrs, sizeof hdrs, url, sizeof url);
printf("req_build rc=%d url=%s\n", rc, url);
if (rc != NCM_OK) return 1;
ncm_http_req_t req;
memset(&req, 0, sizeof req);
req.url = url; req.method = "POST"; req.body = body; req.body_len = strlen(body);
req.extra_hdrs = hdrs; req.verify_ca = NCM_HTTP_VERIFY_DEFAULT; req.timeout_ms = 15000;
char rbuf[2 * 1024 * 1024];
ncm_http_resp_t resp;
memset(&resp, 0, sizeof resp);
long t0 = (long)ncm_now_ms();
rc = ncm_http_request(&req, rbuf, sizeof rbuf, &resp);
long dt = (long)ncm_now_ms() - t0;
printf("http rc=%d status=%d content_length=%zu body_len=%zu elapsed_ms=%ld\n",
rc, resp.status, resp.content_length, resp.body_len, dt);
if (rc != NCM_OK) return 1;
/* 原样落盘(截断到 body_len) */
FILE *fp = fopen(out_path, "wb");
if (fp) { fwrite(resp.body, 1, resp.body_len, fp); fclose(fp); printf("raw saved to %s (%zu bytes)\n", out_path, resp.body_len); }
else printf("cannot write %s\n", out_path);
/* 打印前 1500 字节便于目检 */
size_t show = resp.body_len < 1500 ? resp.body_len : 1500;
printf("--- first %zu bytes ---\n", show);
fwrite(resp.body, 1, show, stdout);
printf("\n--- end ---\n");
/* 用现有解析器(离线重放,无新请求) */
if (resp.body_len > 0) {
ncm_song_t songs[50]; int out_n = 0; int64_t total = 0;
rc = ncm_api_parse_track_all(resp.body, resp.body_len, songs, 50, &out_n, &total);
printf("parse rc=%d out_n=%d total=%lld\n", rc, out_n, (long long)total);
if (rc == NCM_OK && out_n > 0)
printf(" [0] %s / %s / %s\n", songs[0].name, songs[0].art_name, songs[0].album_name);
}
printf("PROBE DONE\n");
return 0;
}