tt-help-cli-ycl 1.3.91 → 1.3.92

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tt-help-cli-ycl",
3
- "version": "1.3.91",
3
+ "version": "1.3.92",
4
4
  "description": "TikTok user & video data scraper - extract ttSeller, verified, locationCreated from HTML source",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli/tag.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { writeFileSync } from "fs";
2
+ import { randomUUID } from "crypto";
2
3
  import { fetchTagData, enrichVideosWithLocation } from "../lib/tag-fetcher.js";
3
4
  import { TikTokScraper } from "../lib/tiktok-scraper.mjs";
4
5
  import {
@@ -11,7 +12,22 @@ import { server as cfgServer } from "../lib/constants.js";
11
12
  const ALL_COUNTRIES = DEFAULT_TARGET_LOCATIONS;
12
13
  const DEFAULT_SERVER = cfgServer || "http://127.0.0.1:3000";
13
14
 
14
- async function pushToServer(serverUrl, filteredAuthors, videos) {
15
+ // 构建带客户端追踪 header fetch 封装
16
+ function buildClientHeaders(clientId, meta, extra = {}) {
17
+ return {
18
+ "X-Client-Id": clientId,
19
+ "X-Client-Info": JSON.stringify(meta),
20
+ ...extra,
21
+ };
22
+ }
23
+
24
+ async function pushToServer(
25
+ serverUrl,
26
+ filteredAuthors,
27
+ videos,
28
+ clientId,
29
+ meta,
30
+ ) {
15
31
  const users = filteredAuthors.map((author) => {
16
32
  const video = videos.find((v) => v.authorUniqueId === author);
17
33
  return {
@@ -23,7 +39,9 @@ async function pushToServer(serverUrl, filteredAuthors, videos) {
23
39
 
24
40
  const res = await fetch(`${serverUrl}/api/raw-users`, {
25
41
  method: "POST",
26
- headers: { "Content-Type": "application/json" },
42
+ headers: buildClientHeaders(clientId, meta, {
43
+ "Content-Type": "application/json",
44
+ }),
27
45
  body: JSON.stringify({ users }),
28
46
  });
29
47
  const data = await res.json();
@@ -412,11 +430,13 @@ export async function handleScore(parsed) {
412
430
  console.log(JSON.stringify(result, null, 2));
413
431
  }
414
432
 
415
- async function reportToServer(baseUrl, result) {
433
+ async function reportToServer(baseUrl, result, clientId, meta) {
416
434
  try {
417
435
  const res = await fetch(`${baseUrl}/api/tags/score-result`, {
418
436
  method: "POST",
419
- headers: { "Content-Type": "application/json" },
437
+ headers: buildClientHeaders(clientId, meta, {
438
+ "Content-Type": "application/json",
439
+ }),
420
440
  body: JSON.stringify(result),
421
441
  });
422
442
  const data = await res.json();
@@ -464,17 +484,24 @@ export async function handleScoreAll(parsed) {
464
484
  let emptyRounds = 0; // 连续无任务的轮数
465
485
  const DISCOVER_AFTER_EMPTY = 3; // 连续 3 轮无任务时触发 discover
466
486
 
487
+ // 生成客户端 ID,用于服务端追踪
488
+ const clientId = randomUUID();
489
+ const clientMeta = { type: "scoring" };
490
+
467
491
  // 复用 TikTokScraper 实例,避免每次 enrich 都启动/关闭 headless 浏览器
468
492
  const enrichScraper = new TikTokScraper({ poolSize: 3 });
469
493
  await enrichScraper.init();
470
494
  log("✅ TikTokScraper 已就绪 (enrich 复用)");
495
+ log(` 客户端 ID: ${clientId.substring(0, 8)}...`);
471
496
  log("");
472
497
 
473
498
  try {
474
499
  while (true) {
475
500
  try {
476
501
  // 从服务端取下一个 new 标签
477
- const tagsRes = await fetch(`${baseUrl}/api/tags?status=new&limit=1`);
502
+ const tagsRes = await fetch(`${baseUrl}/api/tags?status=new&limit=1`, {
503
+ headers: buildClientHeaders(clientId, clientMeta),
504
+ });
478
505
  const tagsData = await tagsRes.json();
479
506
  if (!tagsData.tags || tagsData.tags.length === 0) {
480
507
  emptyRounds++;
@@ -488,6 +515,7 @@ export async function handleScoreAll(parsed) {
488
515
  try {
489
516
  const discRes = await fetch(
490
517
  `${baseUrl}/api/tags/discover?country=${country}&count=5`,
518
+ { headers: buildClientHeaders(clientId, clientMeta) },
491
519
  );
492
520
  const discData = await discRes.json();
493
521
  if (discData.inserted) {
@@ -530,10 +558,12 @@ export async function handleScoreAll(parsed) {
530
558
  error: null,
531
559
  };
532
560
 
533
- // 锁定 tag
561
+ // 锁定 tag(meta 中不放入 tag,避免非 ASCII 字符导致 header ByteString 报错)
534
562
  const claimRes = await fetch(`${baseUrl}/api/tags/claim`, {
535
563
  method: "POST",
536
- headers: { "Content-Type": "application/json" },
564
+ headers: buildClientHeaders(clientId, clientMeta, {
565
+ "Content-Type": "application/json",
566
+ }),
537
567
  body: JSON.stringify({ tag }),
538
568
  });
539
569
  const claimData = await claimRes.json();
@@ -546,7 +576,7 @@ export async function handleScoreAll(parsed) {
546
576
  log(` ⚠️ 无法锁定 (${claimData.error}),标记为 dead 并跳过`);
547
577
  result.error = claimData.error;
548
578
  result.status = "dead";
549
- await reportToServer(baseUrl, result);
579
+ await reportToServer(baseUrl, result, clientId, clientMeta);
550
580
  totalScored++;
551
581
  continue;
552
582
  }
@@ -570,7 +600,7 @@ export async function handleScoreAll(parsed) {
570
600
  log(" ⚠️ 无视频,标记 dead");
571
601
  result.status = "dead";
572
602
  result.error = "no videos found";
573
- await reportToServer(baseUrl, result);
603
+ await reportToServer(baseUrl, result, clientId, clientMeta);
574
604
  totalScored++;
575
605
  continue;
576
606
  }
@@ -589,6 +619,9 @@ export async function handleScoreAll(parsed) {
589
619
  });
590
620
  videos = enriched.videos;
591
621
 
622
+ // 更新 meta 中当前正在处理的标签
623
+ clientMeta.tag = tag;
624
+
592
625
  // 过滤 + 算分 (共用函数)
593
626
  const { matchedAuthorSet } = applyFilterAndScore(
594
627
  videos,
@@ -602,12 +635,14 @@ export async function handleScoreAll(parsed) {
602
635
  baseUrl,
603
636
  [...matchedAuthorSet],
604
637
  videos,
638
+ clientId,
639
+ clientMeta,
605
640
  );
606
641
  result.pushedUsers = pushResult.added || 0;
607
642
  }
608
643
 
609
644
  // 上报结果
610
- await reportToServer(baseUrl, result);
645
+ await reportToServer(baseUrl, result, clientId, clientMeta);
611
646
 
612
647
  totalScored++;
613
648
  const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
@@ -643,12 +678,17 @@ export async function handleScoreAll(parsed) {
643
678
  }
644
679
  log(` ❌ 失败: ${e.message}`);
645
680
  try {
646
- await reportToServer(baseUrl, {
647
- tag: "",
648
- status: "error",
649
- score: 0,
650
- error: e.message,
651
- });
681
+ await reportToServer(
682
+ baseUrl,
683
+ {
684
+ tag: "",
685
+ status: "error",
686
+ score: 0,
687
+ error: e.message,
688
+ },
689
+ clientId,
690
+ clientMeta,
691
+ );
652
692
  } catch {}
653
693
  totalScored++;
654
694
  }
@@ -255,8 +255,14 @@ function renderActiveClients(clients) {
255
255
  const tbody = document.getElementById("activeClientsBody");
256
256
  if (!section || !bar) return;
257
257
 
258
- const types = ["explore", "refresh", "attach", "comments"];
259
- const labels = { explore: "Explore", refresh: "Refresh", attach: "Attach", comments: "Comments" };
258
+ const types = ["explore", "refresh", "attach", "comments", "scoring"];
259
+ const labels = {
260
+ explore: "Explore",
261
+ refresh: "Refresh",
262
+ attach: "Attach",
263
+ comments: "Comments",
264
+ scoring: "Scoring",
265
+ };
260
266
  const grouped = {};
261
267
  for (const c of clients) {
262
268
  if (!grouped[c.type]) grouped[c.type] = [];
@@ -314,9 +320,7 @@ function showClientDetail(type, clients) {
314
320
  tbody.innerHTML = clients
315
321
  .map((c) => {
316
322
  const cid = c.clientId ? c.clientId.substring(0, 8) : "-";
317
- const ipPort = c.ip
318
- ? c.ip + (c.port ? ":" + c.port : "")
319
- : "-";
323
+ const ipPort = c.ip ? c.ip + (c.port ? ":" + c.port : "") : "-";
320
324
  const userId = c.userId || "-";
321
325
  const last = formatRelativeTime(c.lastSeen);
322
326
  return `<tr>
@@ -93,6 +93,7 @@ function inferClientType(routePath) {
93
93
  if (routePath.startsWith("/api/redo-job")) return "refresh";
94
94
  if (routePath.startsWith("/api/user-update-tasks")) return "attach";
95
95
  if (routePath.startsWith("/api/comment-task")) return "comments";
96
+ if (routePath.startsWith("/api/tags")) return "scoring";
96
97
  if (
97
98
  routePath.startsWith("/api/job") ||
98
99
  routePath.startsWith("/api/explore-new")