import asyncio
import websockets
import json
import time
import argparse
from faker import Faker

fake = Faker("zh_CN")


async def send_messages(stream_id):
    # Construct URL with the provided or generated stream_id
    uri = f"ws://192.168.0.209:9999/ws/live/barrage/{stream_id}?access_token=59c74e75-32d5-4d15-ab99-fde861004502&TENANT-ID=1"

    while True:
        try:
            print(f"Connecting to {uri}...")
            async with websockets.connect(uri) as websocket:
                print(f"Connected to stream: {stream_id}")
                while True:
                    # Generate fake data
                    data = {
                        "content": fake.sentence(),
                        "author": fake.name(),
                        "type": "barrage",
                    }
                    message = json.dumps(data, ensure_ascii=False)

                    # Send message
                    await websocket.send(message)
                    print(f"Sent: {message}")

                    # Wait for 1 second
                    await asyncio.sleep(5)
        except Exception as e:
            print(f"Connection failed or ended: {e}")

        print("Reconnecting in 3 seconds...")
        await asyncio.sleep(3)


if __name__ == "__main__":
    try:
        asyncio.run(send_messages(123456))
    except KeyboardInterrupt:
        print("\nStopped by user")
