diff --git a/components/MusicTable.py b/components/MusicTable.py index 715038e..55580ef 100644 --- a/components/MusicTable.py +++ b/components/MusicTable.py @@ -4,7 +4,6 @@ from PyQt5.QtWidgets import QTableView from PyQt5.QtCore import QTimer from tinytag import TinyTag from utils import add_files_to_library -from utils import get_id3_tags import logging @@ -57,9 +56,6 @@ class MusicTable(QTableView): """Sets the filepath of the currently selected song""" self.selected_song_filepath = self.currentIndex().siblingAtColumn(self.headers.index('path')).data() print(f'Selected song: {self.selected_song_filepath}') - print('TAGS:') - print(get_id3_tags(self.selected_song_filepath)) - print('END TAGS') def set_current_song_filepath(self): """Sets the filepath of the currently playing/chosen song""" diff --git a/main.py b/main.py index c1b1976..9846ac7 100644 --- a/main.py +++ b/main.py @@ -69,7 +69,7 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): self.nextButton.clicked.connect(self.on_next_clicked) # Click to next song self.actionPreferences.triggered.connect(self.actionPreferencesClicked) # Open preferences menu self.actionScanLibraries.triggered.connect(self.scan_libraries) # Scan library - self.actionClearDatabase.triggered.connect(initialize_library_database) # Clear database + self.actionClearDatabase.triggered.connect(self.clear_database) # Clear database ## tableView # self.tableView.clicked.connect(self.set_clicked_cell_filepath) self.tableView.doubleClicked.connect(self.play_audio_file) # Double click to play song @@ -194,7 +194,11 @@ class ApplicationWindow(QMainWindow, Ui_MainWindow): def scan_libraries(self): scan_for_music() - # refresh datatable + self.tableView.fetch_library() + + def clear_database(self): + initialize_library_database() + self.tableView.fetch_library() def process_probe(self, buff): buff.startTime() diff --git a/utils/__init__.py b/utils/__init__.py index acb4c5b..fe8a054 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -1,3 +1,4 @@ +from .safe_get import safe_get from .get_id3_tags import get_id3_tags from .initialize_library_database import initialize_library_database from .scan_for_music import scan_for_music diff --git a/utils/add_files_to_library.py b/utils/add_files_to_library.py index e60948b..19cd891 100644 --- a/utils/add_files_to_library.py +++ b/utils/add_files_to_library.py @@ -1,6 +1,7 @@ import DBA from configparser import ConfigParser from utils import get_id3_tags +from utils import safe_get config = ConfigParser() config.read("config.ini") @@ -11,37 +12,47 @@ def add_files_to_library(files): `files` | list() | List of fully qualified paths to audio file(s) Returns a count of records added """ - print(f'utils | adding files to library: {files}') - extensions = config.get('settings', 'extensions').split(',') - insert_data = [] # To store data for batch insert - for file in files: - if any(file.lower().endswith(ext) for ext in extensions): - filename = file.split("/")[-1] - audio = get_id3_tags(file) + print(f"utils | adding files to library: {files}") + extensions = config.get("settings", "extensions").split(",") + insert_data = [] # To store data for batch insert + for filepath in files: + if any(filepath.lower().endswith(ext) for ext in extensions): + filename = filepath.split("/")[-1] + audio = get_id3_tags(filepath) + if "title" not in audio: + return # Append data tuple to insert_data list insert_data.append( ( - file, - audio.title, - audio.album, - audio.artist, - audio.genre, + filepath, + safe_get(audio, "title", [])[0], + safe_get(audio, "album", [])[0] if "album" in audio else None, + safe_get(audio, "artist", [])[0] if "artist" in audio else None, + ",".join(safe_get(audio, "genre", [])) + if "genre" in audio + else None, filename.split(".")[-1], - audio.year, - audio.bitrate, + safe_get(audio, "date", [])[0] if "date" in audio else None, + safe_get(audio, "bitrate", [])[0] if "birate" in audio else None, ) ) # Check if batch size is reached if len(insert_data) >= 1000: with DBA.DBAccess() as db: - db.executemany('INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', insert_data) + db.executemany( + "INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + insert_data, + ) insert_data = [] # Reset the insert_data list # Insert any remaining data if insert_data: with DBA.DBAccess() as db: - db.executemany('INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', insert_data) + db.executemany( + "INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + insert_data, + ) return True diff --git a/utils/get_id3_tags.py b/utils/get_id3_tags.py index 6037290..359dbbd 100644 --- a/utils/get_id3_tags.py +++ b/utils/get_id3_tags.py @@ -11,11 +11,8 @@ def get_id3_tags(file): """ try: audio = EasyID3(file) + print(f'ID3 Tags: {audio}') return audio except Exception as e: print(f"Error: {e}") - return {} - -filepath = '/home/billy/Music/songs/meanings/blah99/H.mp3' -id3_tags = get_id3_tags(filepath) -print(id3_tags) \ No newline at end of file + return {} \ No newline at end of file diff --git a/utils/safe_get.py b/utils/safe_get.py new file mode 100644 index 0000000..dcbb9d4 --- /dev/null +++ b/utils/safe_get.py @@ -0,0 +1,3 @@ +# Define a function to safely access dictionary keys +def safe_get(dictionary, key, default=None): + return dictionary.get(key, default) \ No newline at end of file diff --git a/utils/scan_for_music.py b/utils/scan_for_music.py index cdb80b5..b07c22b 100644 --- a/utils/scan_for_music.py +++ b/utils/scan_for_music.py @@ -2,14 +2,15 @@ import os import DBA from configparser import ConfigParser from utils import get_id3_tags +from utils import safe_get config = ConfigParser() -config.read('config.ini') +config.read("config.ini") def scan_for_music(): - root_dir = config.get('directories', 'library') - extensions = config.get('settings', 'extensions').split(',') + root_dir = config.get("directories", "library") + extensions = config.get("settings", "extensions").split(",") insert_data = [] # To store data for batch insert for dirpath, dirnames, filenames in os.walk(root_dir): @@ -17,31 +18,41 @@ def scan_for_music(): if any(filename.lower().endswith(ext) for ext in extensions): filepath = os.path.join(dirpath, filename) audio = get_id3_tags(filepath) - + if "title" not in audio: + return + # Append data tuple to insert_data list - insert_data.append(( - filepath, - audio.title, - audio.album, - audio.artist, - audio.genre, - filename.split('.')[-1], - audio.year, - audio.bitrate - )) + insert_data.append( + ( + filepath, + safe_get(audio, "title", [])[0], + safe_get(audio, "album", [])[0] if "album" in audio else None, + safe_get(audio, "artist", [])[0] if "artist" in audio else None, + ",".join(safe_get(audio, "genre", [])) if "genre" in audio else None, + filename.split(".")[-1], + safe_get(audio, "date", [])[0] if "date" in audio else None, + safe_get(audio, "bitrate", [])[0] if "birate" in audio else None, + ) + ) # Check if batch size is reached if len(insert_data) >= 1000: with DBA.DBAccess() as db: - db.executemany('INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', insert_data) + db.executemany( + "INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + insert_data, + ) insert_data = [] # Reset the insert_data list # Insert any remaining data if insert_data: with DBA.DBAccess() as db: - db.executemany('INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', insert_data) + db.executemany( + "INSERT OR IGNORE INTO library (filepath, title, album, artist, genre, codec, album_date, bitrate) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + insert_data, + ) + - # id int unsigned auto_increment, # title varchar(255), # album varchar(255), @@ -52,4 +63,4 @@ def scan_for_music(): # bitrate int unsigned, # date_added TIMESTAMP default CURRENT_TIMESTAMP, -# scan_for_music(config.get('directories', 'library1')) \ No newline at end of file +# scan_for_music(config.get('directories', 'library1'))