Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Commit a56bf5d

Browse files
committed
Refactor recording status handling to use PostgreSQL and remove static file paths from configuration
1 parent c0e64df commit a56bf5d

4 files changed

Lines changed: 49 additions & 17 deletions

File tree

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Records broadcasts from the HBNI Audio Streaming Service and send notifications.
1414
2. Create a `.env` file in the root directory of the project with the following variables:
1515

1616
```bash
17-
STATIC_RECORDINGS_PATH="/app/static/Recordings"
18-
RECORDING_STATUS_FILE_PATH="/app/static/recording_status.json"
1917
MINIMUM_RECORDING_LENGTH="10"
2018
LOG_SERVER_HOST="0.0.0.0"
2119
LOG_SERVER_PORT="5054"

dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ EXPOSE 5054
1414
ENV PORT=5054
1515
ENV TZ="America/Guatemala"
1616
ENV PYTHONUNBUFFERED=1
17-
ENV STATIC_RECORDINGS_PATH="/app/static/Recordings"
18-
ENV RECORDING_STATUS_FILE_PATH="/app/static/recording_status.json"
1917
ENV MINIMUM_RECORDING_LENGTH="10"
2018
ENV LOG_SERVER_HOST="0.0.0.0"
2119
ENV LOG_SERVER_PORT="5054"

main.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from logging.handlers import RotatingFileHandler
1313
from typing import Callable, Literal
1414

15+
import asyncpg
1516
import requests
1617
from dotenv import load_dotenv
1718

@@ -22,8 +23,16 @@
2223
import synology_uploader
2324
import zip_file
2425

25-
FOLDER_LOCATION: str = os.path.abspath(os.getcwd()).replace("\\", "/")
26+
load_dotenv()
2627

28+
FOLDER_LOCATION: str = os.path.abspath(os.getcwd()).replace("\\", "/")
29+
db_settings = {
30+
"host": os.getenv("POSTGRES_HOST"),
31+
"port": int(os.getenv("POSTGRES_PORT", 5434)),
32+
"database": os.getenv("POSTGRES_DB"),
33+
"user": os.getenv("POSTGRES_USER"),
34+
"password": os.getenv("POSTGRES_PASSWORD"),
35+
}
2736
if not os.path.exists(f"{FOLDER_LOCATION}/CURRENTLY_RECORDING"):
2837
os.makedirs(f"{FOLDER_LOCATION}/CURRENTLY_RECORDING")
2938

@@ -323,18 +332,45 @@ def run(self):
323332
app_log.error(f"Error in run loop: {e}")
324333
time.sleep(15)
325334

326-
def update_recording_status(self):
327-
recording_status_data: dict[str, dict[str, str]] = {}
328-
for host, stream in self.active_streams.items():
329-
recording_status_data[host] = {
330-
"link": f"https://hbniaudio.hbni.net/{host.replace('/', '')}",
331-
"length": stream.get_time_since_started_recording(),
332-
"description": stream.description,
333-
"starting_time": stream.starting_time.strftime("%B %d %A %Y %I:%M %p"),
334-
}
335-
with open(os.getenv("RECORDING_STATUS_FILE_PATH"), "w") as f:
336-
json.dump(recording_status_data, f)
337-
335+
async def ensure_recording_status_table(self, pool):
336+
async with pool.acquire() as conn:
337+
await conn.execute("""
338+
CREATE TABLE IF NOT EXISTS recording_status (
339+
host TEXT PRIMARY KEY,
340+
link TEXT NOT NULL,
341+
length TEXT NOT NULL,
342+
description TEXT,
343+
starting_time TEXT NOT NULL,
344+
last_updated TIMESTAMPTZ DEFAULT NOW()
345+
);
346+
""")
347+
348+
async def update_recording_status(self):
349+
pool = await asyncpg.create_pool(**db_settings)
350+
351+
# await self.ensure_recording_status_table(pool)
352+
353+
async with pool.acquire() as conn:
354+
async with conn.transaction():
355+
for host, stream in self.active_streams.items():
356+
link = f"https://hbniaudio.hbni.net/{host.replace('/', '')}"
357+
length = stream.get_time_since_started_recording()
358+
description = stream.description
359+
starting_time = stream.starting_time.strftime("%B %d %A %Y %I:%M %p")
360+
361+
query = """
362+
INSERT INTO recording_status (host, link, length, description, starting_time)
363+
VALUES ($1, $2, $3, $4, $5)
364+
ON CONFLICT (host) DO UPDATE
365+
SET link = EXCLUDED.link,
366+
length = EXCLUDED.length,
367+
description = EXCLUDED.description,
368+
starting_time = EXCLUDED.starting_time;
369+
last_updated = NOW();
370+
"""
371+
await conn.execute(query, host, link, length, description, starting_time)
372+
373+
await pool.close()
338374
def send_notification(self):
339375
send_email.send(
340376
"HBNI Audio Stream Recorder Started Successfully",

requirements.txt

36 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)