db struktur
This commit is contained in:
@@ -11,6 +11,62 @@ from pathlib import Path
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
SUPPORTED = {'.mp3', '.flac', '.m4a', '.ogg', '.wav', '.aiff', '.wma'}
|
||||
import uuid as _uuid_module
|
||||
|
||||
|
||||
def _find_or_create_song_conn(conn, title, artist, album, bpm,
|
||||
duration_sec, mbid, acoustid) -> str:
|
||||
"""Find eller opret sang via eksisterende forbindelse."""
|
||||
if mbid:
|
||||
row = conn.execute("SELECT id FROM songs WHERE mbid=?", (mbid,)).fetchone()
|
||||
if row:
|
||||
return row["id"]
|
||||
if acoustid:
|
||||
row = conn.execute("SELECT id FROM songs WHERE acoustid=?", (acoustid,)).fetchone()
|
||||
if row:
|
||||
if mbid:
|
||||
conn.execute("UPDATE songs SET mbid=? WHERE id=? AND mbid IS NULL", (mbid, row["id"]))
|
||||
return row["id"]
|
||||
if title:
|
||||
row = conn.execute(
|
||||
"SELECT id FROM songs WHERE title=? AND artist=?", (title, artist)
|
||||
).fetchone()
|
||||
if row:
|
||||
if mbid:
|
||||
conn.execute("UPDATE songs SET mbid=? WHERE id=? AND mbid IS NULL", (mbid, row["id"]))
|
||||
return row["id"]
|
||||
new_id = str(_uuid_module.uuid4())
|
||||
conn.execute(
|
||||
"INSERT INTO songs (id, title, artist, album, bpm, duration_sec, mbid, acoustid) "
|
||||
"VALUES (?,?,?,?,?,?,?,?)",
|
||||
(new_id, title, artist, album, bpm, duration_sec, mbid or None, acoustid or None)
|
||||
)
|
||||
return new_id
|
||||
|
||||
|
||||
def _upsert_file_conn(conn, song_id, local_path, file_format,
|
||||
file_modified_at, extra_tags) -> str:
|
||||
"""Opret eller opdater fil-post via eksisterende forbindelse."""
|
||||
existing = conn.execute(
|
||||
"SELECT id FROM files WHERE local_path=?", (local_path,)
|
||||
).fetchone()
|
||||
if existing:
|
||||
conn.execute("""
|
||||
UPDATE files SET song_id=?, file_missing=0,
|
||||
file_format=?, file_modified_at=?, extra_tags=?
|
||||
WHERE id=?
|
||||
""", (song_id, file_format, file_modified_at, extra_tags, existing["id"]))
|
||||
return existing["id"]
|
||||
else:
|
||||
file_id = str(_uuid_module.uuid4())
|
||||
conn.execute(
|
||||
"INSERT INTO files (id, song_id, local_path, file_format, file_modified_at, extra_tags) "
|
||||
"VALUES (?,?,?,?,?,?)",
|
||||
(file_id, song_id, local_path, file_format, file_modified_at, extra_tags)
|
||||
)
|
||||
return file_id
|
||||
|
||||
|
||||
|
||||
|
||||
def is_supported(path) -> bool:
|
||||
@@ -33,7 +89,6 @@ def scan_library(library_id: int, library_path: str, db_path: str,
|
||||
"""
|
||||
import sqlite3
|
||||
from local.tag_reader import read_tags
|
||||
from local.local_db import find_or_create_song, upsert_file
|
||||
|
||||
base = Path(library_path)
|
||||
if not base.exists():
|
||||
@@ -84,16 +139,16 @@ def scan_library(library_id: int, library_path: str, db_path: str,
|
||||
acoustid = tags.get("acoustid", "")
|
||||
duration_sec = tags.get("duration_sec", 0)
|
||||
file_format = tags.get("file_format", fp.suffix.lstrip(".").lower())
|
||||
extra_tags = tags.get("extra_tags", "{}")
|
||||
import json as _json
|
||||
_extra = tags.get("extra_tags", {})
|
||||
extra_tags = _json.dumps(_extra) if isinstance(_extra, dict) else (_extra or "{}")
|
||||
|
||||
# Find eller opret sang i global katalog
|
||||
song_id = find_or_create_song(
|
||||
title=title, artist=artist, album=album,
|
||||
bpm=bpm, duration_sec=duration_sec,
|
||||
mbid=mbid, acoustid=acoustid,
|
||||
# Find eller opret sang — alt via samme conn
|
||||
song_id = _find_or_create_song_conn(
|
||||
conn, title, artist, album, bpm, duration_sec, mbid, acoustid
|
||||
)
|
||||
|
||||
# Opdater BPM på sangen hvis vi har bedre data
|
||||
# Opdater BPM
|
||||
if bpm and bpm > 0:
|
||||
conn.execute(
|
||||
"UPDATE songs SET bpm=? WHERE id=? AND (bpm=0 OR bpm IS NULL)",
|
||||
@@ -101,13 +156,7 @@ def scan_library(library_id: int, library_path: str, db_path: str,
|
||||
)
|
||||
|
||||
# Opret eller opdater fil-post
|
||||
upsert_file(
|
||||
song_id=song_id,
|
||||
local_path=path_str,
|
||||
file_format=file_format,
|
||||
file_modified_at=mtime,
|
||||
extra_tags=extra_tags,
|
||||
)
|
||||
_upsert_file_conn(conn, song_id, path_str, file_format, mtime, extra_tags)
|
||||
|
||||
# Dans-tags fra fil
|
||||
file_dances = tags.get("dances", [])
|
||||
|
||||
Reference in New Issue
Block a user