mirror of
https://github.com/hansputera/tiktok-dl.git
synced 2026-04-05 19:51:57 +02:00
feat(provider): organizing provider
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import type {VercelRequest, VercelResponse} from '@vercel/node';
|
||||
import ow from 'ow';
|
||||
|
||||
import {snaptik} from '../lib';
|
||||
import {getRandomProvider} from '../lib/providers';
|
||||
|
||||
export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
try {
|
||||
@@ -9,7 +8,7 @@ export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
'url': ow.string.url,
|
||||
}));
|
||||
|
||||
const result = await snaptik.fetchDownloadPage(req.query.url);
|
||||
const result = await getRandomProvider().fetch(req.query.url);
|
||||
return res.status(200).json(result);
|
||||
} catch (e) {
|
||||
return res.status(400).json({
|
||||
|
||||
19
lib/decorators/handleException.ts
Normal file
19
lib/decorators/handleException.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
/**
|
||||
* Decorator to handle exception.
|
||||
* @return {Function}
|
||||
*/
|
||||
export const handleException = (): Function => {
|
||||
return function(
|
||||
target: any,
|
||||
_: string,
|
||||
descriptor: PropertyDescriptor) {
|
||||
const oldValue = descriptor.value;
|
||||
descriptor.value = (...args: any) => {
|
||||
try {
|
||||
return oldValue.apply(target, args);
|
||||
} catch (err) {
|
||||
return {'error': (err as Error).message};
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
export * from './fetch';
|
||||
export * from './config';
|
||||
export * from './snaptik';
|
||||
export * from './providers';
|
||||
export * from './tiktok';
|
||||
export * from './transformer';
|
||||
|
||||
16
lib/providers/baseProvider.ts
Normal file
16
lib/providers/baseProvider.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
export interface ExtractedInfo {
|
||||
error?: string;
|
||||
result?: {
|
||||
thumb?: string;
|
||||
urls: string[];
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @class BaseProvider
|
||||
*/
|
||||
export abstract class BaseProvider {
|
||||
abstract resourceName(): string;
|
||||
abstract fetch(url: string): Promise<ExtractedInfo>;
|
||||
abstract extract(html: string): ExtractedInfo;
|
||||
};
|
||||
9
lib/providers/index.ts
Normal file
9
lib/providers/index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import {SnaptikProvider} from './snaptikProvider';
|
||||
|
||||
export const Providers = [
|
||||
new SnaptikProvider(),
|
||||
];
|
||||
|
||||
export const getRandomProvider = () => Providers[
|
||||
Math.floor(Math.random() * Providers.length)
|
||||
];
|
||||
@@ -1,41 +1,46 @@
|
||||
import {snaptikFetch} from '.';
|
||||
|
||||
interface Extracted {
|
||||
error?: string;
|
||||
result?: {
|
||||
thumb: string;
|
||||
urls: string[];
|
||||
}
|
||||
}
|
||||
import {snaptikFetch} from '..';
|
||||
import {handleException} from '../decorators/handleException';
|
||||
import {BaseProvider, ExtractedInfo} from './baseProvider';
|
||||
|
||||
/**
|
||||
* @class Snaptik
|
||||
* @class SnaptikProvider
|
||||
*/
|
||||
class Snaptik {
|
||||
export class SnaptikProvider extends BaseProvider {
|
||||
/**
|
||||
*
|
||||
* @return {string}
|
||||
*/
|
||||
public resourceName(): string {
|
||||
return 'snaptik';
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} url - TikTok URL
|
||||
* @return {Extracted}
|
||||
* @param {string} url - TikTok Video URL
|
||||
* @return {Promise<ExtractedInfo>}
|
||||
*/
|
||||
async fetchDownloadPage(url: string) {
|
||||
@handleException()
|
||||
public async fetch(url: string): Promise<ExtractedInfo> {
|
||||
const response = await snaptikFetch('./abc.php', {
|
||||
searchParams: {
|
||||
'url': url,
|
||||
},
|
||||
});
|
||||
|
||||
return this.extractInfo(response.body);
|
||||
return this.extract(response.body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} html - HTML Raw
|
||||
* @return {Extracted}
|
||||
* Extract information from raw html
|
||||
* @param {string} html - Raw HTML
|
||||
* @return {ExtractedInfo}
|
||||
*/
|
||||
private extractInfo(html: string): Extracted {
|
||||
extract(html: string): ExtractedInfo {
|
||||
if (/error/gi.test(html)) {
|
||||
return {
|
||||
'error': html.split('\'')
|
||||
.find((x) => /(((url)? error))|could)/gi.test(x)),
|
||||
.find((x) => /(((url)? error)|could)/gi.test(x)),
|
||||
};
|
||||
} else {
|
||||
// only match script tag
|
||||
@@ -58,6 +63,4 @@ class Snaptik {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const snaptik = new Snaptik();
|
||||
};
|
||||
@@ -14,7 +14,8 @@
|
||||
"skipLibCheck": true,
|
||||
"skipDefaultLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noImplicitAny": true
|
||||
"noImplicitAny": true,
|
||||
"experimentalDecorators": true
|
||||
},
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user