fix(snaptik): only match url and remove dupe url

This commit is contained in:
hansputera
2021-11-06 15:07:36 +07:00
parent 38110d2028
commit 8690bccbbf
6 changed files with 29 additions and 19 deletions

View File

@@ -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,

View File

@@ -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'];

View File

@@ -11,4 +11,7 @@ export const fetch = got.extend({
dnsCache: true,
});
export const Got = got;
export const snaptikFetch = got.extend({
prefixUrl: 'https://snaptik.app/en',
dnsCache: true,
});

View File

@@ -1 +1,5 @@
export * from './fetch';
export * from './fetch';
export * from './config';
export * from './snaptik';
export * from './tiktok';
export * from './transformer';

View File

@@ -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(/<script[\s\S]*?>[\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[],
}
};
}
}
}

View File

@@ -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) {