Fix UberEats task stderr

This commit is contained in:
AlberLC
2023-03-05 01:03:02 +01:00
parent 81854375d1
commit c17ffe1010
2 changed files with 45 additions and 34 deletions

View File

@@ -164,7 +164,7 @@ class FlanaBot(Connect4Bot, PenaltyBot, PollBot, ScraperBot, UberEatsBot, Weathe
if message.chat.config[config_name]: if message.chat.config[config_name]:
await self.start_ubereats(message.chat) await self.start_ubereats(message.chat)
else: else:
self.stop_ubereats(message.chat) await self.stop_ubereats(message.chat)
button_text = f"ubereats (cada {flanautils.TimeUnits(seconds=message.chat.ubereats_seconds).to_words()})" button_text = f"ubereats (cada {flanautils.TimeUnits(seconds=message.chat.ubereats_seconds).to_words()})"
else: else:
button_text = config_name button_text = config_name

View File

@@ -20,6 +20,8 @@ from flanabot.models import Chat, Message
class UberEatsBot(MultiBot, ABC): class UberEatsBot(MultiBot, ABC):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.playwright: playwright.async_api.Playwright | None = None
self.browser: playwright.async_api.Browser | None = None
self.tasks = {} self.tasks = {}
# -------------------------------------------------------- # # -------------------------------------------------------- #
@@ -30,43 +32,50 @@ class UberEatsBot(MultiBot, ABC):
self.register(self._on_ubereats, 'ubereats', priority=2) self.register(self._on_ubereats, 'ubereats', priority=2)
@staticmethod async def _close_playwright(self):
async def _scrape_code(chat: Chat) -> str: if self.browser:
with flanautils.suppress_low_level_stderr(): await self.browser.close()
async with playwright.async_api.async_playwright() as playwright_: if self.playwright:
async with await playwright_.chromium.launch() as browser: await self.playwright.stop()
context: playwright.async_api.BrowserContext = await browser.new_context()
await context.add_cookies(chat.ubereats_cookies)
page = await context.new_page()
await page.goto('https://www.myunidays.com/ES/es-ES/partners/ubereats/access/online')
if button := await page.query_selector("button[class='button highlight']"): async def _scrape_code(self, chat: Chat) -> str:
await button.click() self.playwright = await playwright.async_api.async_playwright().start()
else: self.browser = await self.playwright.chromium.launch()
await page.click("'Revelar código'") context: playwright.async_api.BrowserContext = await self.browser.new_context()
while len(context.pages) != 2: await context.add_cookies(chat.ubereats_cookies)
await asyncio.sleep(0.5)
page = context.pages[1]
if not (new_code_button := await page.query_selector("button[class='getNewCode button secondary']")): page = await context.new_page()
new_code_button = await page.query_selector("'Obtener nuevo código'") await page.goto('https://www.myunidays.com/ES/es-ES/partners/ubereats/access/online')
if new_code_button and await new_code_button.is_enabled():
await new_code_button.click()
await page.wait_for_load_state('networkidle')
if code_input := await page.query_selector("input[class='code toCopy']"): if button := await page.query_selector("button[class='button highlight']"):
code = await code_input.input_value() await button.click()
else: else:
if button := await page.query_selector("button[class='copy button quarternary']"): await page.click("'Revelar código'")
await button.click() while len(context.pages) != 2:
else: await asyncio.sleep(0.5)
await page.click("'Copiar'") page = context.pages[1]
code = pyperclip.paste()
chat.ubereats_cookies = await context.cookies('https://www.myunidays.com') if not (new_code_button := await page.query_selector("button[class='getNewCode button secondary']")):
chat.save() new_code_button = await page.query_selector("'Obtener nuevo código'")
if new_code_button and await new_code_button.is_enabled():
await new_code_button.click()
await page.wait_for_load_state('networkidle')
return code if code_input := await page.query_selector("input[class='code toCopy']"):
code = await code_input.input_value()
else:
if button := await page.query_selector("button[class='copy button quarternary']"):
await button.click()
else:
await page.click("'Copiar'")
code = pyperclip.paste()
chat.ubereats_cookies = await context.cookies('https://www.myunidays.com')
chat.save()
await self._close_playwright()
return code
# ---------------------------------------------- # # ---------------------------------------------- #
# HANDLERS # # HANDLERS #
@@ -113,12 +122,14 @@ class UberEatsBot(MultiBot, ABC):
chat.config['ubereats'] = True chat.config['ubereats'] = True
chat.save() chat.save()
if (task := self.tasks.get(chat.id)) and not task.done(): if (task := self.tasks.get(chat.id)) and not task.done():
await self._close_playwright()
task.cancel() task.cancel()
self.tasks[chat.id] = await flanautils.do_every(chat.ubereats_seconds, self.send_ubereats_code, chat, do_first_now=send_code_now) self.tasks[chat.id] = await flanautils.do_every(chat.ubereats_seconds, self.send_ubereats_code, chat, do_first_now=send_code_now)
def stop_ubereats(self, chat: Chat): async def stop_ubereats(self, chat: Chat):
chat.config['ubereats'] = False chat.config['ubereats'] = False
chat.save() chat.save()
if (task := self.tasks.get(chat.id)) and not task.done(): if (task := self.tasks.get(chat.id)) and not task.done():
await self._close_playwright()
task.cancel() task.cancel()
del self.tasks[chat.id] del self.tasks[chat.id]