Efficient database updating
This commit is contained in:
parent
8ccded2a57
commit
6d6327ae18
@ -188,6 +188,12 @@ class Tracks(Base):
|
||||
ERROR(f"Can't find track id {id}")
|
||||
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
|
||||
def get_path(id):
|
||||
try:
|
||||
|
||||
57
app/songdb.py
Normal file → Executable file
57
app/songdb.py
Normal file → Executable file
@ -30,6 +30,26 @@ def main():
|
||||
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):
|
||||
try:
|
||||
if path.endswith('.mp3'):
|
||||
@ -94,40 +114,27 @@ def trailing_silence(audio_segment, silence_threshold=-50.0,
|
||||
def update_db():
|
||||
"""
|
||||
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 f in files:
|
||||
count += 1
|
||||
path = os.path.join(root, f)
|
||||
ext = os.path.splitext(f)[1]
|
||||
if ext in [".flac", ".mp3"]:
|
||||
track = Tracks.get_or_create(path)
|
||||
tag = TinyTag.get(path)
|
||||
audio = get_audio_segment(path)
|
||||
os_paths_list.append(path)
|
||||
os_paths = set(os_paths_list)
|
||||
|
||||
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()
|
||||
for path in list(db_paths - os_paths):
|
||||
INFO(f"To remove from database: {path}")
|
||||
|
||||
elif ext not in [".jpg"]:
|
||||
INFO(f"Unrecognised file type: {path}")
|
||||
|
||||
INFO(f"{count} files processed")
|
||||
for path in list(os_paths - db_paths):
|
||||
INFO(f"Adding to datbase: {path}")
|
||||
add_path_to_db(path)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Loading…
Reference in New Issue
Block a user