import { decode as decodeHtml } from 'html-entities';
import * as cheerio from 'cheerio';
import type { default as Summary, Player } from '@/summary.js';
import { clip } from '@/utils/clip.js';
import { cleanupTitle } from '@/utils/cleanup-title.js';
import { get, head, scpaping } from '@/utils/got.js';
/**
* Contains only the html snippet for a sanitized iframe as the thumbnail is
* mostly covered in OpenGraph instead.
*
* Width should always be 100%.
*/
async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise {
const href = $('link[type="application/json+oembed"]').attr('href');
if (!href) {
return null;
}
const oEmbedUrl = (() => {
try {
return new URL(href, pageUrl);
} catch { return null; }
})();
if (!oEmbedUrl) {
return null;
}
const oEmbed = await get(oEmbedUrl.href).catch(() => null);
if (!oEmbed) {
return null;
}
const body = (() => {
try {
return JSON.parse(oEmbed);
} catch { /* empty */ }
})();
if (!body || body.version !== '1.0' || !['rich', 'video'].includes(body.type)) {
// Not a well formed rich oEmbed
return null;
}
if (!body.html.startsWith('