ai generated solutions to our ai generated problems
This commit is contained in:
+5
-10
@@ -126,7 +126,7 @@ const CSP_DIRECTIVES = [
|
|||||||
"style-src 'self'",
|
"style-src 'self'",
|
||||||
"style-src-elem 'self'",
|
"style-src-elem 'self'",
|
||||||
"style-src-attr 'unsafe-inline'",
|
"style-src-attr 'unsafe-inline'",
|
||||||
"img-src 'self' data: blob: https://*.basemaps.cartocdn.com https://basemaps.cartocdn.com https://lastfm.freetls.fastly.net https://*.lastfm.freetls.fastly.net",
|
"img-src 'self' data: blob: https://*.basemaps.cartocdn.com https://basemaps.cartocdn.com",
|
||||||
"font-src 'self' data:",
|
"font-src 'self' data:",
|
||||||
"connect-src 'self' https://challenges.cloudflare.com",
|
"connect-src 'self' https://challenges.cloudflare.com",
|
||||||
"frame-src https://challenges.cloudflare.com",
|
"frame-src https://challenges.cloudflare.com",
|
||||||
@@ -179,7 +179,8 @@ let analyticsDb = null
|
|||||||
let latestUptimeSnapshot = null
|
let latestUptimeSnapshot = null
|
||||||
|
|
||||||
function songHistoryPath() {
|
function songHistoryPath() {
|
||||||
return path.join(uptimeStoragePath(), LASTFM_HISTORY_FILE)
|
const fileName = path.basename(LASTFM_HISTORY_FILE || 'lastfm-song-of-day.json')
|
||||||
|
return path.join(uptimeStoragePath(), fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
function readSongHistory() {
|
function readSongHistory() {
|
||||||
@@ -200,12 +201,6 @@ function todayKey() {
|
|||||||
return new Date().toISOString().slice(0, 10)
|
return new Date().toISOString().slice(0, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
function lastfmImage(track) {
|
|
||||||
const images = Array.isArray(track.image) ? track.image : []
|
|
||||||
const image = [...images].reverse().find((item) => item?.['#text'])
|
|
||||||
return image?.['#text'] || ''
|
|
||||||
}
|
|
||||||
|
|
||||||
function normalizeLastfmTrack(track) {
|
function normalizeLastfmTrack(track) {
|
||||||
const artist = track?.artist?.['#text'] || track?.artist?.name || ''
|
const artist = track?.artist?.['#text'] || track?.artist?.name || ''
|
||||||
const name = track?.name || ''
|
const name = track?.name || ''
|
||||||
@@ -217,7 +212,7 @@ function normalizeLastfmTrack(track) {
|
|||||||
name,
|
name,
|
||||||
album: track?.album?.['#text'] || '',
|
album: track?.album?.['#text'] || '',
|
||||||
url: track?.url || '',
|
url: track?.url || '',
|
||||||
image: lastfmImage(track),
|
image: '',
|
||||||
played_at: track?.date?.uts ? Number(track.date.uts) : null,
|
played_at: track?.date?.uts ? Number(track.date.uts) : null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1901,7 +1896,7 @@ const server = http.createServer((req, res) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method === 'GET' && req.url === '/api/lastfm/song-of-day') {
|
if (req.method === 'GET' && req.url === '/api/song-of-day') {
|
||||||
if (!isSameOriginRequest(req)) {
|
if (!isSameOriginRequest(req)) {
|
||||||
sendJson(res, 403, { error: 'Song of the day is restricted to this site' })
|
sendJson(res, 403, { error: 'Song of the day is restricted to this site' })
|
||||||
return
|
return
|
||||||
|
|||||||
+1
-1
@@ -16,7 +16,7 @@ const apiEndpoints = {
|
|||||||
viewers: '/api/viewers',
|
viewers: '/api/viewers',
|
||||||
viewerEvent: '/api/viewers/event',
|
viewerEvent: '/api/viewers/event',
|
||||||
viewerDelete: '/api/viewers/delete',
|
viewerDelete: '/api/viewers/delete',
|
||||||
songOfDay: '/api/lastfm/song-of-day',
|
songOfDay: '/api/song-of-day',
|
||||||
teams: '/api/tss/leaderboard/teams?limit=100',
|
teams: '/api/tss/leaderboard/teams?limit=100',
|
||||||
teamsHealth: '/api/tss/leaderboard/teams?limit=1',
|
teamsHealth: '/api/tss/leaderboard/teams?limit=1',
|
||||||
resolve: (name) => `/api/tss/teams/resolve?name=${encodeURIComponent(name)}`,
|
resolve: (name) => `/api/tss/teams/resolve?name=${encodeURIComponent(name)}`,
|
||||||
|
|||||||
+5
-7
@@ -47,7 +47,8 @@ function expandHome(filePath) {
|
|||||||
|
|
||||||
function devSongHistoryPath(env) {
|
function devSongHistoryPath(env) {
|
||||||
const storageDir = path.resolve(expandHome(env.UPTIME_STORAGE_DIR || '~/tsswebstorage'))
|
const storageDir = path.resolve(expandHome(env.UPTIME_STORAGE_DIR || '~/tsswebstorage'))
|
||||||
return path.join(storageDir, env.LASTFM_HISTORY_FILE || 'lastfm-song-of-day.json')
|
const fileName = path.basename(env.LASTFM_HISTORY_FILE || 'lastfm-song-of-day.json')
|
||||||
|
return path.join(storageDir, fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
function readDevSongHistory(env) {
|
function readDevSongHistory(env) {
|
||||||
@@ -69,16 +70,13 @@ function normalizeLastfmTrack(track) {
|
|||||||
const name = track?.name || ''
|
const name = track?.name || ''
|
||||||
if (!artist || !name) return null
|
if (!artist || !name) return null
|
||||||
|
|
||||||
const images = Array.isArray(track.image) ? track.image : []
|
|
||||||
const image = [...images].reverse().find((item) => item?.['#text'])
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: `${artist.toLowerCase()}::${name.toLowerCase()}`,
|
id: `${artist.toLowerCase()}::${name.toLowerCase()}`,
|
||||||
artist,
|
artist,
|
||||||
name,
|
name,
|
||||||
album: track?.album?.['#text'] || '',
|
album: track?.album?.['#text'] || '',
|
||||||
url: track?.url || '',
|
url: track?.url || '',
|
||||||
image: image?.['#text'] || '',
|
image: '',
|
||||||
played_at: track?.date?.uts ? Number(track.date.uts) : null,
|
played_at: track?.date?.uts ? Number(track.date.uts) : null,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -176,7 +174,7 @@ function isAllowedApiUrl(req) {
|
|||||||
|
|
||||||
if (url.pathname === '/api/viewers' && (req.method === 'GET' || req.method === 'HEAD')) return true
|
if (url.pathname === '/api/viewers' && (req.method === 'GET' || req.method === 'HEAD')) return true
|
||||||
if (url.pathname === '/api/viewers/event' && req.method === 'POST') return true
|
if (url.pathname === '/api/viewers/event' && req.method === 'POST') return true
|
||||||
if (url.pathname === '/api/lastfm/song-of-day' && (req.method === 'GET' || req.method === 'HEAD')) return true
|
if (url.pathname === '/api/song-of-day' && (req.method === 'GET' || req.method === 'HEAD')) return true
|
||||||
|
|
||||||
if (req.method !== 'GET' && req.method !== 'HEAD') return false
|
if (req.method !== 'GET' && req.method !== 'HEAD') return false
|
||||||
|
|
||||||
@@ -228,7 +226,7 @@ function apiGuard(env) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const url = new URL(req.url, 'http://localhost')
|
const url = new URL(req.url, 'http://localhost')
|
||||||
if (url.pathname === '/api/lastfm/song-of-day' && req.method === 'GET') {
|
if (url.pathname === '/api/song-of-day' && req.method === 'GET') {
|
||||||
try {
|
try {
|
||||||
const result = await devSongOfTheDay(env)
|
const result = await devSongOfTheDay(env)
|
||||||
res.writeHead(result.status, {
|
res.writeHead(result.status, {
|
||||||
|
|||||||
Reference in New Issue
Block a user