Identify database id of selected song

This commit is contained in:
Keith Edmunds 2021-03-21 17:11:53 +00:00
parent 09d56d1c27
commit dab05c079a

65
app.py
View File

@ -5,9 +5,9 @@ import vlc
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pydub import AudioSegment from pydub import AudioSegment
from PyQt5.QtCore import QEvent from PyQt5.QtCore import QEvent, Qt
from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox from PyQt5.QtWidgets import QApplication, QDialog, QMainWindow, QMessageBox
from PyQt5.QtWidgets import QTableWidgetItem, QFileDialog from PyQt5.QtWidgets import QTableWidgetItem, QFileDialog, QListWidgetItem
from PyQt5.uic import loadUi from PyQt5.uic import loadUi
from threading import Timer from threading import Timer
from time import sleep from time import sleep
@ -24,22 +24,6 @@ import sqlalchemy
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
# "Constants"
DISPLAY_SQL = False
MYSQL_CONNECT = "mysql+mysqldb://songdb:songdb@localhost/songdb"
# Declare mapping
Base = declarative_base()
engine = sqlalchemy.create_engine(f"{MYSQL_CONNECT}?charset=utf8",
encoding='utf-8',
echo=DISPLAY_SQL)
Base.metadata.create_all(engine)
# Create a Session
Session = sessionmaker(bind=engine)
session = Session()
class RepeatedTimer: class RepeatedTimer:
def __init__(self, interval, function, *args, **kwargs): def __init__(self, interval, function, *args, **kwargs):
@ -47,7 +31,7 @@ class RepeatedTimer:
self.interval = interval self.interval = interval
self.function = function self.function = function
self.args = args self.args = args
self.kwargs = kwargsdlg_search_database_ui self.kwargs = kwargs
self.is_running = False self.is_running = False
self.start() self.start()
@ -66,6 +50,7 @@ class RepeatedTimer:
self._timer.cancel() self._timer.cancel()
self.is_running = False self.is_running = False
class Track: class Track:
def __init__(self, path): def __init__(self, path):
self.path = path self.path = path
@ -85,7 +70,7 @@ class Track:
f" length={self.length}" f" length={self.length}"
f" start_gap={self.start_gap}" f" start_gap={self.start_gap}"
f" fade_at={self.fade_at}" f" fade_at={self.fade_at}"
f" silence_at={sedlg_search_database_uilf.silence_at}" f" silence_at={self.silence_at}"
f" title={self.title}" f" title={self.title}"
f" artist={self.artist}" f" artist={self.artist}"
) )
@ -150,7 +135,6 @@ class Track:
ROOT = "/home/kae/music/" ROOT = "/home/kae/music/"
class Window(QMainWindow, Ui_MainWindow): class Window(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None): def __init__(self, parent=None):
super().__init__(parent) super().__init__(parent)
@ -206,7 +190,7 @@ class Window(QMainWindow, Ui_MainWindow):
dlg = QFileDialog() dlg = QFileDialog()
dlg.setFileMode(QFileDialog.ExistingFile) dlg.setFileMode(QFileDialog.ExistingFile)
dlg.setViewMode(QFileDialog.Detail) dlg.setViewMode(QFileDialog.Detail)
Gdlg.setDirectory(ROOT) dlg.setDirectory(ROOT)
dlg.setNameFilter("Music files (*.flac *.mp3)") dlg.setNameFilter("Music files (*.flac *.mp3)")
if dlg.exec_(): if dlg.exec_():
@ -234,22 +218,22 @@ class DbDialog(QDialog):
self.ui = Ui_Dialog() self.ui = Ui_Dialog()
self.ui.setupUi(self) self.ui.setupUi(self)
self.ui.lineEdit.textEdited.connect(self.chars_typed) self.ui.lineEdit.textEdited.connect(self.chars_typed)
# import ipdb; ipdb.set_trace() self.ui.listWidget.itemDoubleClicked.connect(self.listdclick)
# self.lineEdit.clicked.connect(self.selectFile)
def chars_typed(self, s): def chars_typed(self, s):
print(f"chars_typed({s})")
if len(s) >= 3: if len(s) >= 3:
# matches = Tracks.search_titles(s) matches = Tracks.search_titles(s)
matches = session.query(Tracks).filter(
Tracks.title.ilike(f"%{s}%")).all()
if matches: if matches:
# import ipdb; ipdb.set_trace()
songs = [t.title for t in matches]
self.ui.listWidget.clear() self.ui.listWidget.clear()
self.ui.listWidget.addItems(songs) for track in matches:
t = QListWidgetItem()
t.setText(track.title)
t.setData(Qt.UserRole, track.id)
self.ui.listWidget.addItem(t)
def listdclick(self, entry):
import ipdb; ipdb.set_trace()
print(f"clicked entry id={entry.data(Qt.UserRole)}")
if __name__ == "__main__": if __name__ == "__main__":
app = QApplication(sys.argv) app = QApplication(sys.argv)
@ -262,8 +246,8 @@ if __name__ == "__main__":
# #
# #
# #
## @tl.job(interval=timedelta(seconds=1)) # # @tl.job(interval=timedelta(seconds=1))
#def update_progress(player, talk_at, silent_at): # def update_progress(player, talk_at, silent_at):
# elapsed_time = player.get_time() # elapsed_time = player.get_time()
# total_time = player.get_length() # total_time = player.get_length()
# remaining_time = total_time - elapsed_time # remaining_time = total_time - elapsed_time
@ -280,16 +264,17 @@ if __name__ == "__main__":
# , end="\r") # , end="\r")
# #
# #
## Print name of current song, print name of next song. Play current when # # Print name of current song, print name of next song. Play current when
## return pressed, Pri--current-song-output-lengthnt remaining time every # # return pressed, Pri--current-song-output-lengthnt remaining time every
## second. When it ends, print name of new current and next song. # # second. When it ends, print name of new current and next song.
# #
# #
#def test(): # def test():
# track = "wibg.mp3" # track = "wibg.mp3"
# segment = AudioSegment.from_mp3(track) # segment = AudioSegment.from_mp3(trm.ack)
# print(f"Track: {track}") # print(f"Track: {track}")
# print(f"Leading silence: {ms_to_mmss(leading_silence(segment), decimals=1)}") # print(f"Leading silence: {ms_to_mmss(leading_silence(segment),
# decimals=1)}")
# talk_at = significant_fade(segment) # talk_at = significant_fade(segment)
# silent_at = trailing_silence(segment) # silent_at = trailing_silence(segment)
# print(f"Talkover fade: {ms_to_mmss(talk_at)}") # print(f"Talkover fade: {ms_to_mmss(talk_at)}")