mirror of
https://github.com/hansputera/tiktok-dl.git
synced 2026-04-05 19:51:57 +02:00
feat(util): add basic extractor
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {snaptikFetch} from '..';
|
||||
import {handleException} from '../decorators';
|
||||
import {BaseProvider, ExtractedInfo} from './baseProvider';
|
||||
import {basicExtractor} from './util';
|
||||
|
||||
/**
|
||||
* @class SnaptikProvider
|
||||
@@ -38,37 +39,5 @@ export class SnaptikProvider extends BaseProvider {
|
||||
return this.extract(response.body);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract information from raw html
|
||||
* @param {string} html - Raw HTML
|
||||
* @return {ExtractedInfo}
|
||||
*/
|
||||
extract(html: string): ExtractedInfo {
|
||||
if (/error/gi.test(html)) {
|
||||
return {
|
||||
'error': html.split('\'')
|
||||
.find((x) => /(((url)? error)|could)/gi.test(x)),
|
||||
};
|
||||
} else {
|
||||
// only match script tag
|
||||
const obfuscatedScripts = html
|
||||
.match(/<script[\s\S]*?>[\s\S]*?<\/script>/gi);
|
||||
if (!obfuscatedScripts?.length) {
|
||||
return {
|
||||
error: 'Cannot download the video!',
|
||||
};
|
||||
} else {
|
||||
// eslint-disable-next-line max-len
|
||||
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[],
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
public extract = basicExtractor;
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {Providers, tikmateFetch} from '..';
|
||||
import {tikmateFetch} from '..';
|
||||
import {handleException} from '../decorators';
|
||||
import {BaseProvider, ExtractedInfo} from './baseProvider';
|
||||
import {basicExtractor} from './util';
|
||||
|
||||
/**
|
||||
* @class TikmateProvider
|
||||
@@ -49,6 +50,5 @@ export class TikmateProvider extends BaseProvider {
|
||||
return this.extract(abcResponse.body);
|
||||
}
|
||||
|
||||
public extract = (Providers.find(
|
||||
(p) => p.resourceName() === 'snaptik') as BaseProvider).extract;
|
||||
public extract = basicExtractor;
|
||||
};
|
||||
|
||||
29
lib/providers/util/extractor.ts
Normal file
29
lib/providers/util/extractor.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import {ExtractedInfo} from '../baseProvider';
|
||||
|
||||
export const basicExtractor = (html: string): ExtractedInfo => {
|
||||
if (/error/gi.test(html)) {
|
||||
return {
|
||||
'error': html.split('\'')
|
||||
.find((x) => /(((url)? error)|could)/gi.test(x)),
|
||||
};
|
||||
} else {
|
||||
// only match script tag
|
||||
const obfuscatedScripts = html
|
||||
.match(/<script[\s\S]*?>[\s\S]*?<\/script>/gi);
|
||||
if (!obfuscatedScripts?.length) {
|
||||
return {
|
||||
error: 'Cannot download the video!',
|
||||
};
|
||||
} else {
|
||||
// eslint-disable-next-line max-len
|
||||
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[],
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
1
lib/providers/util/index.ts
Normal file
1
lib/providers/util/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './extractor';
|
||||
Reference in New Issue
Block a user