diff --git a/backend/src/app.js b/backend/src/app.js index f64308e..aec85a5 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -245,11 +245,14 @@ export function buildApp(config, deps = {}) { try { const url = `https://www.googleapis.com/youtube/v3/search?part=snippet&type=video` + - `&videoCategoryId=10&maxResults=1&q=${encodeURIComponent(q)}&key=${encodeURIComponent(key)}` + `&videoCategoryId=10&maxResults=5&q=${encodeURIComponent(q)}&key=${encodeURIComponent(key)}` const res = await (deps.playFetch ?? fetch)(url) if (!res.ok) return reply.code(502).send({ error: `YouTube svarade ${res.status}` }) const js = await res.json() - const hit = js.items?.[0] + const items = js.items ?? [] + // föredra "Artist - Topic"-kanalens ljudspår (= låten som på YT Music) + // framför officiella musikvideor + const hit = items.find((i) => (i.snippet?.channelTitle ?? '').endsWith(' - Topic')) ?? items[0] if (!hit) return reply.code(404).send({ error: 'ingen video hittades' }) return { videoId: hit.id.videoId, diff --git a/backend/test/search.test.js b/backend/test/search.test.js index 85ec32d..698ee18 100644 --- a/backend/test/search.test.js +++ b/backend/test/search.test.js @@ -141,7 +141,10 @@ test('GET /api/play slår upp videoId via YouTube Data API', async () => { return { ok: true, json: async () => ({ - items: [{ id: { videoId: 'abc123' }, snippet: { title: 'Sanctified with Dynamite', channelTitle: 'Powerwolf - Topic' } }], + items: [ + { id: { videoId: 'video99' }, snippet: { title: 'Official Video', channelTitle: 'Metal Blade Records' } }, + { id: { videoId: 'abc123' }, snippet: { title: 'Sanctified with Dynamite', channelTitle: 'Powerwolf - Topic' } }, + ], }), } },