Implementacija obveščanja preko ntfy.sh #12

This commit is contained in:
2026-03-21 22:52:32 +01:00
parent e57727ba0b
commit e49142ea6f
6 changed files with 93 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
import sqlite3
import json
import os
import urllib.request
DB_PATH = 'songs.db'
SETTINGS_PATH = 'settings.json'
@@ -34,6 +35,29 @@ conn.commit()
# POMOŽNE FUNKCIJE
# ---------------------------------------------------------------------------
def send_ntfy_notification(song_id, title, lyrics):
"""
Pošlje obvestilo o spremembi besedila preko ntfy.sh.
"""
topic = settings.get("ntfy_topic", "").strip()
if not topic:
return
label = settings.get("installation_label", "Projekcija")
message = f"{song_id}\n{title}\n{lyrics}"
try:
url = f"https://ntfy.sh/{topic}"
req = urllib.request.Request(
url,
data=message.encode('utf-8'),
method='POST'
)
with urllib.request.urlopen(req, timeout=5) as response:
pass
except Exception as e:
print(f"Napaka pri pošiljanju ntfy obvestila: {e}")
def get_next_free_id(start: int = 792) -> int:
"""Vrne prvo prosto številko pesmi, ki je ≥ start (privzeto 792)."""
next_id = start
@@ -125,6 +149,7 @@ def dodaj_pesem():
cursor.execute("INSERT OR REPLACE INTO songs (id, title, lyrics) VALUES (?,?,?)",
(song_id, title, full_lyrics))
conn.commit()
send_ntfy_notification(song_id, title, full_lyrics)
print("Pesem uspešno shranjena!")
def uredi_pesem():
@@ -162,6 +187,7 @@ def uredi_pesem():
cursor.execute("UPDATE songs SET title = ?, lyrics = ? WHERE id = ?",
(new_title, new_lyrics, song_id))
conn.commit()
send_ntfy_notification(song_id, new_title, new_lyrics)
print("Pesem uspešno posodobljena!")
def prestavi_pesem():
@@ -200,6 +226,7 @@ def prestavi_pesem():
(new_id, vir[0], vir[1]))
cursor.execute("DELETE FROM songs WHERE id = ?", (old_id,))
conn.commit()
send_ntfy_notification(new_id, vir[0], vir[1])
print("Pesem uspešno prestavljena!")
def izbrisi_pesem():
@@ -320,6 +347,7 @@ def uvozi_bazo():
# -- shranjevanje pesmi ---------------------------------------------
cursor.execute("INSERT OR REPLACE INTO songs (id, title, lyrics) VALUES (?,?,?)",
(song_id, title, lyrics))
send_ntfy_notification(song_id, title, lyrics)
conn.commit()
print("\nUvoz pesmi uspešen!")