webstreamr-github/src/extractor/SuperVideo.ts

58 lines
1.8 KiB
TypeScript

import bytes from 'bytes';
import * as cheerio from 'cheerio';
import { Extractor } from './types';
import { extractUrlFromPacked, Fetcher } from '../utils';
import { Context, CountryCode, UrlResult } from '../types';
import { NotFoundError } from '../error';
export class SuperVideo implements Extractor {
public readonly id = 'supervideo';
public readonly label = 'SuperVideo';
public readonly ttl = 900000; // 15m
private readonly fetcher: Fetcher;
public constructor(fetcher: Fetcher) {
this.fetcher = fetcher;
}
public readonly supports = (_ctx: Context, url: URL): boolean => null !== url.host.match(/supervideo/);
public readonly normalize = (url: URL): URL => new URL(url.href.replace('/e/', '/').replace('/embed-', '/'));
public readonly extract = async (ctx: Context, url: URL, countryCode: CountryCode): Promise<UrlResult[]> => {
const html = await this.fetcher.text(ctx, url);
if (html.includes('This video can be watched as embed only')) {
return await this.extract(ctx, new URL(`/e${url.pathname}`, url.origin), countryCode);
}
if (/'The file was deleted|The file expired/.test(html)) {
throw new NotFoundError();
}
const heightAndSizeMatch = html.match(/\d{3,}x(\d{3,}), ([\d.]+ ?[GM]B)/);
const $ = cheerio.load(html);
const title = $('.download__title').text().trim();
return [
{
url: extractUrlFromPacked(html, [/sources:\[{file:"(.*?)"/]),
label: this.label,
sourceId: `${this.id}_${countryCode}`,
ttl: this.ttl,
meta: {
countryCode,
title,
...(heightAndSizeMatch && {
bytes: bytes.parse(heightAndSizeMatch[2] as string) as number,
height: parseInt(heightAndSizeMatch[1] as string) as number,
}),
},
},
];
};
}