kramcat / CharacterAI

Unofficial Python API for character.ai
https://docs.kram.cat
MIT License
378 stars 51 forks source link

Error occurs if there is a wait time on the site? #23

Closed hexteeth closed 1 year ago

hexteeth commented 1 year ago

I haven't been able to get my code to work yet fully, if I run it it opens up the correct chat in the browser, but before the page loads the code shows errors. I think this may be caused by the fact that usually there is a wait time for the Character AI site and that might be throwing it off? It could be something else though. Here are the error messages it displays:

/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/unix_events.py:878: RuntimeWarning: A loop is being detached from a child watcher with pending handlers RuntimeWarning) Traceback (most recent call last): File "/Users/hexgeissinger/Desktop/caitest.py", line 39, in asyncio.run(main()) File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run return loop.run_until_complete(main) File "/Applications/Thonny.app/Contents/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 587, in run_until_complete return future.result() File "/Users/hexgeissinger/Desktop/caitest.py", line 15, in main chat = await client.chat.get_chat(char) File "/Users/hexgeissinger/Library/Python/3.7/lib/python/site-packages/characterai/pyasynccai.py", line 369, in get_chat token=token File "/Users/hexgeissinger/Library/Python/3.7/lib/python/site-packages/characterai/pyasynccai.py", line 57, in PostResponse await goto(link, wait=wait, token=token) File "/Users/hexgeissinger/Library/Python/3.7/lib/python/site-packages/characterai/pyasynccai.py", line 38, in goto raise errors.NoResponse('The Site is Overloaded') AttributeError: module 'characterai.errors' has no attribute 'NoResponse'

UPDATE: I just ran it when there wasn't a wait time on the website and it worked fine. what should I do so that the code will know to wait until the page loads properly?

KubaPro010 commented 1 year ago

It's a error where kramcat forgot to define "NoResponse", you either can define it in the "errors.py", or just wrap the function in a try-catch

hexteeth commented 1 year ago

okay i figured out what to do, you need to change the line in characterai.py (line 38) where it calls the error in the first place and tell it to wait instead. calling the error ends the execution of the program but if you change it to:

if page.title() != 'Waiting Room powered by Cloudflare':
    return page
else:
    if wait:
        page.wait_for_selector(
            'div#wrapper', state='detached', timeout=0
        )
        goto(link=link, wait=wait)
    else:
        #raise errors.NoResponse('The Site is Overloaded')
        page.wait_for_selector(
            'div#wrapper', state='detached', timeout=0
        )
        goto(link=link, wait=wait)

instead then it will just wait until the wait time is over and then load like normal. I am very new to python so if there is a better way to do this please let me know!

KubaPro010 commented 11 months ago

It's inefficient, you can either paste class NoResponse(PyCAIError): pass at the end of the errors.py or just do this:

if page.title() != 'Waiting Room powered by Cloudflare':
    return page
else:
    page.wait_for_selector(
        'div#wrapper', state='detached', timeout=0
    )
    goto(link=link, wait=wait)