DiscordPY и Pyppeteer - PullRequest
       29

DiscordPY и Pyppeteer

0 голосов
/ 19 июня 2020

Итак, мне нужно python файлов, бота Discord и другого python скрипта, который просто посещает страницу с помощью pyppeteer. Когда команда запускается на боте Discord, я хочу, чтобы веб-страница была посещена, но я хочу продолжить выполнение других команд на боте после этого, поэтому я хотел бы сделать это асинхронно.

соответствующий bot.py

import controller

@bot.command()
@commands.cooldown(1, 300, commands.BucketType.member)
async def function(ctx):
    #verification code
    await controller.run(link)

соответствующий controller.py

 async def open(link, proxy):
    browser = await launch({'args': ['--proxy-server='+proxy],
                            'headless': True,
                            'handleSIGINT' : False,
                            'handleSIGTERM' : False,
                            'handleSIGHUP' : False})

    page = await browser.newPage()
    try:
        await page.goto(link, timeout=120000)
        await browser.close()
        return True
    except:
        await browser.close()
        return False

async def func(proxies, link, runs):
    completed = 0
    while completed<runs:
        complete = await open(link, completed, proxies[completed])
        if complete:
            completed +=1

async def run(link):
    proxies_selected = [list of proxies to use]
    await func(proxies_selected, link,  4)

Однако я получаю ошибки, ниже - вся трассировка стека.

[E:pyppeteer.connection] connection unexpectedly closed
Task exception was never retrieved
future: <Task finished coro=<Connection._async_send() done, defined at C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyppeteer\connection.py:69> exception=InvalidStateError('invalid state')>
Traceback (most recent call last):
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 827, in transfer_data
    message = await self.read_message()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 895, in read_message
    frame = await self.read_data_frame(max_size=self.max_size)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 971, in read_data_frame
    frame = await self.read_frame(max_size)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 1051, in read_frame
    extensions=self.extensions,
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\framing.py", line 105, in read
    data = await reader(2)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\asyncio\streams.py", line 679, in readexactly
    await self._wait_for_data('readexactly')
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\asyncio\streams.py", line 473, in _wait_for_data
    await self._waiter
concurrent.futures._base.CancelledError

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

Traceback (most recent call last):
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyppeteer\connection.py", line 73, in _async_send
    await self.connection.send(msg)
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 555, in send
    await self.ensure_open()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\websockets\protocol.py", line 812, in ensure_open
    raise self.connection_closed_exc()
websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyppeteer\connection.py", line 79, in _async_send
    await self.dispose()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyppeteer\connection.py", line 170, in dispose
    await self._on_close()
  File "C:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyppeteer\connection.py", line 153, in _on_close
    f'Protocol error {cb.method}: Target closed.',  # type: ignore
asyncio.base_futures.InvalidStateError: invalid state

Спасибо

...