From 3df2438c79672db77c35b7bd30b660564d5647fc Mon Sep 17 00:00:00 2001 From: Myned Date: Tue, 13 Oct 2020 02:23:23 -0400 Subject: [PATCH] Add personal temp notifications --- Pipfile | 1 + src/cogs/weeb.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ src/run.py | 5 ++-- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/cogs/weeb.py diff --git a/Pipfile b/Pipfile index e76ebed..80d024f 100644 --- a/Pipfile +++ b/Pipfile @@ -22,6 +22,7 @@ beautifulsoup4 = "*" requests = "*" html5lib = "*" tldextract = "*" +selenium = "*" [dev-packages] lxml = "*" diff --git a/src/cogs/weeb.py b/src/cogs/weeb.py new file mode 100644 index 0000000..f5910d7 --- /dev/null +++ b/src/cogs/weeb.py @@ -0,0 +1,61 @@ +from selenium.webdriver import Chrome +from selenium.webdriver.chrome.options import Options + +import asyncio +import traceback as tb +from discord.ext import commands as cmds +from utils import utils as u + + +class Weeb(cmds.Cog): + + def __init__(self, bot): + self.bot = bot + self.weebing = False + + with open('id.json') as f: + self.id = int(f.readline()) + print('LOADED : id.json') + + if not self.weebing: + self.weebing = True + self.bot.loop.create_task(self.start()) + print('STARTED : weebing') + + async def refresh(self, browser, urls): + message = '' + + for item, url in urls.items(): + browser.get(url) + status = browser.find_elements_by_css_selector('#addToCartText-product-template')[0].text + + if status != 'SOLD OUT': + message += f'{item} is in stock!\n{url}\n' + + return message + + async def start(self): + try: + opts = Options() + opts.headless = True + browser = Chrome(executable_path='/usr/bin/chromedriver', options=opts) + urls = { + 'Novelties': 'https://switchmod.net/collections/ended-gbs/products/gmk-metaverse-2?variant=31671816880208', + 'Royal': 'https://switchmod.net/collections/ended-gbs/products/gmk-metaverse-2?variant=31671816945744' + } + + while self.weebing: + message = await self.refresh(browser, urls) + + if message: + await self.bot.get_user(self.id).send(message) + await self.bot.get_user(u.config['owner_id']).send('Message sent') + + browser.quit() + self.weebing = False + + await asyncio.sleep(60) + + except Exception as e: + tb.print_exc() + await self.bot.get_user(u.config['owner_id']).send(f'! ERROR !\n\n{repr(e)}') diff --git a/src/run.py b/src/run.py index c6046e7..6d910c3 100644 --- a/src/run.py +++ b/src/run.py @@ -32,14 +32,15 @@ bot = cmds.Bot( @bot.event async def on_ready(): if not checks.ready: - from cogs import booru, info, management, owner, tools + from cogs import weeb, booru, info, management, owner, tools for cog in ( tools.Utils(bot), owner.Bot(bot), management.Admin(bot), info.Info(bot), - booru.MsG(bot)): + booru.MsG(bot), + weeb.Weeb(bot)): bot.add_cog(cog) u.cogs[type(cog).__name__] = cog print(f'COG : {type(cog).__name__}')