Skip to content

Commit

Permalink
finish refactor for better UI, implement better generator loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Cybergenik committed Mar 26, 2022
1 parent feaea13 commit 83be2be
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
56 changes: 36 additions & 20 deletions bot/runemaster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
35 changes: 17 additions & 18 deletions bot/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')

Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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

4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requests
discord.py>=1.3.4
riotwatcher
playwright
playwright>=1.20
python-dotenv
cachetools>=5.0.0
cachetools>=5.0.0

0 comments on commit 83be2be

Please sign in to comment.