diff --git a/components/ExportPlaylistWindow.py b/components/ExportPlaylistWindow.py index 592f31d..6a282f2 100644 --- a/components/ExportPlaylistWindow.py +++ b/components/ExportPlaylistWindow.py @@ -12,7 +12,7 @@ from PyQt5.QtWidgets import ( ) from PyQt5.QtGui import QFont from configparser import ConfigParser -from utils import get_id3_tags, get_reorganize_vars +from utils import get_reorganize_vars from components import ErrorDialog import DBA import os @@ -22,13 +22,15 @@ class ExportPlaylistWindow(QDialog): def __init__(self): super(ExportPlaylistWindow, self).__init__() self.setWindowTitle("Export playlist") + self.setMinimumSize(600, 400) config = ConfigParser() config.read("config.ini") - self.relative_path = config.get("directories", "playlist_relative_path") - self.export_path = config.get("directories", "playlist_export_path") - self.selected_playlist_name = "my-playlist.m3u" - self.current_m3u_path = self.export_path - self.current_relative_path = self.relative_path + self.relative_path: str = config.get("directories", "playlist_relative_path") + self.export_path: str = config.get("directories", "playlist_export_path") + self.selected_playlist_name: str = "my-playlist.m3u" + self.current_m3u_path: str = self.export_path + self.current_relative_path: str = self.relative_path + self.chosen_list_widget_item: QListWidgetItem | None = None layout = QVBoxLayout() # Header label @@ -39,7 +41,7 @@ class ExportPlaylistWindow(QDialog): # Get playlists from db playlist_dict = {} with DBA.DBAccess() as db: - data = db.query("SELECT id, name from playlist;", ()) + data = db.query("SELECT id, name from playlist ORDER BY id ASC;", ()) for row in data: playlist_dict[row[0]] = row[1] @@ -47,7 +49,7 @@ class ExportPlaylistWindow(QDialog): self.item_dict = {} self.playlist_listWidget = QListWidget(self) for i, (k, v) in enumerate(playlist_dict.items()): - # item_text = f"{i} | {v}" + # item_text = f"{i+1} | {v}" item_text = v item = QListWidgetItem(item_text) self.playlist_listWidget.addItem(item) @@ -87,45 +89,64 @@ class ExportPlaylistWindow(QDialog): # Signals self.save_button.clicked.connect(self.save) self.checkbox.toggled.connect(self.relative_path_input.setEnabled) - self.playlist_listWidget.currentItemChanged.connect( + self.playlist_listWidget.currentRowChanged.connect( self.set_current_selected_playlist ) self.setLayout(layout) self.show() def set_current_selected_playlist(self) -> None: - self.selected_playlist_name = ( - self.playlist_listWidget.selectedItems()[0].text() + ".m3u" + """ + Sets the current playlist name, then edits the playlist export path + """ + # Get the current chosen list item + self.chosen_list_widget_item: QListWidgetItem | None = ( + self.playlist_listWidget.currentItem() ) - print(self.selected_playlist_name) + # We don't care if its None + if self.chosen_list_widget_item is None: + return + + # Create the filename for the playlist to be exported + self.selected_playlist_name = self.chosen_list_widget_item.text() + ".m3u" + + # Change line edit text for playlist path + current_text: list = self.m3u_path_input.text().split("/") + if "." in current_text[-1]: + current_text = current_text[:-1] + else: + current_text = current_text + m3u_text = os.path.join("/", *current_text, self.selected_playlist_name) + self.m3u_path_input.setText(m3u_text) def save(self) -> None: """Exports the chosen database playlist to a .m3u file""" - selected_items = [ - item.text() for item in self.playlist_listWidget.selectedItems() - ] - selected_db_ids = [self.item_dict[item] for item in selected_items] + if self.chosen_list_widget_item is None: + return + selected_db_id = self.item_dict[self.chosen_list_widget_item.text()] relative_path = self.relative_path_input.text() m3u_path = self.m3u_path_input.text() + + # If no output path is provided, just close the window... if m3u_path == "" or m3u_path is None: self.close() return - for playlist in selected_db_ids: - try: - with DBA.DBAccess() as db: - db_paths = db.query( - "SELECT s.filepath FROM song_playlist as sp JOIN song as s ON s.id = sp.song_id WHERE sp.playlist_id = ?;", - (playlist,), - )[0] - except Exception as e: - logging.error( - f"ExportPlaylistWindow.py save() | could not retrieve playlist songs: {e}" - ) - error_dialog = ErrorDialog( - "Could not get songs from this playlist. noooooo" - ) - error_dialog.exec() - self.close() + try: + with DBA.DBAccess() as db: + db_paths = db.query( + "SELECT s.filepath FROM song_playlist as sp JOIN song as s ON s.id = sp.song_id WHERE sp.playlist_id = ?;", + (selected_db_id,), + )[0] + except Exception as e: + logging.error( + f"ExportPlaylistWindow.py save() | could not retrieve playlist songs: {e}" + ) + error_dialog = ErrorDialog( + f"Could not get songs from this playlist. noooooo: {e}" + ) + error_dialog.exec() + self.close() + # Gather playlist song paths write_paths = [] if self.checkbox.isChecked: @@ -140,9 +161,12 @@ class ExportPlaylistWindow(QDialog): # Normal paths for song in db_paths: write_paths.append(song) + # Write playlist file - for line in write_paths: - pass + filename = self.m3u_path_input.text() + os.makedirs(os.path.dirname(filename), exist_ok=True) + with open(filename, "w") as f: + f.writelines(write_paths) self.close() def cancel(self) -> None: