|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import asyncpg |
| 5 | +import aiohttp |
| 6 | + |
| 7 | +# --- Configuration --- |
| 8 | +PG_USER = "postgres" |
| 9 | +PG_PASS = "postgres" |
| 10 | +PG_HOST = "localhost" |
| 11 | +PG_PORT = "5432" |
| 12 | +DB_NAME = "lingo_ai" |
| 13 | +API_URL = "http://localhost:8001/meetings/" |
| 14 | +SQL_QUERY = 'SELECT "accessToken", "refreshToken", "botName" FROM bot;' |
| 15 | +PG_CONN_STRING = f"postgresql://{PG_USER}:{PG_PASS}@{PG_HOST}:{PG_PORT}/{DB_NAME}" |
| 16 | + |
| 17 | + |
| 18 | +# Fetch access tokens asynchronously |
| 19 | +async def fetch_data(): |
| 20 | + try: |
| 21 | + conn = await asyncpg.connect(PG_CONN_STRING) |
| 22 | + rows = await conn.fetch(SQL_QUERY) |
| 23 | + await conn.close() |
| 24 | + return [ |
| 25 | + { |
| 26 | + "access_token": row["accessToken"], |
| 27 | + "refresh_token": row["refreshToken"], |
| 28 | + "bot_name": row["botName"], |
| 29 | + } |
| 30 | + for row in rows |
| 31 | + ] |
| 32 | + except Exception as e: |
| 33 | + print(f"[ERROR] Database error: {e}") |
| 34 | + return [] |
| 35 | + |
| 36 | + |
| 37 | +# Make one request |
| 38 | +async def make_request(session, data): |
| 39 | + headers = { |
| 40 | + "Authorization": f"Bearer {data["access_token"]}", |
| 41 | + "Content-Type": "application/json", |
| 42 | + } |
| 43 | + body = {"refresh_token": data["refresh_token"], "bot_name": data["bot_name"]} |
| 44 | + try: |
| 45 | + async with session.get( |
| 46 | + API_URL, headers=headers, json=body, timeout=10 |
| 47 | + ) as response: |
| 48 | + status = response.status |
| 49 | + # text = await response.text() |
| 50 | + print(f"[INFO] Token: {data['bot_name']}: -> Status: {status}") |
| 51 | + except Exception as e: |
| 52 | + print(f"[ERROR] Token {data['bot_name']}: failed: {e}") |
| 53 | + |
| 54 | + |
| 55 | +# Main async workflow |
| 56 | +async def main(): |
| 57 | + tokens = await fetch_data() |
| 58 | + async with aiohttp.ClientSession() as session: |
| 59 | + tasks = [make_request(session, token) for token in tokens] |
| 60 | + await asyncio.gather(*tasks) |
| 61 | + |
| 62 | + |
| 63 | +# Entry point |
| 64 | +if __name__ == "__main__": |
| 65 | + asyncio.run(main()) |
0 commit comments