|
12 | 12 | from logging.handlers import RotatingFileHandler |
13 | 13 | from typing import Callable, Literal |
14 | 14 |
|
| 15 | +import asyncpg |
15 | 16 | import requests |
16 | 17 | from dotenv import load_dotenv |
17 | 18 |
|
|
22 | 23 | import synology_uploader |
23 | 24 | import zip_file |
24 | 25 |
|
25 | | -FOLDER_LOCATION: str = os.path.abspath(os.getcwd()).replace("\\", "/") |
| 26 | +load_dotenv() |
26 | 27 |
|
| 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 | +} |
27 | 36 | if not os.path.exists(f"{FOLDER_LOCATION}/CURRENTLY_RECORDING"): |
28 | 37 | os.makedirs(f"{FOLDER_LOCATION}/CURRENTLY_RECORDING") |
29 | 38 |
|
@@ -323,18 +332,45 @@ def run(self): |
323 | 332 | app_log.error(f"Error in run loop: {e}") |
324 | 333 | time.sleep(15) |
325 | 334 |
|
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() |
338 | 374 | def send_notification(self): |
339 | 375 | send_email.send( |
340 | 376 | "HBNI Audio Stream Recorder Started Successfully", |
|
0 commit comments