diff --git a/api/download.ts b/api/download.ts index 64e0412..5570be1 100644 --- a/api/download.ts +++ b/api/download.ts @@ -1,7 +1,7 @@ import type { VercelRequest, VercelResponse } from '@vercel/node'; import ow from 'ow'; -import { snaptik } from '../lib/snaptik'; +import { snaptik } from '../lib'; export default async (req: VercelRequest, res: VercelResponse) => { try { @@ -9,8 +9,8 @@ export default async (req: VercelRequest, res: VercelResponse) => { 'url': ow.string.url, })); - await snaptik.fetchDownloadPage(req.query.url); - return res.status(200).json({ 'error': null }); + const result = await snaptik.fetchDownloadPage(req.query.url); + return res.status(200).json(result); } catch(e) { return res.status(400).json({ 'error': (e as Error).message, diff --git a/api/search.ts b/api/search.ts index 0aa278d..88fdb8e 100644 --- a/api/search.ts +++ b/api/search.ts @@ -1,7 +1,7 @@ import type { VercelRequest, VercelResponse } from '@vercel/node'; import ow from 'ow'; -import { tiktok } from '../lib/tiktok'; +import { tiktok } from '../lib'; const SearchType = ['trend', 'cards']; diff --git a/lib/fetch.ts b/lib/fetch.ts index 6cf7c93..492ca9f 100644 --- a/lib/fetch.ts +++ b/lib/fetch.ts @@ -11,4 +11,7 @@ export const fetch = got.extend({ dnsCache: true, }); -export const Got = got; \ No newline at end of file +export const snaptikFetch = got.extend({ + prefixUrl: 'https://snaptik.app/en', + dnsCache: true, +}); diff --git a/lib/index.ts b/lib/index.ts index a478a1f..de8c844 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1 +1,5 @@ -export * from './fetch'; \ No newline at end of file +export * from './fetch'; +export * from './config'; +export * from './snaptik'; +export * from './tiktok'; +export * from './transformer'; diff --git a/lib/snaptik.ts b/lib/snaptik.ts index 54929ed..dddae8d 100644 --- a/lib/snaptik.ts +++ b/lib/snaptik.ts @@ -1,15 +1,16 @@ -import { Got } from '.'; +import { snaptikFetch } from '.'; interface Extracted { error?: string; + result?: { + thumb: string; + urls: string[]; + } } -class Snaptik { - private client = Got.extend({ - prefixUrl: 'https://snaptik.app/en', - }); +class Snaptik { async fetchDownloadPage(url: string) { - const response = await this.client.get('./abc.php', { + const response = await snaptikFetch('./abc.php', { searchParams: { 'url': url, } @@ -28,10 +29,14 @@ class Snaptik { const obfuscatedScripts = html.match(/[\s\S]*?<\/script>/gi); if (!obfuscatedScripts?.length) return { error: 'Cannot download the video!' }; else { - // remove script tag and trim it - const cleanedScript = obfuscatedScripts[0].replace(/<(\/)?script( type=".+")?>/g, '').trim(); - console.log(cleanedScript); - return { 'error': 'asw' }; + const results = eval(obfuscatedScripts[0].replace(/<(\/)?script( type=".+")?>/g, '').trim().replace('eval', '')).match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/gi); + return { + 'error': undefined, + 'result': { + 'thumb': results.shift(), + 'urls': [...new Set(results)] as string[], + } + }; } } } diff --git a/lib/tiktok.ts b/lib/tiktok.ts index d39cbc5..7b0e15d 100644 --- a/lib/tiktok.ts +++ b/lib/tiktok.ts @@ -1,8 +1,6 @@ -import { fetch, TFetch } from '.'; +import { fetch, TFetch, tiktokBase, Transformer } from '.'; import { ItemEnums, SearchFullResult, SearchPreviewTypeResult } from '../types'; -import { tiktokBase } from './config'; -import { Transformer } from './transformer'; class TikTok { async searchPreview(query: string) {