probe-douyin-share.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env node
  2. /** 验证 TikHub 分享链接解析 + 视频评论 + 用户主页 endpoint */
  3. const fs = require('fs');
  4. const path = require('path');
  5. const os = require('os');
  6. const https = require('https');
  7. const TOKEN = JSON.parse(fs.readFileSync(
  8. path.join(os.homedir(), '.openclaw', 'skills', 'xiaohongshu-search-notes', 'api-config.json'),
  9. 'utf-8'
  10. )).endpoint.headers.Authorization.replace(/^Bearer\s+/, '');
  11. function req(hostname, pathUrl) {
  12. return new Promise((resolve) => {
  13. const r = https.request({
  14. hostname, path: pathUrl, method: 'GET',
  15. headers: { Authorization: `Bearer ${TOKEN}`, Accept: 'application/json' },
  16. }, (res) => {
  17. const chunks = [];
  18. res.on('data', (c) => chunks.push(c));
  19. res.on('end', () => resolve({ status: res.statusCode, body: Buffer.concat(chunks).toString('utf-8') }));
  20. });
  21. r.on('error', (e) => resolve({ status: 0, body: e.message }));
  22. r.setTimeout(30000, () => { r.destroy(); resolve({ status: 0, body: 'timeout' }); });
  23. r.end();
  24. });
  25. }
  26. const SHARE_LINKS = [
  27. { id: 'liver', url: 'https://v.douyin.com/BMklXPTyiUM/' },
  28. { id: 'probiotic', url: 'https://v.douyin.com/1eV76OP0FNM/' },
  29. { id: 'monkey', url: 'https://v.douyin.com/C3mMbhRYKHw/' },
  30. ];
  31. (async () => {
  32. for (const item of SHARE_LINKS) {
  33. console.log(`\n=== ${item.id} ===`);
  34. console.log(`share_url = ${item.url}`);
  35. // 1) share url → video
  36. const r1 = await req('api.tikhub.io',
  37. `/api/v1/douyin/app/v3/fetch_one_video_by_share_url?share_url=${encodeURIComponent(item.url)}`);
  38. console.log(`[1/resolve] status=${r1.status}`);
  39. let aweme_id, sec_uid;
  40. try {
  41. const j = JSON.parse(r1.body);
  42. if (j.code === 200 && j.data) {
  43. const info = j.data.aweme_detail || j.data.aweme_info || j.data;
  44. aweme_id = info.aweme_id;
  45. sec_uid = info.author?.sec_uid;
  46. console.log(` aweme_id = ${aweme_id}`);
  47. console.log(` author = ${info.author?.nickname}`);
  48. console.log(` desc = ${String(info.desc || '').slice(0, 80).replace(/\s+/g, ' ')}`);
  49. console.log(` stats = digg:${info.statistics?.digg_count} comment:${info.statistics?.comment_count} share:${info.statistics?.share_count} play:${info.statistics?.play_count}`);
  50. } else {
  51. console.log(` ERR code=${j.code} msg=${j.message || j.message_zh}`);
  52. console.log(` body: ${r1.body.slice(0, 300)}`);
  53. }
  54. } catch (e) {
  55. console.log(` PARSE ERR: ${e.message}`);
  56. console.log(` body: ${r1.body.slice(0, 300)}`);
  57. }
  58. if (!aweme_id) continue;
  59. await new Promise((r) => setTimeout(r, 800));
  60. // 2) video comments
  61. const r2 = await req('api.tikhub.io',
  62. `/api/v1/douyin/app/v3/fetch_video_comments?aweme_id=${aweme_id}&cursor=0&count=20`);
  63. console.log(`[2/comments] status=${r2.status}`);
  64. try {
  65. const j = JSON.parse(r2.body);
  66. if (j.code === 200) {
  67. const cmts = j.data?.comments || [];
  68. console.log(` 评论数 = ${cmts.length}, total=${j.data?.total}, has_more=${j.data?.has_more}`);
  69. if (cmts[0]) {
  70. console.log(` [0] ${cmts[0].user?.nickname}: ${String(cmts[0].text || '').slice(0, 60)} (digg=${cmts[0].digg_count})`);
  71. }
  72. if (cmts[1]) console.log(` [1] ${cmts[1].user?.nickname}: ${String(cmts[1].text || '').slice(0, 60)}`);
  73. } else {
  74. console.log(` ERR code=${j.code} msg=${j.message_zh || j.message}`);
  75. }
  76. } catch (e) {
  77. console.log(` PARSE ERR: ${e.message}`);
  78. }
  79. if (!sec_uid) continue;
  80. await new Promise((r) => setTimeout(r, 800));
  81. // 3) user post videos
  82. const r3 = await req('api.tikhub.io',
  83. `/api/v1/douyin/app/v3/fetch_user_post_videos?sec_user_id=${encodeURIComponent(sec_uid)}&max_cursor=0&count=10`);
  84. console.log(`[3/user_posts] status=${r3.status}`);
  85. try {
  86. const j = JSON.parse(r3.body);
  87. if (j.code === 200) {
  88. const vids = j.data?.aweme_list || [];
  89. console.log(` 作者作品 = ${vids.length}, has_more=${j.data?.has_more}`);
  90. vids.slice(0, 3).forEach((v, i) => {
  91. console.log(` [${i}] digg:${v.statistics?.digg_count} comment:${v.statistics?.comment_count} | ${String(v.desc || '').slice(0, 50)}`);
  92. });
  93. } else {
  94. console.log(` ERR code=${j.code} msg=${j.message_zh || j.message}`);
  95. }
  96. } catch (e) {
  97. console.log(` PARSE ERR: ${e.message}`);
  98. }
  99. await new Promise((r) => setTimeout(r, 800));
  100. // 4) user profile
  101. const r4 = await req('api.tikhub.io',
  102. `/api/v1/douyin/app/v3/handler_user_profile?sec_user_id=${encodeURIComponent(sec_uid)}`);
  103. console.log(`[4/user_profile] status=${r4.status}`);
  104. try {
  105. const j = JSON.parse(r4.body);
  106. if (j.code === 200) {
  107. const u = j.data?.user || j.data;
  108. console.log(` nickname = ${u?.nickname}, 粉丝 = ${u?.follower_count}, 作品 = ${u?.aweme_count}, 签名 = ${String(u?.signature || '').slice(0, 50)}`);
  109. } else {
  110. console.log(` ERR code=${j.code} msg=${j.message_zh || j.message}`);
  111. }
  112. } catch (e) {
  113. console.log(` PARSE ERR: ${e.message}`);
  114. }
  115. }
  116. })();