Efficient database updating

This commit is contained in:
Keith Edmunds 2021-04-05 13:20:28 +01:00
parent 8ccded2a57
commit 6d6327ae18
2 changed files with 38 additions and 25 deletions

View File

@ -188,6 +188,12 @@ class Tracks(Base):
ERROR(f"Can't find track id {id}") ERROR(f"Can't find track id {id}")
return None return None
@staticmethod
def get_all_paths():
"Return a list of paths of all tracks"
return [a[0] for a in session.query(Tracks.path).all()]
@staticmethod @staticmethod
def get_path(id): def get_path(id):
try: try:

57
app/songdb.py Normal file → Executable file
View File

@ -30,6 +30,26 @@ def main():
INFO("Finished") INFO("Finished")
def add_path_to_db(path):
"Add passed path to database along with metadata"
track = Tracks.get_or_create(path)
tag = TinyTag.get(path)
audio = get_audio_segment(path)
track.title = tag.title
track.artist = tag.artist
track.duration = int(round(tag.duration,
Config.MILLISECOND_SIGFIGS) * 1000)
track.start_gap = leading_silence(audio)
track.fade_at = round(fade_point(audio) / 1000,
Config.MILLISECOND_SIGFIGS) * 1000
track.silence_at = round(trailing_silence(audio) / 1000,
Config.MILLISECOND_SIGFIGS) * 1000
track.mtime = os.path.getmtime(path)
session.commit()
def get_audio_segment(path): def get_audio_segment(path):
try: try:
if path.endswith('.mp3'): if path.endswith('.mp3'):
@ -94,40 +114,27 @@ def trailing_silence(audio_segment, silence_threshold=-50.0,
def update_db(): def update_db():
""" """
Repopulate database Repopulate database
TODO: remove missing files
""" """
count = 0 # Search for tracks in only one of directory and database
db_paths = set(Tracks.get_all_paths())
os_paths_list = []
for root, dirs, files in os.walk(Config.ROOT): for root, dirs, files in os.walk(Config.ROOT):
for f in files: for f in files:
count += 1
path = os.path.join(root, f) path = os.path.join(root, f)
ext = os.path.splitext(f)[1] ext = os.path.splitext(f)[1]
if ext in [".flac", ".mp3"]: if ext in [".flac", ".mp3"]:
track = Tracks.get_or_create(path) os_paths_list.append(path)
tag = TinyTag.get(path) os_paths = set(os_paths_list)
audio = get_audio_segment(path)
track.title = tag.title for path in list(db_paths - os_paths):
track.artist = tag.artist INFO(f"To remove from database: {path}")
track.duration = int(round(
tag.duration, Config.MILLISECOND_SIGFIGS
) * 1000)
track.start_gap = leading_silence(audio)
track.fade_at = round(
fade_point(audio) / 1000, Config.MILLISECOND_SIGFIGS
) * 1000
track.silence_at = round(
trailing_silence(audio) / 1000, Config.MILLISECOND_SIGFIGS
) * 1000
track.mtime = os.path.getmtime(path)
session.commit()
elif ext not in [".jpg"]: for path in list(os_paths - db_paths):
INFO(f"Unrecognised file type: {path}") INFO(f"Adding to datbase: {path}")
add_path_to_db(path)
INFO(f"{count} files processed")
if __name__ == '__main__': if __name__ == '__main__':