feat: optimize now playing query

This commit is contained in:
Cory Dransfeldt 2024-08-01 12:45:09 -07:00
parent 6723935876
commit f1d73e1341
No known key found for this signature in database
3 changed files with 12 additions and 42 deletions

4
package-lock.json generated
View file

@ -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",

View file

@ -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": {

View file

@ -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 <a href="https://coryd.dev/music/artists/${sanitizeMediaString(scrobbleData.artist_name)}-${sanitizeMediaString(parseCountryField(scrobbleData.artists.country))}">${
scrobbleData.artist_name
}</a>`,
content: `${emoji || '🎧'} ${data.track_name} by <a href="https://coryd.dev/music/artists/${sanitizeMediaString(data.artist_name)}-${sanitizeMediaString(parseCountryField(data.artist_country))}">${data.artist_name}</a>`,
}), { headers })
}
}