aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--server/index.ts39
2 files changed, 38 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 6d9634c..b66d881 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,7 @@ notes.txt
playlist*.js
urls*.txt
urls*.js
+server/data/daily-songs.json
# JavaScript
# dependencies
diff --git a/server/index.ts b/server/index.ts
index c223861..0bcfed6 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -1,4 +1,6 @@
import express from 'express';
+import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
+import path from 'path';
import { songs } from './data/songs';
import cors from 'cors';
import dotenv from 'dotenv';
@@ -10,6 +12,12 @@ app.use(express.json());
const SERVER_PORT = process.env.SERVER_PORT || 3001;
const SALT = process.env.VITE_HEARDLE_SALT ?? 'changeme';
+const DAILY_SONGS_FILE = path.resolve(
+ process.env.DAILY_SONGS_FILE ?? 'server/data/daily-songs.json',
+);
+
+type Song = (typeof songs)[number];
+type DailySongs = Record<string, Song>;
function getObfuscationKey(): Buffer {
const date = new Date().toISOString().split('T')[0];
@@ -35,11 +43,38 @@ function hashString(str: string): number {
return hash;
}
-app.get('/today', (_req, res) => {
- const date = getUtcDate();
+function readDailySongs(): DailySongs {
+ if (!existsSync(DAILY_SONGS_FILE)) {
+ return {};
+ }
+
+ return JSON.parse(readFileSync(DAILY_SONGS_FILE, 'utf8')) as DailySongs;
+}
+
+function writeDailySongs(dailySongs: DailySongs): void {
+ mkdirSync(path.dirname(DAILY_SONGS_FILE), { recursive: true });
+ writeFileSync(DAILY_SONGS_FILE, JSON.stringify(dailySongs, null, 2));
+}
+
+function getDailySong(date: string): Song {
+ const dailySongs = readDailySongs();
+ const savedSong = dailySongs[date];
+
+ if (savedSong) {
+ return savedSong;
+ }
+
const seed = hashString(date);
const index = seed % songs.length;
const song = songs[index];
+ dailySongs[date] = song;
+ writeDailySongs(dailySongs);
+ return song;
+}
+
+app.get('/today', (_req, res) => {
+ const date = getUtcDate();
+ const song = getDailySong(date);
const obfuscationKey = getObfuscationKey();
const songJson = JSON.stringify(song);
const obfuscatedData = xorBuffer(Buffer.from(songJson, 'utf8'), obfuscationKey);
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage