|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +from urllib.parse import unquote, urlparse |
| 4 | + |
| 5 | +import aiohttp |
| 6 | +import asyncpg |
| 7 | +from dotenv import load_dotenv |
| 8 | + |
| 9 | +load_dotenv() |
| 10 | + |
| 11 | +# Database settings |
| 12 | +db_settings = { |
| 13 | + "host": os.getenv("POSTGRES_HOST"), |
| 14 | + "port": int(os.getenv("POSTGRES_PORT", 5434)), |
| 15 | + "database": os.getenv("POSTGRES_DB"), |
| 16 | + "user": os.getenv("POSTGRES_USER"), |
| 17 | + "password": os.getenv("POSTGRES_PASSWORD"), |
| 18 | +} |
| 19 | + |
| 20 | +FILEBROWSER_URL = os.getenv("FILEBROWSER_URL") |
| 21 | +FILEBROWSER_USERNAME = os.getenv("FILEBROWSER_USERNAME") |
| 22 | +FILEBROWSER_PASSWORD = os.getenv("FILEBROWSER_PASSWORD") |
| 23 | +FILEBROWSER_UPLOAD_PATH = os.getenv("FILEBROWSER_UPLOAD_PATH", "HBNI-Audio/Recordings") |
| 24 | + |
| 25 | +# Global dict of filebrowser items {name: full_path} |
| 26 | +FILEBROWSER_ITEMS = {} |
| 27 | + |
| 28 | + |
| 29 | +async def get_filebrowser_token(): |
| 30 | + async with aiohttp.ClientSession() as session: |
| 31 | + async with session.post( |
| 32 | + f"{FILEBROWSER_URL}/api/login", |
| 33 | + json={"username": FILEBROWSER_USERNAME, "password": FILEBROWSER_PASSWORD}, |
| 34 | + ) as response: |
| 35 | + token = await response.text() |
| 36 | + return token.strip() |
| 37 | + |
| 38 | + |
| 39 | +async def get_public_share_url(file_relative_path: str, token: str) -> str: |
| 40 | + headers = {"X-Auth": token.strip(), "accept": "*/*"} |
| 41 | + async with aiohttp.ClientSession() as session: |
| 42 | + async with session.post( |
| 43 | + f"{FILEBROWSER_URL}/api/share/{file_relative_path}", |
| 44 | + headers=headers, |
| 45 | + json={"path": f"/{file_relative_path}"}, |
| 46 | + ) as response: |
| 47 | + if response.status != 200: |
| 48 | + body = await response.text() |
| 49 | + raise Exception( |
| 50 | + f"Failed to create share link: {response.status} - {body}" |
| 51 | + ) |
| 52 | + data = await response.json() |
| 53 | + return data["hash"] |
| 54 | + |
| 55 | + |
| 56 | +async def list_filebrowser_items(): |
| 57 | + global FILEBROWSER_ITEMS |
| 58 | + token = await get_filebrowser_token() |
| 59 | + headers = {"X-Auth": token, "Accept": "application/json"} |
| 60 | + |
| 61 | + async with aiohttp.ClientSession() as session: |
| 62 | + async with session.get( |
| 63 | + f"{FILEBROWSER_URL}/api/resources/{FILEBROWSER_UPLOAD_PATH}", |
| 64 | + headers=headers, |
| 65 | + ) as response: |
| 66 | + if response.status != 200: |
| 67 | + body = await response.text() |
| 68 | + raise Exception(f"Failed to list items: {response.status} - {body}") |
| 69 | + data = await response.json() |
| 70 | + |
| 71 | + items = data.get("items", []) |
| 72 | + FILEBROWSER_ITEMS = { |
| 73 | + item["name"]: item["path"].lstrip("/") for item in items if not item["isDir"] |
| 74 | + } |
| 75 | + |
| 76 | + print(f"📂 Loaded {len(FILEBROWSER_ITEMS)} files from FileBrowser.") |
| 77 | + |
| 78 | + |
| 79 | +def extract_filename_from_download_link(download_link: str) -> str: |
| 80 | + path = urlparse(download_link).path |
| 81 | + return unquote(os.path.basename(path)).replace("&", "&").replace("&Amp;", "&") |
| 82 | + |
| 83 | + |
| 84 | +async def list_audioarchives(): |
| 85 | + await list_filebrowser_items() |
| 86 | + token = await get_filebrowser_token() |
| 87 | + pool = await asyncpg.create_pool(**db_settings) |
| 88 | + |
| 89 | + async with pool.acquire() as conn: |
| 90 | + query = """ |
| 91 | + SELECT * FROM audioarchives |
| 92 | + WHERE download_link LIKE '%play_recording%' |
| 93 | + ORDER BY date DESC; |
| 94 | + """ |
| 95 | + rows = await conn.fetch(query) |
| 96 | + print(f"🎵 Found {len(rows)} rows missing share_hash:") |
| 97 | + |
| 98 | + for row in rows: |
| 99 | + download_link = row["download_link"] |
| 100 | + filename = extract_filename_from_download_link(download_link) |
| 101 | + current_hash = row["share_hash"] |
| 102 | + |
| 103 | + # if current_hash: |
| 104 | + # print(f"✅ Already has share_hash: {filename} → {current_hash}") |
| 105 | + # continue |
| 106 | + |
| 107 | + filebrowser_path = FILEBROWSER_ITEMS.get(filename) |
| 108 | + if not filebrowser_path: |
| 109 | + print(f"⚠️ File not found in FileBrowser: {filename}") |
| 110 | + continue |
| 111 | + |
| 112 | + try: |
| 113 | + share_hash = await get_public_share_url(filebrowser_path, token) |
| 114 | + update_query = ( |
| 115 | + "UPDATE audioarchives SET share_hash = $1 WHERE download_link = $2;" |
| 116 | + ) |
| 117 | + await conn.execute(update_query, share_hash, download_link) |
| 118 | + print(f"✅ Added share_hash for: {filename} → {share_hash}") |
| 119 | + except Exception as e: |
| 120 | + print(f"❌ Failed to create share for {filename}: {str(e)}") |
| 121 | + |
| 122 | + await pool.close() |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + asyncio.run(list_audioarchives()) |
0 commit comments