tt-help-cli-ycl 1.3.46 → 1.3.48
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 +1 -1
- package/src/lib/tiktok-scraper.mjs +21 -7
package/package.json
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import fs from "fs";
|
|
1
2
|
import os from "os";
|
|
2
3
|
import path from "path";
|
|
3
4
|
import { chromium } from "playwright";
|
|
@@ -9,7 +10,7 @@ const DEFAULT_WAF_TTL = 120000;
|
|
|
9
10
|
const DEFAULT_WARM_URL = "https://www.tiktok.com/@nike";
|
|
10
11
|
const BROWSER_CLOSE_TIMEOUT = 5000;
|
|
11
12
|
const DEFAULT_MAX_REQUESTS_PER_PAGE = 50;
|
|
12
|
-
const FALLBACK_PROFILE_PORT =
|
|
13
|
+
const FALLBACK_PROFILE_PORT = 9999;
|
|
13
14
|
|
|
14
15
|
function delay(ms) {
|
|
15
16
|
return new Promise((r) => setTimeout(r, ms));
|
|
@@ -25,6 +26,11 @@ function getFallbackProfileDir() {
|
|
|
25
26
|
);
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
function fallbackProfileExists() {
|
|
30
|
+
const dir = getFallbackProfileDir();
|
|
31
|
+
return fs.existsSync(dir);
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
class PageSlot {
|
|
29
35
|
constructor(page) {
|
|
30
36
|
this.page = page;
|
|
@@ -139,12 +145,16 @@ export class TikTokScraper {
|
|
|
139
145
|
|
|
140
146
|
async _ensureFallback(executablePath) {
|
|
141
147
|
if (this._fallbackContext) return;
|
|
148
|
+
if (!fallbackProfileExists()) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
142
151
|
const fallbackDir = getFallbackProfileDir();
|
|
143
152
|
console.error(`[TikTokScraper] 无登录态获取失败,fallback 到登录态 profile: ${fallbackDir}`);
|
|
144
153
|
const { browser, context, slots } = await initContext(executablePath, 1, fallbackDir);
|
|
145
154
|
this._fallbackBrowser = browser;
|
|
146
155
|
this._fallbackContext = context;
|
|
147
156
|
this._fallbackSlots = slots;
|
|
157
|
+
return true;
|
|
148
158
|
}
|
|
149
159
|
|
|
150
160
|
async close() {
|
|
@@ -275,9 +285,11 @@ export class TikTokScraper {
|
|
|
275
285
|
|
|
276
286
|
// 解析失败:fallback 到登录态
|
|
277
287
|
if (!result) {
|
|
278
|
-
await this._ensureFallback(executablePath);
|
|
279
|
-
|
|
280
|
-
|
|
288
|
+
const ok = await this._ensureFallback(executablePath);
|
|
289
|
+
if (ok) {
|
|
290
|
+
rawHtml = await this._fetchViewSource(url, this._fallbackContext);
|
|
291
|
+
result = rawHtml ? parseUserInfo(rawHtml) : null;
|
|
292
|
+
}
|
|
281
293
|
}
|
|
282
294
|
|
|
283
295
|
return result || null;
|
|
@@ -292,9 +304,11 @@ export class TikTokScraper {
|
|
|
292
304
|
|
|
293
305
|
// 解析失败:fallback 到登录态
|
|
294
306
|
if (!result) {
|
|
295
|
-
await this._ensureFallback(executablePath);
|
|
296
|
-
|
|
297
|
-
|
|
307
|
+
const ok = await this._ensureFallback(executablePath);
|
|
308
|
+
if (ok) {
|
|
309
|
+
rawHtml = await this._fetchViewSource(videoUrl, this._fallbackContext);
|
|
310
|
+
result = rawHtml ? parseVideoInfo(rawHtml) : null;
|
|
311
|
+
}
|
|
298
312
|
}
|
|
299
313
|
|
|
300
314
|
return result || null;
|