35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import type { RichContent } from "../../model/interfaces.ts";
|
|
import type { RichContentProvider } from "../rich-content-service.ts";
|
|
import { fetchWithTimeout } from "../rich-content-service.ts";
|
|
|
|
const YOUTUBE_REGEX =
|
|
/(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]{11})/;
|
|
|
|
export const youtubeProvider: RichContentProvider = {
|
|
name: "youtube",
|
|
|
|
matches(url: string): boolean {
|
|
return YOUTUBE_REGEX.test(url);
|
|
},
|
|
|
|
async fetch(url: string): Promise<RichContent> {
|
|
const videoId = url.match(YOUTUBE_REGEX)![1];
|
|
const thumbnailUrl = `https://img.youtube.com/vi/${videoId}/hqdefault.jpg`;
|
|
let title: string | undefined;
|
|
|
|
try {
|
|
const oembedUrl =
|
|
`https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=${videoId}&format=json`;
|
|
const res = await fetchWithTimeout(oembedUrl);
|
|
if (res.ok) {
|
|
const data = await res.json() as { title?: string };
|
|
title = data.title;
|
|
}
|
|
} catch {
|
|
// oembed failed — thumbnail still works
|
|
}
|
|
|
|
return { type: "youtube", siteName: "YouTube", url, videoId, title, thumbnailUrl };
|
|
},
|
|
};
|