export playlist better yay
This commit is contained in:
parent
d131100876
commit
2705737c42
@ -12,7 +12,7 @@ from PyQt5.QtWidgets import (
|
|||||||
)
|
)
|
||||||
from PyQt5.QtGui import QFont
|
from PyQt5.QtGui import QFont
|
||||||
from configparser import ConfigParser
|
from configparser import ConfigParser
|
||||||
from utils import get_id3_tags, get_reorganize_vars
|
from utils import get_reorganize_vars
|
||||||
from components import ErrorDialog
|
from components import ErrorDialog
|
||||||
import DBA
|
import DBA
|
||||||
import os
|
import os
|
||||||
@ -22,13 +22,15 @@ class ExportPlaylistWindow(QDialog):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(ExportPlaylistWindow, self).__init__()
|
super(ExportPlaylistWindow, self).__init__()
|
||||||
self.setWindowTitle("Export playlist")
|
self.setWindowTitle("Export playlist")
|
||||||
|
self.setMinimumSize(600, 400)
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read("config.ini")
|
config.read("config.ini")
|
||||||
self.relative_path = config.get("directories", "playlist_relative_path")
|
self.relative_path: str = config.get("directories", "playlist_relative_path")
|
||||||
self.export_path = config.get("directories", "playlist_export_path")
|
self.export_path: str = config.get("directories", "playlist_export_path")
|
||||||
self.selected_playlist_name = "my-playlist.m3u"
|
self.selected_playlist_name: str = "my-playlist.m3u"
|
||||||
self.current_m3u_path = self.export_path
|
self.current_m3u_path: str = self.export_path
|
||||||
self.current_relative_path = self.relative_path
|
self.current_relative_path: str = self.relative_path
|
||||||
|
self.chosen_list_widget_item: QListWidgetItem | None = None
|
||||||
layout = QVBoxLayout()
|
layout = QVBoxLayout()
|
||||||
|
|
||||||
# Header label
|
# Header label
|
||||||
@ -39,7 +41,7 @@ class ExportPlaylistWindow(QDialog):
|
|||||||
# Get playlists from db
|
# Get playlists from db
|
||||||
playlist_dict = {}
|
playlist_dict = {}
|
||||||
with DBA.DBAccess() as db:
|
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:
|
for row in data:
|
||||||
playlist_dict[row[0]] = row[1]
|
playlist_dict[row[0]] = row[1]
|
||||||
|
|
||||||
@ -47,7 +49,7 @@ class ExportPlaylistWindow(QDialog):
|
|||||||
self.item_dict = {}
|
self.item_dict = {}
|
||||||
self.playlist_listWidget = QListWidget(self)
|
self.playlist_listWidget = QListWidget(self)
|
||||||
for i, (k, v) in enumerate(playlist_dict.items()):
|
for i, (k, v) in enumerate(playlist_dict.items()):
|
||||||
# item_text = f"{i} | {v}"
|
# item_text = f"{i+1} | {v}"
|
||||||
item_text = v
|
item_text = v
|
||||||
item = QListWidgetItem(item_text)
|
item = QListWidgetItem(item_text)
|
||||||
self.playlist_listWidget.addItem(item)
|
self.playlist_listWidget.addItem(item)
|
||||||
@ -87,45 +89,64 @@ class ExportPlaylistWindow(QDialog):
|
|||||||
# Signals
|
# Signals
|
||||||
self.save_button.clicked.connect(self.save)
|
self.save_button.clicked.connect(self.save)
|
||||||
self.checkbox.toggled.connect(self.relative_path_input.setEnabled)
|
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.set_current_selected_playlist
|
||||||
)
|
)
|
||||||
self.setLayout(layout)
|
self.setLayout(layout)
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
def set_current_selected_playlist(self) -> None:
|
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:
|
def save(self) -> None:
|
||||||
"""Exports the chosen database playlist to a .m3u file"""
|
"""Exports the chosen database playlist to a .m3u file"""
|
||||||
selected_items = [
|
if self.chosen_list_widget_item is None:
|
||||||
item.text() for item in self.playlist_listWidget.selectedItems()
|
return
|
||||||
]
|
selected_db_id = self.item_dict[self.chosen_list_widget_item.text()]
|
||||||
selected_db_ids = [self.item_dict[item] for item in selected_items]
|
|
||||||
relative_path = self.relative_path_input.text()
|
relative_path = self.relative_path_input.text()
|
||||||
m3u_path = self.m3u_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:
|
if m3u_path == "" or m3u_path is None:
|
||||||
self.close()
|
self.close()
|
||||||
return
|
return
|
||||||
for playlist in selected_db_ids:
|
|
||||||
try:
|
try:
|
||||||
with DBA.DBAccess() as db:
|
with DBA.DBAccess() as db:
|
||||||
db_paths = db.query(
|
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 = ?;",
|
"SELECT s.filepath FROM song_playlist as sp JOIN song as s ON s.id = sp.song_id WHERE sp.playlist_id = ?;",
|
||||||
(playlist,),
|
(selected_db_id,),
|
||||||
)[0]
|
)[0]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(
|
logging.error(
|
||||||
f"ExportPlaylistWindow.py save() | could not retrieve playlist songs: {e}"
|
f"ExportPlaylistWindow.py save() | could not retrieve playlist songs: {e}"
|
||||||
)
|
)
|
||||||
error_dialog = ErrorDialog(
|
error_dialog = ErrorDialog(
|
||||||
"Could not get songs from this playlist. noooooo"
|
f"Could not get songs from this playlist. noooooo: {e}"
|
||||||
)
|
)
|
||||||
error_dialog.exec()
|
error_dialog.exec()
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
# Gather playlist song paths
|
# Gather playlist song paths
|
||||||
write_paths = []
|
write_paths = []
|
||||||
if self.checkbox.isChecked:
|
if self.checkbox.isChecked:
|
||||||
@ -140,9 +161,12 @@ class ExportPlaylistWindow(QDialog):
|
|||||||
# Normal paths
|
# Normal paths
|
||||||
for song in db_paths:
|
for song in db_paths:
|
||||||
write_paths.append(song)
|
write_paths.append(song)
|
||||||
|
|
||||||
# Write playlist file
|
# Write playlist file
|
||||||
for line in write_paths:
|
filename = self.m3u_path_input.text()
|
||||||
pass
|
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
||||||
|
with open(filename, "w") as f:
|
||||||
|
f.writelines(write_paths)
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
def cancel(self) -> None:
|
def cancel(self) -> None:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user