tt-help-cli-ycl 1.0.1 → 1.0.3

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/src/lib/parser.js CHANGED
@@ -1,47 +1,47 @@
1
- import { USER_SECTION_SIZE } from './constants.js';
2
-
3
- export function extractUserSection(html) {
4
- const idx = html.indexOf('"uniqueId"');
5
- if (idx < 0) return null;
6
- return html.substring(idx, idx + USER_SECTION_SIZE);
7
- }
8
-
9
- export function parseUserSection(section) {
10
- const data = {};
11
-
12
- for (const key of ['uniqueId', 'uid', 'secUid']) {
13
- const m = section.match(new RegExp(`"${key}":"([^"]*)`));
14
- if (m) data[key] = m[1];
15
- }
16
-
17
- for (const key of ['nickname', 'signature']) {
18
- const m = section.match(new RegExp(`"${key}":"((?:[^"\\\\]|\\\\.)*)"`, 'g'));
19
- if (m) {
20
- const raw = m[0].replace(`"${key}":"`, '').replace(/"$/, '');
21
- data[key] = raw.replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
22
- }
23
- }
24
-
25
- for (const key of ['ttSeller', 'verified']) {
26
- const m = section.match(new RegExp(`"${key}":\\s*(true|false)`));
27
- data[key] = m ? m[1] === 'true' : undefined;
28
- }
29
-
30
- for (const key of ['followerCount', 'followingCount', 'heartCount', 'videoCount', 'diggCount']) {
31
- const m = section.match(new RegExp(`"${key}":(\\d+)`));
32
- if (m) data[key] = parseInt(m[1], 10);
33
- }
34
-
35
- const mt = section.match(/"createTime":(\d+)/);
36
- if (mt) data.createTime = parseInt(mt[1], 10);
37
-
38
- const ma = section.match(/"avatarLarger":"([^"]*)/);
39
- if (ma) data.avatarLarger = ma[1].replace(/\\u002F/g, '/');
40
-
41
- return data;
42
- }
43
-
44
- export function extractLocationCreated(html) {
45
- const m = html.match(/"locationCreated":"([^"]*)/);
46
- return m ? m[1] : null;
47
- }
1
+ import { USER_SECTION_SIZE } from './constants.js';
2
+
3
+ export function extractUserSection(html) {
4
+ const idx = html.indexOf('"uniqueId"');
5
+ if (idx < 0) return null;
6
+ return html.substring(idx, idx + USER_SECTION_SIZE);
7
+ }
8
+
9
+ export function parseUserSection(section) {
10
+ const data = {};
11
+
12
+ for (const key of ['uniqueId', 'uid', 'secUid']) {
13
+ const m = section.match(new RegExp(`"${key}":"([^"]*)`));
14
+ if (m) data[key] = m[1];
15
+ }
16
+
17
+ for (const key of ['nickname', 'signature']) {
18
+ const m = section.match(new RegExp(`"${key}":"((?:[^"\\\\]|\\\\.)*)"`, 'g'));
19
+ if (m) {
20
+ const raw = m[0].replace(`"${key}":"`, '').replace(/"$/, '');
21
+ data[key] = raw.replace(/\\n/g, '\n').replace(/\\\\/g, '\\');
22
+ }
23
+ }
24
+
25
+ for (const key of ['ttSeller', 'verified']) {
26
+ const m = section.match(new RegExp(`"${key}":\\s*(true|false)`));
27
+ data[key] = m ? m[1] === 'true' : undefined;
28
+ }
29
+
30
+ for (const key of ['followerCount', 'followingCount', 'heartCount', 'videoCount', 'diggCount']) {
31
+ const m = section.match(new RegExp(`"${key}":(\\d+)`));
32
+ if (m) data[key] = parseInt(m[1], 10);
33
+ }
34
+
35
+ const mt = section.match(/"createTime":(\d+)/);
36
+ if (mt) data.createTime = parseInt(mt[1], 10);
37
+
38
+ const ma = section.match(/"avatarLarger":"([^"]*)/);
39
+ if (ma) data.avatarLarger = ma[1].replace(/\\u002F/g, '/');
40
+
41
+ return data;
42
+ }
43
+
44
+ export function extractLocationCreated(html) {
45
+ const m = html.match(/"locationCreated":"([^"]*)/);
46
+ return m ? m[1] : null;
47
+ }
package/src/lib/scrape.js CHANGED
@@ -1,39 +1,39 @@
1
- import { extractUserSection, parseUserSection, extractLocationCreated } from './parser.js';
2
- import { fetchHtml, makeProfileUrl, isProfileUrl, isVideoUrl, extractProfileHandle } from './fetcher.js';
3
-
4
- export async function extractUserData(profileUrl, proxyUrl) {
5
- const profileHtml = await fetchHtml(profileUrl, proxyUrl);
6
- const section = extractUserSection(profileHtml);
7
- if (!section) throw new Error('无法解析用户信息');
8
- const data = parseUserSection(section);
9
- data.locationCreated = extractLocationCreated(profileHtml);
10
- return data;
11
- }
12
-
13
- export async function extractVideoLocation(videoUrl, proxyUrl) {
14
- const videoHtml = await fetchHtml(videoUrl, proxyUrl);
15
- return extractLocationCreated(videoHtml);
16
- }
17
-
18
- export async function processUrl(url, proxyUrl) {
19
- if (isProfileUrl(url)) {
20
- const profileUrl = makeProfileUrl(url);
21
- const profileData = await extractUserData(profileUrl, proxyUrl);
22
- return [profileData];
23
- }
24
-
25
- if (isVideoUrl(url)) {
26
- const profileHandle = extractProfileHandle(url);
27
- if (!profileHandle) throw new Error(`无法从视频URL提取用户主页: ${url}`);
28
-
29
- const profileUrl = makeProfileUrl(profileHandle);
30
- const [profileData, locationCreated] = await Promise.all([
31
- extractUserData(profileUrl, proxyUrl),
32
- extractVideoLocation(url, proxyUrl),
33
- ]);
34
-
35
- return [{ ...profileData, locationCreated }];
36
- }
37
-
38
- return [];
39
- }
1
+ import { extractUserSection, parseUserSection, extractLocationCreated } from './parser.js';
2
+ import { fetchHtml, makeProfileUrl, isProfileUrl, isVideoUrl, extractProfileHandle } from './fetcher.js';
3
+
4
+ export async function extractUserData(profileUrl, proxyUrl) {
5
+ const profileHtml = await fetchHtml(profileUrl, proxyUrl);
6
+ const section = extractUserSection(profileHtml);
7
+ if (!section) throw new Error('无法解析用户信息');
8
+ const data = parseUserSection(section);
9
+ data.locationCreated = extractLocationCreated(profileHtml);
10
+ return data;
11
+ }
12
+
13
+ export async function extractVideoLocation(videoUrl, proxyUrl) {
14
+ const videoHtml = await fetchHtml(videoUrl, proxyUrl);
15
+ return extractLocationCreated(videoHtml);
16
+ }
17
+
18
+ export async function processUrl(url, proxyUrl) {
19
+ if (isProfileUrl(url)) {
20
+ const profileUrl = makeProfileUrl(url);
21
+ const profileData = await extractUserData(profileUrl, proxyUrl);
22
+ return [profileData];
23
+ }
24
+
25
+ if (isVideoUrl(url)) {
26
+ const profileHandle = extractProfileHandle(url);
27
+ if (!profileHandle) throw new Error(`无法从视频URL提取用户主页: ${url}`);
28
+
29
+ const profileUrl = makeProfileUrl(profileHandle);
30
+ const [profileData, locationCreated] = await Promise.all([
31
+ extractUserData(profileUrl, proxyUrl),
32
+ extractVideoLocation(url, proxyUrl),
33
+ ]);
34
+
35
+ return [{ ...profileData, locationCreated }];
36
+ }
37
+
38
+ return [];
39
+ }