Genteki / pyvts

A python library for interacting with the VTube Studio API
https://genteki.github.io/pyvts/
MIT License
73 stars 12 forks source link

Websocket is close #49

Open Guevin1 opened 2 months ago

Guevin1 commented 2 months ago

code:

async def main():
    vts = pyvts.vts(plugin_info=plugin_info)
    await vts.connect()
    await vts.request_authenticate_token()  # get token
    await vts.request_authenticate()  # use token
    new_parameter_name = "start_parameter"
    await vts.request(
        vts.vts_request.requestCustomParameter(new_parameter_name)
    )
    global MyVts, character_id, emotions
    MyVts = vts

    while True:
        character_new = await MyVts.request(MyVts.vts_request.BaseRequest("CurrentModelRequest"))
        character_new = character_new["data"]

        if character_new["modelID"] != character_id:
            response_data = await vts.request(vts.vts_request.requestHotKeyList())
            hotkey_list = []
            for hotkey in response_data['data']['availableHotkeys']:
                hotkey_list.append(hotkey['name'])
            emotions = hotkey_list
            character_id = character_new["modelID"]
            print("new vtuber's avatar")
            print(emotions)
        inp = input("Эмоция: ")
        if inp == "stop":
            break

        await emotion(inp)

async def emotion(name):
    ID = 0
    if name in emotions:
        ID = emotions.index(name)
        print("emotion found")
    send_hotkey_request = MyVts.vts_request.requestTriggerHotKey(emotions[ID])
    await MyVts.request(send_hotkey_request) # send request to play 'My Animation 1'

Error

Traceback (most recent call last):
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 1192, in drain
    await self._drain()
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 334, in _drain
    raise exc
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 963, in transfer_data
    message = await self.read_message()
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 1033, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 1108, in read_data_frame
    frame = await self.read_frame(max_size)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 1165, in read_frame
    frame = await Frame.read(
            ^^^^^^^^^^^^^^^^^
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/framing.py", line 68, in read
    data = await reader(2)
           ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/streams.py", line 741, in readexactly
    raise self._exception
  File "/usr/lib/python3.12/asyncio/selector_events.py", line 1075, in write
    n = self._sock.send(data)
        ^^^^^^^^^^^^^^^^^^^^^
BrokenPipeError: [Errno 32] Broken pipe

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/guevin1/Рабочий стол/Проекты/vtuber/emotion.py", line 52, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/guevin1/Рабочий стол/Проекты/vtuber/emotion.py", line 40, in main
    await emotion(inp)
  File "/home/guevin1/Рабочий стол/Проекты/vtuber/emotion.py", line 49, in emotion
    await MyVts.request(send_hotkey_request) # send request to play 'My Animation 1'
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/guevin1/.local/lib/python3.12/site-packages/pyvts/vts.py", line 113, in request
    response_msg = await self.websocket.recv()
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 568, in recv
    await self.ensure_open()
  File "/home/guevin1/.local/lib/python3.12/site-packages/websockets/legacy/protocol.py", line 939, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: no close frame received or sent
CheapNightbot commented 1 month ago

Hi, I am getting the following error while trying to run the code your provided:

None
Traceback (most recent call last):
  File "...\main.py", line 53, in <module>
    asyncio.run(main())
  File "...\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "...\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "...\main.py", line 29, in main
    if character_new["modelID"] != character_id:
                                   ^^^^^^^^^^^^
NameError: name 'character_id' is not defined. Did you mean: 'character_new'?

By the way, how are you calling your main function? Will you mind providing the full code so that I can run and debug ?

Also, from the error you've provided, it seems like the websocket connection timeout / closing by the time you are calling await MyVts.request(send_hotkey_request): websockets.exceptions.ConnectionClosedError. But not sure. 😓