From f5ac91b94228d08e0641693dab3111ea93a72c64 Mon Sep 17 00:00:00 2001 From: iou1name Date: Thu, 6 Feb 2025 13:33:06 -0500 Subject: [PATCH] change filepath to relative --- build_library.py | 2 ++ database.py | 1 - fix_fpath.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 fix_fpath.py diff --git a/build_library.py b/build_library.py index eafce89..38ca474 100644 --- a/build_library.py +++ b/build_library.py @@ -20,6 +20,8 @@ def read_track(filepath): """ t = tinytag.TinyTag.get(filepath) + filepath = os.path.relpath(filepath, config.music_dir) + d = { 'filepath': filepath, 'artist': t.artist, diff --git a/database.py b/database.py index 8988e75..76933d2 100644 --- a/database.py +++ b/database.py @@ -74,7 +74,6 @@ async def get_tracks(request, albumartist, album, date): def convert_filepath(path): """Convert a filepath to a URL.""" - path = os.path.relpath(path, config.music_dir) path = config.server_homepage + '/library/' + path #path = parse.quote(path) return path diff --git a/fix_fpath.py b/fix_fpath.py new file mode 100644 index 0000000..d7acdd8 --- /dev/null +++ b/fix_fpath.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +""" +Makes all existing filepaths in the database relative to `config.music_dir`. +""" +import os +import asyncio + +import asyncpg + +import config + + +async def fix_fpaths(): + """ + Makes all existing filepaths in the database relative to + `config.music_dir`. + """ + db_pool = await asyncpg.create_pool(**config.db) + async with db_pool.acquire() as conn: + tracks = await conn.fetch("SELECT filepath FROM track") + + data = [] + for track in tracks: + fpath = track['filepath'] + fpath = os.path.relpath(fpath, config.music_dir) + entry = (fpath, track['filepath']) + data.append(entry) + + stm = await conn.prepare("UPDATE track SET filepath = $1 WHERE filepath = $2") + await stm.executemany(data) + print("Done.") + +if __name__ == '__main__': + asyncio.run(fix_fpaths())