Fix UberEats task stderr
This commit is contained in:
@@ -164,7 +164,7 @@ class FlanaBot(Connect4Bot, PenaltyBot, PollBot, ScraperBot, UberEatsBot, Weathe
|
||||
if message.chat.config[config_name]:
|
||||
await self.start_ubereats(message.chat)
|
||||
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()})"
|
||||
else:
|
||||
button_text = config_name
|
||||
|
||||
@@ -20,6 +20,8 @@ from flanabot.models import Chat, Message
|
||||
class UberEatsBot(MultiBot, ABC):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.playwright: playwright.async_api.Playwright | None = None
|
||||
self.browser: playwright.async_api.Browser | None = None
|
||||
self.tasks = {}
|
||||
|
||||
# -------------------------------------------------------- #
|
||||
@@ -30,13 +32,18 @@ class UberEatsBot(MultiBot, ABC):
|
||||
|
||||
self.register(self._on_ubereats, 'ubereats', priority=2)
|
||||
|
||||
@staticmethod
|
||||
async def _scrape_code(chat: Chat) -> str:
|
||||
with flanautils.suppress_low_level_stderr():
|
||||
async with playwright.async_api.async_playwright() as playwright_:
|
||||
async with await playwright_.chromium.launch() as browser:
|
||||
context: playwright.async_api.BrowserContext = await browser.new_context()
|
||||
async def _close_playwright(self):
|
||||
if self.browser:
|
||||
await self.browser.close()
|
||||
if self.playwright:
|
||||
await self.playwright.stop()
|
||||
|
||||
async def _scrape_code(self, chat: Chat) -> str:
|
||||
self.playwright = await playwright.async_api.async_playwright().start()
|
||||
self.browser = await self.playwright.chromium.launch()
|
||||
context: playwright.async_api.BrowserContext = await self.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')
|
||||
|
||||
@@ -66,6 +73,8 @@ class UberEatsBot(MultiBot, ABC):
|
||||
chat.ubereats_cookies = await context.cookies('https://www.myunidays.com')
|
||||
chat.save()
|
||||
|
||||
await self._close_playwright()
|
||||
|
||||
return code
|
||||
|
||||
# ---------------------------------------------- #
|
||||
@@ -113,12 +122,14 @@ class UberEatsBot(MultiBot, ABC):
|
||||
chat.config['ubereats'] = True
|
||||
chat.save()
|
||||
if (task := self.tasks.get(chat.id)) and not task.done():
|
||||
await self._close_playwright()
|
||||
task.cancel()
|
||||
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.save()
|
||||
if (task := self.tasks.get(chat.id)) and not task.done():
|
||||
await self._close_playwright()
|
||||
task.cancel()
|
||||
del self.tasks[chat.id]
|
||||
|
||||
Reference in New Issue
Block a user