Osveževanje baze besedil preko web-a #5
This commit is contained in:
81
projector.py
81
projector.py
@@ -11,6 +11,8 @@ import sys
|
|||||||
import ctypes
|
import ctypes
|
||||||
import tkinter.messagebox as messagebox
|
import tkinter.messagebox as messagebox
|
||||||
from web.server import start_server_thread
|
from web.server import start_server_thread
|
||||||
|
import urllib.request
|
||||||
|
import tempfile
|
||||||
|
|
||||||
DB_PATH = 'songs.db'
|
DB_PATH = 'songs.db'
|
||||||
SETTINGS_PATH = 'settings.json'
|
SETTINGS_PATH = 'settings.json'
|
||||||
@@ -50,7 +52,8 @@ class SongProjector:
|
|||||||
"screen_width_percent": 60,
|
"screen_width_percent": 60,
|
||||||
"font_bold": True,
|
"font_bold": True,
|
||||||
"show_song_info": True,
|
"show_song_info": True,
|
||||||
"split_by_stanza": False # TODO: mogoče nekoč (prelom po kitici namesto po višini)
|
"split_by_stanza": False, # TODO: mogoče nekoč (prelom po kitici namesto po višini)
|
||||||
|
"db_update_url": ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if not os.path.exists(SETTINGS_PATH):
|
if not os.path.exists(SETTINGS_PATH):
|
||||||
@@ -302,6 +305,9 @@ class SongProjector:
|
|||||||
subprocess.Popen([sys.executable, os.path.join(BASE_DIR, "add_song.py")]) # fixme: novi proces ne dobi stdin-a; predelati na tkinter aplikacijo?
|
subprocess.Popen([sys.executable, os.path.join(BASE_DIR, "add_song.py")]) # fixme: novi proces ne dobi stdin-a; predelati na tkinter aplikacijo?
|
||||||
self.exit_program()
|
self.exit_program()
|
||||||
return
|
return
|
||||||
|
elif self.song_number == "9901":
|
||||||
|
self.update_songs_database()
|
||||||
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
song_id = int(self.song_number)
|
song_id = int(self.song_number)
|
||||||
@@ -505,6 +511,79 @@ class SongProjector:
|
|||||||
else:
|
else:
|
||||||
self.label.config(text="Ni zadetkov.")
|
self.label.config(text="Ni zadetkov.")
|
||||||
|
|
||||||
|
# ------------------------------------------------------
|
||||||
|
# Posodobitev baze pesmi
|
||||||
|
# ------------------------------------------------------
|
||||||
|
def update_songs_database(self):
|
||||||
|
url = self.settings.get("db_update_url", "").strip()
|
||||||
|
if not url:
|
||||||
|
msg = "URL za posodobitev ni nastavljen v settings.json."
|
||||||
|
print(msg)
|
||||||
|
self.label.config(text=msg)
|
||||||
|
self.song_number = ""
|
||||||
|
return
|
||||||
|
|
||||||
|
msg = f"Prenašam posodobitev iz: {url}..."
|
||||||
|
print(msg)
|
||||||
|
self.label.config(text=msg)
|
||||||
|
self.root.update()
|
||||||
|
|
||||||
|
temp_db_path = None
|
||||||
|
try:
|
||||||
|
# Prenos datoteke
|
||||||
|
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
||||||
|
temp_db_path = tmp_file.name
|
||||||
|
with urllib.request.urlopen(url) as response:
|
||||||
|
tmp_file.write(response.read())
|
||||||
|
|
||||||
|
# Preverjanje integritete
|
||||||
|
print("Preverjam integriteto prenesene baze...")
|
||||||
|
check_conn = sqlite3.connect(temp_db_path)
|
||||||
|
check_cursor = check_conn.cursor()
|
||||||
|
|
||||||
|
# PRAGMA integrity_check
|
||||||
|
check_cursor.execute("PRAGMA integrity_check")
|
||||||
|
integrity_result = check_cursor.fetchone()[0]
|
||||||
|
if integrity_result != "ok":
|
||||||
|
raise Exception(f"Integriteta baze ni OK: {integrity_result}")
|
||||||
|
|
||||||
|
# Preverjanje obstoja tabele songs
|
||||||
|
check_cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='songs'")
|
||||||
|
if not check_cursor.fetchone():
|
||||||
|
raise Exception("Tabela 'songs' ne obstaja v preneseni bazi.")
|
||||||
|
|
||||||
|
# Upsert v trenutno bazo
|
||||||
|
print("Izvajam upsert v lokalno bazo...")
|
||||||
|
check_cursor.execute("SELECT id, title, lyrics FROM songs")
|
||||||
|
new_songs = check_cursor.fetchall()
|
||||||
|
|
||||||
|
upsert_count = 0
|
||||||
|
for song_id, title, lyrics in new_songs:
|
||||||
|
self.cursor.execute("""
|
||||||
|
INSERT INTO songs (id, title, lyrics) VALUES (?, ?, ?)
|
||||||
|
ON CONFLICT(id) DO UPDATE SET title=excluded.title, lyrics=excluded.lyrics
|
||||||
|
""", (song_id, title, lyrics))
|
||||||
|
upsert_count += 1
|
||||||
|
|
||||||
|
self.conn.commit()
|
||||||
|
check_conn.close()
|
||||||
|
|
||||||
|
final_msg = f"Posodobitev uspešna! Posodobljenih/dodanih {upsert_count} pesmi."
|
||||||
|
print(final_msg)
|
||||||
|
self.label.config(text=final_msg)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
error_msg = f"Napaka pri posodobitvi: {e}"
|
||||||
|
print(error_msg)
|
||||||
|
self.label.config(text=error_msg)
|
||||||
|
finally:
|
||||||
|
if temp_db_path and os.path.exists(temp_db_path):
|
||||||
|
try:
|
||||||
|
os.remove(temp_db_path)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
self.song_number = ""
|
||||||
|
|
||||||
# ------------------------------------------------------
|
# ------------------------------------------------------
|
||||||
# Izhod
|
# Izhod
|
||||||
# ------------------------------------------------------
|
# ------------------------------------------------------
|
||||||
|
|||||||
@@ -7,5 +7,6 @@
|
|||||||
"font_bold": false,
|
"font_bold": false,
|
||||||
"show_song_info": true,
|
"show_song_info": true,
|
||||||
"split_by_stanza": false,
|
"split_by_stanza": false,
|
||||||
"web_port": 5000
|
"web_port": 5000,
|
||||||
|
"db_update_url": "https://k5c.korenjak.si/s/dKeE6eXMmBPteQF/download"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user