stable
need run main fetch command then run indexer then LinkerPatternGenerator then link_pattern command in the main
This commit is contained in:
parent
04da522b64
commit
11e3b06824
10 changed files with 918 additions and 1 deletions
80
LocalLibraryIndexer.py
Normal file
80
LocalLibraryIndexer.py
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import os
|
||||
import fnmatch
|
||||
|
||||
import mutagen
|
||||
|
||||
from DataBase import *
|
||||
import eyed3
|
||||
from mutagen.flac import FLAC
|
||||
|
||||
|
||||
music_extensions = ('*.mp3', '*.flac', '*.wav', '*.aac', '*.ogg', '*.m4a')
|
||||
exclude_directories = ("*mary--*", "*example-word*")
|
||||
|
||||
|
||||
def update_local_library(root_dir):
|
||||
old_library = get_data("local_library")
|
||||
known_paths = {track['path']: track_id for track_id, track in old_library.items()}
|
||||
new_library = {}
|
||||
|
||||
song_id = '0'
|
||||
|
||||
def get_new_song_id(prev_id, song_path):
|
||||
if song_path in known_paths:
|
||||
return int(known_paths[song_path])
|
||||
while (prev_id in old_library) or (prev_id in new_library):
|
||||
prev_id = str(int(prev_id) + 1)
|
||||
return prev_id
|
||||
|
||||
for dir_path, dir_names, filenames in os.walk(root_dir):
|
||||
dir_names[:] = [d for d in dir_names if not any(fnmatch.fnmatch(d, exclude) for exclude in exclude_directories)]
|
||||
filtered_filenames = [filename for filename in filenames if any(fnmatch.fnmatch(filename, ext) for ext in music_extensions)]
|
||||
|
||||
for filename in filtered_filenames:
|
||||
song_name, track_type = os.path.splitext(filename)
|
||||
relative_path = os.path.relpath(os.path.join(dir_path, filename), root_dir)
|
||||
song_id = get_new_song_id(song_id, relative_path)
|
||||
new_library[song_id] = {
|
||||
"name": song_name,
|
||||
"path": relative_path,
|
||||
"type": track_type,
|
||||
"artist": "",
|
||||
"album": ""
|
||||
}
|
||||
|
||||
for track_id, track in new_library.items():
|
||||
abs_path = os.path.join(root_dir, track['path'])
|
||||
match track['type']:
|
||||
case ".mp3":
|
||||
mp3track = eyed3.load(abs_path)
|
||||
if not mp3track:
|
||||
print(f"Failed to load mp3 {abs_path} using just name of the file as track name")
|
||||
track['name'] = track['path']
|
||||
else:
|
||||
tag = mp3track.tag
|
||||
if not tag:
|
||||
print(f"Invalid mp3 header {abs_path} using just name of the file as track name")
|
||||
track['name'] = track['path']
|
||||
else:
|
||||
track['artist'] = tag.artist
|
||||
track['name'] = tag.title
|
||||
track['album'] = tag.album
|
||||
|
||||
case ".flac":
|
||||
try:
|
||||
metadata = FLAC(abs_path).tags
|
||||
if 'artist' in metadata:
|
||||
track['artist'] = " ".join(metadata['artist'])
|
||||
if 'TITLE' in metadata:
|
||||
track['name'] = " ".join(metadata['TITLE'])
|
||||
if 'album' in metadata:
|
||||
track['album'] = " ".join(metadata['album'])
|
||||
except mutagen.MutagenError:
|
||||
print(f"Invalid flac header {abs_path} using just name of the file as track name")
|
||||
track['name'] = track['path']
|
||||
|
||||
save_data(new_library, "local_library")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
update_local_library("/home/auser/Music/")
|
||||
Loading…
Add table
Add a link
Reference in a new issue