#!/usr/bin/env python3 """ Load a playlist into the database. """ import os import asyncio import asyncpg import config async def load_playlist(path): """Load a playlist into the database.""" print("Loading:", path) with open(path, 'r') as file: data = file.read().splitlines() data = [line.replace('.flac', '.opus') for line in data] ## not generic ## pname = os.path.basename(path) db_pool = await asyncpg.create_pool(**config.db) async with db_pool.acquire() as conn: pid = await conn.fetchrow( "SELECT id FROM playlist WHERE name = $1", pname) if not pid: pid = await conn.fetchrow( "INSERT INTO playlist (name) VALUES ($1) RETURNING id", pname) pid = pid['id'] data = [(pid, line) for line in data] stmt = await conn.prepare( "INSERT INTO playlist_track (playlist_id, filepath) VALUES ($1, $2)") await stmt.executemany(data) print("Done. Loaded", len(data), "tracks") if __name__ == "__main__": import argparse parser = argparse.ArgumentParser( description="Load a new playlist into the database.") parser.add_argument( "path", help="The filepath of the .m3u file.") args = parser.parse_args() asyncio.run(load_playlist(args.path))