Files
ratebubble/src/utils/contentUrl.ts

50 lines
1.2 KiB
TypeScript

export type SupportedProvider = 'netflix' | 'primevideo';
const NETFLIX_HOSTS = new Set([
'www.netflix.com',
'netflix.com',
'www.netflix.com.tr',
'netflix.com.tr',
]);
const PRIME_HOSTS = new Set([
'www.primevideo.com',
'primevideo.com',
]);
export interface ParsedContentUrl {
provider: SupportedProvider;
id: string;
}
export function parseSupportedContentUrl(rawUrl: string): ParsedContentUrl | null {
try {
const parsedUrl = new URL(rawUrl);
const hostname = parsedUrl.hostname.toLowerCase();
if (NETFLIX_HOSTS.has(hostname)) {
const titleIdMatch = parsedUrl.pathname.match(/\/title\/(\d+)/);
if (!titleIdMatch) return null;
const id = titleIdMatch[1];
if (!id) return null;
return { provider: 'netflix', id };
}
if (PRIME_HOSTS.has(hostname)) {
const detailIdMatch = parsedUrl.pathname.match(/\/detail\/([A-Za-z0-9]+)/);
if (!detailIdMatch) return null;
const id = detailIdMatch[1];
if (!id) return null;
return { provider: 'primevideo', id };
}
return null;
} catch {
return null;
}
}
export function isSupportedContentUrl(rawUrl: string): boolean {
return Boolean(parseSupportedContentUrl(rawUrl));
}