From dcd4928813e7800985e8b4802e73216e12246d65 Mon Sep 17 00:00:00 2001 From: claude Date: Sat, 1 Aug 2026 05:31:04 +0200 Subject: [PATCH] =?UTF-8?q?Spelaren=20f=C3=B6redrar=20l=C3=A5tversionen=20?= =?UTF-8?q?(Artist=20-=20Topic)=20framf=C3=B6r=20musikvideor,=20som=20p?= =?UTF-8?q?=C3=A5=20YT=20Music?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01824ZrvG2mDYYrmwqypLNup --- backend/src/app.js | 7 +++++-- backend/test/search.test.js | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) 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' } }, + ], }), } },