From f1d73e134177495914fd39742d8bd4e02a2e9ec1 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Thu, 1 Aug 2024 12:45:09 -0700 Subject: [PATCH] feat: optimize now playing query --- package-lock.json | 4 ++-- package.json | 2 +- workers/playing/index.js | 48 ++++++++-------------------------------- 3 files changed, 12 insertions(+), 42 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6ec54f06..d02b848e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "21.0.2", + "version": "21.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "21.0.2", + "version": "21.1.0", "license": "MIT", "dependencies": { "@cdransf/api-text": "^1.4.0", diff --git a/package.json b/package.json index d11dfe1d..7233cf9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "21.0.2", + "version": "21.1.0", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "scripts": { diff --git a/workers/playing/index.js b/workers/playing/index.js index 1c50d784..63ae02ed 100644 --- a/workers/playing/index.js +++ b/workers/playing/index.js @@ -16,31 +16,12 @@ const getCountryName = (countryCode) => regionNames.of(countryCode.trim()) || co const parseCountryField = (countryField) => { if (!countryField) return null - const delimiters = [',', '/', '&', 'and'] - let countries = [countryField] - - delimiters.forEach(delimiter => { - countries = countries.flatMap(country => country.split(delimiter)) - }) + const delimiters = /[,\/&and]+/ + const countries = countryField.split(delimiters) return countries.map(getCountryName).join(', ') } -const fetchGenreById = async (supabase, genreId) => { - const { data, error } = await supabase - .from('genres') - .select('emoji') - .eq('id', genreId) - .single() - - if (error) { - console.error('Error fetching genre:', error) - return null - } - - return data.emoji -} - export default { async fetch(request, env) { const SUPABASE_URL = env.SUPABASE_URL @@ -48,15 +29,9 @@ export default { const supabase = createClient(SUPABASE_URL, SUPABASE_KEY) const { data, error } = await supabase - .from('listens') - .select(` - track_name, - artist_name, - listened_at, - artists (mbid, genres, country, emoji) - `) - .order('listened_at', { ascending: false }) - .range(0, 1) + .from('optimized_latest_listen') + .select('*') + .single() const headers = { "Content-Type": "application/json", @@ -68,18 +43,13 @@ export default { return new Response(JSON.stringify({ error: "Failed to fetch the latest track" }), { headers }) } - if (data.length === 0) { - return new Response(JSON.stringify({ message: "No recent tracks found" }), { headers }) - } + if (!data) return new Response(JSON.stringify({ message: "No recent tracks found" }), { headers }) - const scrobbleData = data[0] - const genreEmoji = await fetchGenreById(supabase, scrobbleData.artists.genres) - const emoji = scrobbleData.artists.emoji || genreEmoji + const genreEmoji = data.genre_emoji + const emoji = data.artist_emoji || genreEmoji return new Response(JSON.stringify({ - content: `${emoji || '🎧'} ${scrobbleData.track_name} by ${ - scrobbleData.artist_name - }`, + content: `${emoji || '🎧'} ${data.track_name} by ${data.artist_name}`, }), { headers }) } } \ No newline at end of file