From 83be2bea5c2324d8b3c54c6837365a81ef64cbbf Mon Sep 17 00:00:00 2001 From: Luciano Date: Sat, 26 Mar 2022 04:16:53 -0600 Subject: [PATCH] finish refactor for better UI, implement better generator loading --- bot/runemaster.py | 56 ++++++++++++++++++++++++++++++----------------- bot/screenshot.py | 35 ++++++++++++++--------------- requirements.txt | 4 ++-- 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/bot/runemaster.py b/bot/runemaster.py index 8a5c103..930f1ba 100644 --- a/bot/runemaster.py +++ b/bot/runemaster.py @@ -27,7 +27,7 @@ async def on_message(message): await message.channel.send('Ready for the Rift? type >commands for a list of all commands') elif re.search('^>[a-zA-Z]', message.content, flags=re.IGNORECASE): _in = message.content.split(' ', 1) - command = _in[0].lkower() + command = _in[0].lower() if len(_in) == 1: #region Generic commands if command == ">hello": @@ -77,9 +77,9 @@ async def on_message(message): else: await message.channel.send("That ranked tier does not exist, type >tiers for all the tiers.") elif command == '>info': - champ = CHAMPS.real_champ(name=args) - if champ is not None: - info = CHAMPS.get_champ(champ=champ) + name = CHAMPS.real_champ(name=args) + if name is not None: + info = CHAMPS.get_champ(champ=name) response = Embed( title = f"__{info.champ} | {info.title}__", description = info.desc, @@ -94,38 +94,54 @@ async def on_message(message): elif command == '>runes': await message.channel.send("Fetching Rune Data...") name = CHAMPS.real_champ(name=args) - if name is not None: - file = File(fp=await BROWSER.get_cached_screenshot(name=name, action="runes"), filename=f'{name}.png') - await message.channel.send(f"__{args.capitalize()} Runes__",file=file) - else: + if name is None: await message.channel.send("That champ does not exist") + return + im_bytes = await BROWSER.get_cached_screenshot(name=name, action="runes") + if im_bytes is None: + await message.channel.send("Unable to gather runes data :c") + return + file = File(fp=im_bytes, filename=f'{name}.png') + await message.channel.send(f"__{args.capitalize()} Runes__",file=file) elif command == '>build': await message.channel.send("Fetching Build Data...") name = CHAMPS.real_champ(name=args) - if name is not None: - file = File(fp=await BROWSER.get_cached_screenshot(name=name, action="build"), filename=f'{name}.png') - await message.channel.send(f"__{args.capitalize()} Build__",file=file) - else: + if name is None: await message.channel.send("That champ does not exist") + return + im_bytes = await BROWSER.get_cached_screenshot(name=name, action="build") + if im_bytes is None: + await message.channel.send("Unable to gather build data :c") + return + file = File(fp=im_bytes, filename=f'{name}.png') + await message.channel.send(f"__{args.capitalize()} Build__",file=file) elif command == '>skills' or command == '>abilities' or command == ">spells": await message.channel.send("Fetching Skills Data...") name = CHAMPS.real_champ(name=args) - if name is not None: - file = File(fp=await BROWSER.get_cached_screenshot(name=name, action="skills"), filename=f'{name}.png') - await message.channel.send(f"__{args.capitalize()} Skills__",file=file) - else: + if name is None: await message.channel.send("That champ does not exist") + return + im_bytes = await BROWSER.get_cached_screenshot(name=name, action="skills") + if im_bytes is None: + await message.channel.send("Unable to gather skills data :c") + return + file = File(fp=im_bytes, filename=f'{name}.png') + await message.channel.send(f"__{args.capitalize()} Skills__",file=file) elif command == '>stats': await message.channel.send("Fetching Stats Data...") name = CHAMPS.real_champ(name=args) - if name is not None: - file = File(fp=await BROWSER.get_cached_screenshot(name=name, action="stats"), filename=f'{name}.png') - await message.channel.send(f"__{args.capitalize()} Stats__",file=file) - else: + if name is None: await message.channel.send("That champ does not exist") + return + im_bytes = await BROWSER.get_cached_screenshot(name=name, action="stats") + if im_bytes is None: + await message.channel.send("Unable to gather stats data :c") + return + file = File(fp=im_bytes, filename=f'{name}.png') + await message.channel.send(f"__{args.capitalize()} Stats__",file=file) elif command == '>summon': await message.channel.send("Fetching Summoner data...") args = args.split(" ", 1) diff --git a/bot/screenshot.py b/bot/screenshot.py index 272f243..0b59101 100644 --- a/bot/screenshot.py +++ b/bot/screenshot.py @@ -5,21 +5,19 @@ from cachetools import TTLCache from datetime import datetime, timedelta -async def init_browser(): - async with async_playwright() as p: - browser = await p.chromium.launch() - return await browser.new_page() - class Browser: """Singleton class to represent Playwright Browser for screenshots""" def __init__(self): - self.__browser = init_browser + self.__pw = None self.__cache = TTLCache(maxsize=500, ttl=timedelta(hours=6), timer=datetime.now) async def __new_page(self) -> Coroutine[Any, Any, Page]: """Creates and returns new page, caller assumes ownership and must close page""" try: - return await self.__browser.__anext__() + if not self.__pw: + self.__pw = await async_playwright().start() + self.__browser = await self.__pw.chromium.launch() + return await self.__browser.new_page() except Exception as e: print(f'Unable to get new page: \n {e}') @@ -40,16 +38,16 @@ async def __cached_screenshot(self, name:str, action:str) -> BytesIO: await page.goto(url) if action == "runes": await page.set_viewport_size({"width": 734, "height": 607}) - await page.click("body > div.l-wrap.l-wrap--champion > div.l-container > div > div.tabWrap._recognized > div.l-champion-statistics-content.tabItems > div.tabItem.Content.championLayout-overview > div > div.l-champion-statistics-content__main > div > table") + await page.locator("//html/body/div[1]/div[5]/div[1]/table[3]").click() elif action == "build": await page.set_viewport_size({"width": 734, "height": 667}) - await page.click("body > div.l-wrap.l-wrap--champion > div.l-container > div > div.tabWrap._recognized > div.l-champion-statistics-content.tabItems > div.tabItem.Content.championLayout-overview > div > div.l-champion-statistics-content__main > table:nth-child(2)") + await page.locator("//html/body/div[1]/div[5]/div[1]/table[2]").click() elif action == "skills": await page.set_viewport_size({"width": 734, "height": 340}) - await page.click("body > div.l-wrap.l-wrap--champion > div.l-container > div > div.tabWrap._recognized > div.l-champion-statistics-content.tabItems > div.tabItem.Content.championLayout-overview > div > div.l-champion-statistics-content__main > table.champion-overview__table.champion-overview__table--summonerspell") + await page.locator("//html/body/div[1]/div[5]/div[1]/table[1]").click() elif action == "stats": await page.set_viewport_size({"width": 1200, "height": 265}) - await page.click("body > div.l-wrap.l-wrap--champion > div.l-container > div > div.l-champion-statistics-header") + await page.locator("//html/body/div[1]/div[1]/div[1]").click() else: return None except Exception as e: @@ -70,19 +68,19 @@ async def get_screenshot(self, name:str, action:str, prefix: str) -> BytesIO: await page.goto(url) if action == "matches": await page.set_viewport_size({"width": 690, "height": 1250}) - await page.click("#SummonerLayoutContent > div.tabItem.Content.SummonerLayoutContent.summonerLayout-summary > div.RealContent > div > div.Content") + await page.locator("/html/body/div[1]/div[5]/div[2]").click() elif action == "soloranked_matches": - await page.click("#right_gametype_soloranked > a") + await page.locator("/html/body/div[1]/div[5]/div[2]/div[1]/ul/li[2]/button").click() await page.set_viewport_size({"width": 690, "height": 1250}) - await page.click("#SummonerLayoutContent > div.tabItem.Content.SummonerLayoutContent.summonerLayout-summary > div.RealContent > div > div.Content") + await page.locator("/html/body/div[1]/div[5]/div[2]").click() elif action == "flexranked_matches": - await page.click("#right_gametype_flexranked > a") + await page.locator("/html/body/div[1]/div[5]/div[2]/div[1]/ul/li[3]/button").click() await page.set_viewport_size({"width": 690, "height": 1250}) - await page.click("#SummonerLayoutContent > div.tabItem.Content.SummonerLayoutContent.summonerLayout-summary > div.RealContent > div > div.Content") + await page.locator("/html/body/div[1]/div[5]/div[2]").click() elif action == "leaderboard": - await page.click("body > div.l-wrap.l-wrap--summoner > div.l-menu > ul > li:nth-child(6) > a") + #await page.click("body > div.l-wrap.l-wrap--summoner > div.l-menu > ul > li:nth-child(6) > a") await page.set_viewport_size({"width": 970, "height": 391}) - await page.click("body > div.l-wrap.l-wrap--ranking > div.l-container > div.LadderRankingLayoutWrap > div > div > div > div.ranking-highest") + await page.click("/html/body/div[1]/div[5]/div[2]") else: return None except Exception as e: @@ -93,3 +91,4 @@ async def get_screenshot(self, name:str, action:str, prefix: str) -> BytesIO: await page.close() screenshot_bytes.seek(0) return screenshot_bytes + diff --git a/requirements.txt b/requirements.txt index 26208aa..35ac93c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ requests discord.py>=1.3.4 riotwatcher -playwright +playwright>=1.20 python-dotenv -cachetools>=5.0.0 \ No newline at end of file +cachetools>=5.0.0