Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

2.4 KB
Raw Blame
import { ytFetch, optionalCount } from './youtubeApi.ts';
import type { YouTubeKey } from './youtubeApi.ts';
import { NotFoundError, ValidationError } from '../../lib/errors.ts';
import type { YouTubeApiListResponse, YouTubeApiVideo } from './youtubeApi.ts';
import type { YouTubeTrackedVideoData } from '../../../shared/types/youtube.ts';

// requested with part=snippet,statistics only
type TrackedApiVideo = Pick<YouTubeApiVideo, 'id' | 'snippet' | 'statistics'>;

export function extractYouTubeVideoId(url: string): string | null {
    let parsedUrl: URL;
    try {
        parsedUrl = new URL(url.trim());
    } catch {
        // not a url at all, so it has no video id
        return null;
    }

    const { hostname, pathname, searchParams } = parsedUrl;
    if (hostname === 'youtu.be') return pathname.slice(1).split('?')[0];
    if (!hostname.includes('youtube.com')) return null;
    if (pathname.startsWith('/shorts/') || pathname.startsWith('/embed/')) return pathname.split('/')[2];
    return searchParams.get('v');
}

export async function fetchTrackedYouTubeData(videoUrl: string, apiKey: YouTubeKey, signal?: AbortSignal): Promise<YouTubeTrackedVideoData> {
    const videoId = extractYouTubeVideoId(videoUrl);
    if (!videoId) throw new ValidationError('Invalid YouTube URL - could not extract video ID');

    const data = await ytFetch<YouTubeApiListResponse<TrackedApiVideo>>('videos', {
        part: 'snippet,statistics',
        id: videoId,
    }, apiKey, signal);

    if (!data.items || data.items.length === 0) throw new NotFoundError('YouTube video not found - check the URL');

    const { snippet, statistics } = data.items[0];
    const isShort = videoUrl.includes('/shorts/');

    return {
        contentType: 'youtube',
        title: snippet.title,
        channelTitle: snippet.channelTitle,
        channelId: snippet.channelId,
        thumbnail: snippet.thumbnails?.medium?.url || snippet.thumbnails?.default?.url || '',
        publishedAt: snippet.publishedAt,
        viewCount: parseInt(statistics.viewCount || '0', 10),
        likeCount: optionalCount(statistics.likeCount),
        commentCount: optionalCount(statistics.commentCount),
        isShort,
        permalink: isShort
            ? `https://www.youtube.com/shorts/${videoId}`
            : `https://www.youtube.com/watch?v=${videoId}`,
        lastFetchedAt: new Date().toISOString(),
    };
}