Skip to content

Commit

Permalink
extract user metadata from the html
Browse files Browse the repository at this point in the history
  • Loading branch information
drawrowfly committed Dec 21, 2020
1 parent 40ea9b9 commit adb089d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 42 deletions.
54 changes: 12 additions & 42 deletions src/core/TikTok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
UserMetadata,
HashtagMetadata,
Headers,
WebHtmlUserMetadata,
} from '../types';

import { Downloader } from '../core';
Expand Down Expand Up @@ -871,24 +872,9 @@ export class TikTokScraper extends EventEmitter {
};
}

const id = encodeURIComponent(this.input);
const query = {
uri: `${this.mainHost}node/share/user/@${id}?uniqueId=${id}&verifyFp=${this.verifyFp}`,
qs: {
user_agent: this.headers['user-agent'],
},
headers: {
cookie: this.getCookies(true),
},
method: 'GET',
json: true,
};
try {
const response = await this.request<TikTokMetadata>(query);
if (response.statusCode !== 0) {
throw new Error(`Can't find the user: ${this.input}`);
}
this.idStore = response.userInfo.user.id;
const response = await this.getUserProfileInfo();
this.idStore = response.user.id;
return {
id: this.idStore,
secUid: '',
Expand All @@ -913,36 +899,20 @@ export class TikTokScraper extends EventEmitter {
throw `Username is missing`;
}
const options = {
uri: `${this.mainHost}node/share/user/@${this.input}?uniqueId=${this.input}&verifyFp=${this.verifyFp}`,
qs: {
user_agent: this.headers['user-agent'],
screen_width: 1792,
screen_height: 1120,
browser_language: 'en-US',
browser_platform: 'MacIntel',
appId: 1233,
isIOS: false,
isMobile: false,
isAndroid: false,
appType: 'm',
browser_online: true,
browser_version: '5.0 (Macintosh)',
browser_name: 'Mozilla',
},
method: 'GET',
uri: `https://www.tiktok.com/@${this.input}`,
json: true,
};

try {
const response = await this.request<TikTokMetadata>(options);

if (!response) {
throw new Error(`Can't find user: ${this.input}`);
}
if (response.statusCode !== 0) {
throw new Error(`Can't find user: ${this.input}`);
const response = await this.request<string>(options);
const breakResponse = response
.split(`<script id="__NEXT_DATA__" type="application/json" crossorigin="anonymous">`)[1]
.split(`</script>`)[0];
if (breakResponse) {
const userMetadata: WebHtmlUserMetadata = JSON.parse(breakResponse);
return userMetadata.props.pageProps.userInfo;
}
return response.userInfo;
throw new Error(`Can't extract user metadata from the html page`);
} catch (error) {
throw error.message;
}
Expand Down
8 changes: 8 additions & 0 deletions src/types/TikTokApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,11 @@ export interface HashtagMetadata {
};
stats: { videoCount: number; viewCount: number };
}

export interface WebHtmlUserMetadata {
props: {
pageProps: {
userInfo: UserMetadata;
};
};
}

0 comments on commit adb089d

Please sign in to comment.