feat(util): add basic extractor

This commit is contained in:
hansputera
2021-11-07 11:02:04 +07:00
parent f270fb06e8
commit 18c9c18a3f
4 changed files with 35 additions and 36 deletions

View File

@@ -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;
};

View File

@@ -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;
};

View 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[],
},
};
}
}
};

View File

@@ -0,0 +1 @@
export * from './extractor';