From 68c4e4a863eb5c00c267125e92632a5d45d35fa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferhat=20Suat=20Erdo=C4=9Fan?= Date: Sat, 12 Mar 2022 22:58:50 +0300 Subject: [PATCH] s1 --- src/core/TikTok.ts | 57 +++++++++++++++++++++++++++------------------- src/entry.ts | 3 +-- 2 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/core/TikTok.ts b/src/core/TikTok.ts index 62997a9b..84cb7699 100644 --- a/src/core/TikTok.ts +++ b/src/core/TikTok.ts @@ -21,7 +21,6 @@ import { ScrapeType, TikTokConstructor, Result, - MusicMetadata, RequestQuery, History, Proxy, @@ -1123,7 +1122,7 @@ export class TikTokScraper extends EventEmitter { * Get music information * @param {} music link */ - public async getMusicInfo(): Promise { + public async getMusicInfo(): Promise { if (!this.input) { throw new Error(`Music is missing`); } @@ -1131,7 +1130,7 @@ export class TikTokScraper extends EventEmitter { const musicTitle = /music\/([\w-]+)-\d+/.exec(this.input); const musicId = /music\/[\w-]+-(\d+)/.exec(this.input); - const query = { + /* const query = { uri: `https://www.tiktok.com/node/share/music/${musicTitle ? musicTitle[1] : ''}-${musicId ? musicId[1] : ''}`, qs: { screen_width: 1792, @@ -1154,22 +1153,38 @@ export class TikTokScraper extends EventEmitter { }, method: 'GET', json: true, - }; - - const unsignedURL = `${query.uri}?${new URLSearchParams(query.qs as any).toString()}`; - const _signature = sign(unsignedURL, this.headers['user-agent']); + }; */ - // @ts-ignore - query.qs._signature = _signature; + const options = { + method: 'GET', + uri: `https://www.tiktok.com/music/${musicTitle ? musicTitle[1] : ''}-${musicId ? musicId[1] : ''}`, + json: true, + }; - try { - const response = await this.request(query); + /* try { + const response = await this.request(options); + const breakResponse = response + .split(/`)[0]; + if (breakResponse) { + const musicMetadata = JSON.parse(breakResponse); + return musicMetadata.props.pageProps.musicInfo; + } + } */ /* try { + const response = await this.request(options); if (response.statusCode !== 0) { throw new Error(`Can't find music data: ${this.input}`); } return response.musicInfo; - } catch (error) { - throw new Error(error.message); + } */try { + const response = await this.request(options); + const breakResponse = response + .split(/`)[0]; + const musicMetadata = JSON.parse(breakResponse); + return musicMetadata.props.pageProps.musicInfo; + }catch (error) { + throw new Error(); } } @@ -1189,7 +1204,7 @@ export class TikTokScraper extends EventEmitter { * This method can be used if you aren't able to retrieve video metadata from a simple API call * Can be slow */ - private async getVideoMetadataFromHtml(): Promise { + /* private async getVideoMetadataFromHtml(): Promise { const options = { uri: this.input, method: 'GET', @@ -1201,7 +1216,7 @@ export class TikTokScraper extends EventEmitter { throw new Error(`Can't extract video meta data`); } - if (response.includes("__NEXT_DATA__")){ + if (response.includes('__NEXT_DATA__')) { const rawVideoMetadata = response .split(/`)[0]; @@ -1211,23 +1226,21 @@ export class TikTokScraper extends EventEmitter { return videoData as FeedItems; } - if (response.includes("SIGI_STATE")) { + if (response.includes('SIGI_STATE')) { // Sometimes you may receive a state in different format, so we should parse it too // New format - https://pastebin.com/WLUpL0ei - const rawVideoMetadata = response - .split("window['SIGI_STATE']=")[1] - .split(";window['SIGI_RETRY']=")[0]; + const rawVideoMetadata = response.split("window['SIGI_STATE']=")[1].split(";window['SIGI_RETRY']=")[0]; const videoProps = JSON.parse(rawVideoMetadata); const videoData = Object.values(videoProps.ItemModule)[0]; return videoData as FeedItems; } - throw new Error('No available parser for html page') + throw new Error('No available parser for html page'); } catch (error) { throw new Error(`Can't extract video metadata: ${this.input}`); } - } + } */ /** * Get video metadata from the regular API endpoint @@ -1270,8 +1283,6 @@ export class TikTokScraper extends EventEmitter { let videoData = {} as FeedItems; if (html) { - videoData = await this.getVideoMetadataFromHtml(); - } else { videoData = await this.getVideoMetadata(); } diff --git a/src/entry.ts b/src/entry.ts index 68f44dca..14a7cb49 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -17,7 +17,6 @@ import { PostCollector, History, HistoryItem, - MusicMetadata, } from './types'; import CONST from './constant'; import { makeVerifyFp } from './helpers'; @@ -140,7 +139,7 @@ export const getHashtagInfo = async (input: string, options = {} as Options): Pr return result; }; -export const getMusicInfo = async (input: string, options = {} as Options): Promise => { +export const getMusicInfo = async (input: string, options = {} as Options): Promise => { if (options && typeof options !== 'object') { throw new TypeError('Object is expected'); }