summaryrefslogtreecommitdiff
path: root/src/routes/api/playlist/+server.ts
blob: b70ac26f1e29fa82020fc6703e3188f5e405678d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { json } from '@sveltejs/kit';
import { getPlaylist } from '$lib/server/ytdlp';
import type { RequestHandler } from './$types';

export const GET: RequestHandler = async ({ url }) => {
  const playlistUrl = url.searchParams.get('url');
  if (!playlistUrl) {
    return json({ error: 'Missing playlist URL' }, { status: 400 });
  }

  // Validate it's a YouTube playlist URL
  if (!playlistUrl.includes('youtube.com/playlist') && !playlistUrl.includes('list=')) {
    return json({ error: 'Invalid YouTube playlist URL' }, { status: 400 });
  }

  try {
    const playlist = await getPlaylist(playlistUrl);
    return json(playlist);
  } catch (e) {
    console.error('Playlist fetch error:', e);
    return json({ error: 'Failed to fetch playlist' }, { status: 500 });
  }
};