Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the html scrape for getting music info #580

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 13 additions & 41 deletions src/core/TikTok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,56 +1068,28 @@ export class TikTokScraper extends EventEmitter {

/**
* Get music information
* @param {} music link
* @param {} music ID
*/
public async getMusicInfo(): Promise<MusicMetadata> {
await this.getValidHeaders(this.input, false);
if (!this.input) {
throw `Music is missing`;
throw `MusicID is missing`;
}

const musicTitle = /music\/([\w-]+)-\d+/.exec(this.input);
const musicId = /music\/[\w-]+-(\d+)/.exec(this.input);

const query = {
uri: `${this.mainHost}node/share/music/${musicTitle ? musicTitle[1] : ''}-${musicId ? musicId[1] : ''}`,
qs: {
screen_width: 1792,
screen_height: 1120,
lang: 'en',
priority_region: '',
referer: '',
root_referer: '',
app_language: 'en',
is_page_visible: true,
history_len: 6,
focus_state: true,
is_fullscreen: false,
aid: 1988,
app_name: 'tiktok_web',
timezone_name: '',
device_platform: 'web',
musicId: musicId ? musicId[1] : '',
musicName: musicTitle ? musicTitle[1] : '',
},
const options = {
method: 'GET',
uri: `https://www.tiktok.com/music/original-sound-${this.input}`,
json: true,
};

const unsignedURL = `${query.uri}?${new URLSearchParams(query.qs as any).toString()}`;
const _signature = sign(unsignedURL);

// @ts-ignore
query.qs._signature = _signature;

try {
const response = await this.request<TikTokMetadata>(query);
if (response.statusCode !== 0) {
throw new Error(`Can't find music data: ${this.input}`);
const response = await this.request(options);
const breakResponse = response
.split(/<script id="__NEXT_DATA__" type="application\/json" nonce="[a-z0-9\-\_]+" crossorigin="anonymous">/i)[1]
.split(`</script>`)[0];
if (breakResponse) {
const musicMetadata = JSON.parse(breakResponse);
return musicMetadata.props.pageProps.musicInfo;
}
return response.musicInfo;
} catch (error) {
throw error.message;
}
catch (_a) {
}
}

Expand Down