remove unnecessary check for filetype when adding songs to database. fix query params for viewing playlists

This commit is contained in:
billypom on debian 2025-05-04 08:54:37 -04:00
parent 98fd191ef3
commit de260f3786
4 changed files with 80 additions and 56 deletions

View File

@ -674,6 +674,9 @@ class MusicTable(QTableView):
hint: You get a `playlist_id` from the signal emitted from PlaylistsPane as a tuple (1,)
"""
debug(
f"load_music_table() | playlist id: \nTYPE: {type(playlist_id)}\nVALUE: {playlist_id}\n"
)
self.disconnect_data_changed()
self.disconnect_layout_changed()
self.vertical_scroll_position = self.verticalScrollBar().value() # type: ignore
@ -699,12 +702,12 @@ class MusicTable(QTableView):
query = f"{query} WHERE {search_clause};"
else:
query = f"{query} AND {search_clause};"
data = db.query(
query,
(selected_playlist_id, params),
)
data = db.query(query, (selected_playlist_id, params))
else:
data = db.query(query, (selected_playlist_id,))
except Exception as e:
error(f"load_music_table() | Unhandled exception: {e}")
error(f"load_music_table() | Unhandled exception 1: {e}")
return
else: # Load the library
try:
@ -722,7 +725,7 @@ class MusicTable(QTableView):
(params),
)
except Exception as e:
error(f"load_music_table() | Unhandled exception: {e}")
error(f"load_music_table() | Unhandled exception 2: {e}")
return
# Populate the model
row_count: int = 0

View File

@ -1,20 +1,31 @@
from PyQt5.QtWidgets import QAction, QInputDialog, QListWidget, QMenu, QMessageBox, QTreeWidget, QTreeWidgetItem
from PyQt5.QtWidgets import (
QAction,
QInputDialog,
QListWidget,
QMenu,
QMessageBox,
QTreeWidget,
QTreeWidgetItem,
)
from PyQt5.QtCore import pyqtSignal, Qt, QPoint
import DBA
from logging import debug
from components import CreatePlaylistWindow
class PlaylistWidgetItem(QTreeWidgetItem):
def __init__(self, parent, id, name):
super().__init__([name], 0)
self.id = id
# NOTE: ideas:
# auto sort list (alphabetical?)
# reorder list
# duplicate playlist
class PlaylistsPane(QTreeWidget):
playlistChoiceSignal = pyqtSignal(int)
playlistCreatedSignal = pyqtSignal()
@ -35,13 +46,15 @@ class PlaylistsPane(QTreeWidget):
def reload_playlists(self):
"""
Clears and reinitializes the playlists tree
each playlist is a branch/child of root node `Playlists`
each playlist is a branch/child of root node `Playlists`
"""
# take all children away
self._playlists_root.takeChildren()
# NOTE: implement user sorting by adding a column to playlist db table for 'rank' or something
with DBA.DBAccess() as db:
playlists = db.query("SELECT id, name FROM playlist ORDER BY date_created DESC LIMIT 1;", ())
playlists = db.query(
"SELECT id, name FROM playlist ORDER BY date_created DESC LIMIT 1;", ()
)
for playlist in playlists:
branch = PlaylistWidgetItem(self, playlist[0], playlist[1])
self._playlists_root.addChild(branch)
@ -75,14 +88,19 @@ class PlaylistsPane(QTreeWidget):
Asks user for input
Renames selected playlist based on user input
"""
text, ok = QInputDialog.getText(self, "Rename playlist", "New name: ")
text, ok = QInputDialog.getText(
self, "Rename playlist", "New name: "
)
if len(text) > 64:
QMessageBox.warning(self, "WARNING", "Name must not exceed 64 characters")
return
if ok:
with DBA.DBAccess() as db:
db.execute('UPDATE playlist SET name = ? WHERE id = ?;', (text, self.playlist_db_id_choice))
db.execute(
"UPDATE playlist SET name = ? WHERE id = ?;",
(text, self.playlist_db_id_choice),
)
self.reload_playlists()
def delete_playlist(self, *args):
@ -96,23 +114,25 @@ class PlaylistsPane(QTreeWidget):
)
if reply == QMessageBox.Yes:
with DBA.DBAccess() as db:
db.execute("DELETE FROM playlist WHERE id = ?;", (self.playlist_db_id_choice,))
db.execute(
"DELETE FROM playlist WHERE id = ?;", (self.playlist_db_id_choice,)
)
# reload
self.reload_playlists()
def playlist_clicked(self, item):
"""Specific playlist index was clicked"""
"""Specific playlist pane index was clicked"""
if item == self._playlists_root or item == self._library_root:
self.playlist_db_id_choice = None
self.all_songs_selected()
# self.all_songs_selected()
self.allSongsSignal.emit()
elif isinstance(item, PlaylistWidgetItem):
debug(f"ID: {item.id}, name: {item.text(0)}")
self.playlist_db_id_choice = item.id
self.playlistChoiceSignal.emit(int(item.id))
def all_songs_selected(self):
"""Emits a signal to display all songs in the library"""
# I have no idea why this has to be in its own function, but it does
# or else it doesn't work
self.allSongsSignal.emit()
# def all_songs_selected(self):
# """Emits a signal to display all songs in the library"""
# # I have no idea why this has to be in its own function, but it does
# # or else it doesn't work
# self.allSongsSignal.emit()

View File

@ -147,6 +147,7 @@ class PreferencesWindow(QDialog):
self.config.write(configfile)
self.reloadConfigSignal.emit()
self.on_edit_toggled()
# only reload db if we changed the db
if self.current_category_str == "db":
self.reloadDatabaseSignal.emit()

View File

@ -28,48 +28,48 @@ def add_files_to_database(files, progress_callback=None):
config.read(cfg_file)
if not files:
return False, {"Failure": "All operations failed in add_files_to_database()"}
extensions = config.get("settings", "extensions").split(",")
failed_dict = {}
insert_data = [] # To store data for batch insert
for filepath in files:
if any(filepath.lower().endswith(ext) for ext in extensions):
if progress_callback:
progress_callback.emit(filepath)
filename = filepath.split("/")[-1]
try:
progress_callback.emit(filepath)
except Exception:
pass
filename = filepath.split("/")[-1]
tags, details = get_tags(filepath)
if details:
failed_dict[filepath] = details
continue
audio = id3_remap(tags)
tags, details = get_tags(filepath)
if details:
failed_dict[filepath] = details
continue
audio = id3_remap(tags)
# Append data tuple to insert_data list
insert_data.append(
(
filepath,
audio["title"],
audio["album"],
audio["artist"],
audio["track_number"],
audio["genre"],
filename.split(".")[-1],
audio["date"],
audio["bitrate"],
audio["length"],
)
# Append data tuple to insert_data list
insert_data.append(
(
filepath,
audio["title"],
audio["album"],
audio["artist"],
audio["track_number"],
audio["genre"],
filename.split(".")[-1],
audio["date"],
audio["bitrate"],
audio["length"],
)
# Check if batch size is reached
if len(insert_data) >= 1000:
debug(f"inserting a LOT of songs: {len(insert_data)}")
with DBA.DBAccess() as db:
db.executemany(
"INSERT OR IGNORE INTO song (filepath, title, album, artist, track_number, genre, codec, album_date, bitrate, length_seconds) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
insert_data,
)
insert_data = [] # Reset the insert_data list
else:
# continue adding files if we havent reached big length
continue
)
# Check if batch size is reached
if len(insert_data) >= 1000:
debug(f"inserting a LOT of songs: {len(insert_data)}")
with DBA.DBAccess() as db:
db.executemany(
"INSERT OR IGNORE INTO song (filepath, title, album, artist, track_number, genre, codec, album_date, bitrate, length_seconds) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
insert_data,
)
insert_data = [] # Reset the insert_data list
else:
# continue adding files if we havent reached big length
continue
# Insert any remaining data
debug("i check for insert data")
if insert_data: