working on exporting playlists

This commit is contained in:
billy 2024-09-01 13:07:19 -04:00
parent 08a4b7fce3
commit 9c00710150
5 changed files with 41 additions and 13 deletions

View File

@ -50,10 +50,12 @@ class AddToPlaylistWindow(QDialog):
self.show()
def save(self):
selected_items = [item.text() for item in self.listWidget.selectedItems()]
selected_db_ids = [self.item_dict[item] for item in selected_items]
for song in self.song_db_ids:
for playlist in selected_db_ids:
selected_playlists = [item.text() for item in self.listWidget.selectedItems()]
selected_playlists_db_ids = [
self.item_dict[item] for item in selected_playlists
]
for playlist in selected_playlists_db_ids:
for song in self.song_db_ids:
try:
with DBA.DBAccess() as db:
db.execute(
@ -62,7 +64,7 @@ class AddToPlaylistWindow(QDialog):
)
except Exception as e:
logging.error(
"AddToPlaylistWindow.py save() | could not insert song into playlist: {e}"
f"AddToPlaylistWindow.py save() | could not insert song into playlist: {e}"
)
self.close()

View File

@ -10,6 +10,7 @@ from PyQt5.QtWidgets import (
QListWidgetItem,
)
from PyQt5.QtGui import QFont
from configparser import ConfigParser
import DBA
@ -17,6 +18,9 @@ class ExportPlaylistWindow(QDialog):
def __init__(self):
super(ExportPlaylistWindow, self).__init__()
self.setWindowTitle("Export playlist")
config = ConfigParser()
config.read("config.ini")
self.export_path = config.get("directories", "playlist_export_path")
layout = QVBoxLayout()
# button_layout = QHBoxLayout()
@ -40,6 +44,15 @@ class ExportPlaylistWindow(QDialog):
layout.addWidget(label)
layout.addWidget(self.listWidget)
# Export path label
label = QLabel("Export path")
label.setFont(QFont("Sans", weight=QFont.Bold)) # bold category
label.setStyleSheet("text-transform:lowercase;") # uppercase category
# Export path
self.input = QLineEdit(self.export_path)
layout.addWidget(self.input)
# Save button
save_button = QPushButton("Export")
save_button.clicked.connect(self.save)
@ -49,19 +62,28 @@ class ExportPlaylistWindow(QDialog):
def save(self) -> None:
"""Exports the chosen database playlist to a .m3u file"""
selected_items = [item.text() for item in self.listWidget.selectedItems()]
selected_db_ids = [self.item_dict[item] for item in selected_items]
value = self.input.text()
if value == "" or value is None:
self.close()
return
else:
for playlist in selected_db_ids:
print(type(playlist))
print(playlist)
try:
with DBA.DBAccess() as db:
db.execute("INSERT INTO playlist (name) VALUES (?);", (value,))
selected_song_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,),
)
except Exception as e:
logging.error(
f"CreatePlaylistWindow.py save() | Could not create playlist: {e}"
f"ExportPlaylistWindow.py save() | could not retrieve playlist songs: {e}"
)
self.close()
# FIXME: make this write to a .m3u file, also
# need to consider relative paths + config for that
self.close()
def cancel(self) -> None:
self.close()

View File

@ -367,6 +367,7 @@ class MusicTable(QTableView):
Loads data into self (QTableView)
Default to loading all songs.
If playlist_id is given, load songs in a particular playlist
playlist_id is emitted from PlaylistsPane as a tuple (1,)
"""
try:
# Loading the table also causes cell data to change, technically
@ -385,18 +386,20 @@ class MusicTable(QTableView):
self.model.clear()
self.model.setHorizontalHeaderLabels(self.table_headers)
if playlist_id:
playlist_id = playlist_id[0]
selected_playlist_id = playlist_id[0]
print(f"selected_playlist_id: {selected_playlist_id}")
# Fetch playlist data
try:
with DBA.DBAccess() as db:
data = db.query(
"SELECT s.id, s.title, s.artist, s.album, s.track_number, s.genre, s.codec, s.album_date, s.filepath FROM song s JOIN song_playlist sp ON s.id = sp.id WHERE sp.id = ?",
(playlist_id,),
"SELECT s.id, s.title, s.artist, s.album, s.track_number, s.genre, s.codec, s.album_date, s.filepath FROM song s JOIN song_playlist sp ON s.id = sp.song_id WHERE sp.playlist_id = ?",
(selected_playlist_id,),
)
except Exception as e:
logging.warning(
f"MusicTable.py | load_music_table | Unhandled exception: {e}"
)
print(f"MusicTable.py | load_music_table | Unhandled exception: {e}")
return
else:
# Fetch library data

View File

@ -38,7 +38,7 @@ class PlaylistsPane(QTreeWidget):
if isinstance(item, PlaylistWidgetItem):
print(f"ID: {item.id}, name: {item.text(0)}")
self.playlist_db_id_choice = item.id
self.playlistChoiceSignal.emit(item.id)
self.playlistChoiceSignal.emit(int(item.id))
elif item.text(0).lower() == "all songs":
self.all_songs_selected()

View File

@ -6,6 +6,7 @@ database = db/library.db
# Useful paths to have stored
library = /path/to/music
reorganize_destination = /where/to/reorganize/to
playlist_export_path = /where/to/export/to
[settings]
# Which file types are scanned