From 57d4d1ce5ce282fef5742df7f111c340a00ca235 Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 15 Dec 2022 12:00:01 +0100 Subject: [PATCH] Working on selenium migration --- .vscode/settings.json | 3 +++ bwtbot.py | 18 +++++++++++++----- config.json | 2 +- modules/app.py | 7 +++++++ modules/bwt.py | 41 +++++++++++++++++++++++++---------------- requirements.txt | 3 ++- 6 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 modules/app.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..457f44d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.analysis.typeCheckingMode": "basic" +} \ No newline at end of file diff --git a/bwtbot.py b/bwtbot.py index 47b0ae7..7f8d5f0 100644 --- a/bwtbot.py +++ b/bwtbot.py @@ -1,21 +1,27 @@ #-*- coding: utf-8 -*- import traceback -from pyrogram import Client, filters, idle +from pyrogram import filters, idle from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat from pyrogram.enums.chat_action import ChatAction from functions import * from modules.colors import * from modules.bwt import * +from modules.app import app +from selenium import webdriver +from selenium.webdriver.support.ui import WebDriverWait import subprocess import os config = jsonLoad("config.json") - owner_id = config["owner_id"] -app = Client(config["bot_name"], api_id=config["api_id"], api_hash=config["api_hash"], bot_token=config["bot_token"]) - +driver = webdriver.Remote( + command_executor=config["selenium_address"], + options=webdriver.FirefoxOptions() +) +wait = WebDriverWait(driver, 10) +# driver.execute_script(f"window.open('about:blank', 'home');") @app.on_message(~ filters.scheduled & filters.command(["setcard", "задать карту"], prefixes=["/", ""])) async def setcard(_, msg): @@ -55,7 +61,7 @@ async def balance(_, msg): try: if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]: await app.send_chat_action(chat_id=msg.chat.id, action=ChatAction.TYPING) - water_left = await getWaterLeft(userGet(msg.from_user.id, "card"), msg.from_user.id, app) + water_left = await getWaterLeft(driver, wait, userGet(msg.from_user.id, "card"), msg.from_user.id) if water_left == "": raise EmptyCardException("Card information is empty") elif water_left == "Failure": @@ -109,6 +115,7 @@ pid = os.getpid() async def kill(_, msg): if msg.from_user.id == owner_id: await msg.reply_text(f"Shutting down bot with pid **{pid}**") + driver.quit() os.system(f"kill -9 {pid}") @@ -167,6 +174,7 @@ app.set_bot_commands([ idle() +driver.quit() app.send_message(owner_id, f"Shutting down bot with pid **{pid}**") print(f'\n{nowtime()} {WHITE}Shutting down with PID {YELLOW}{pid}{RESET}') diff --git a/config.json b/config.json index a4835a1..ee6b27a 100644 --- a/config.json +++ b/config.json @@ -5,5 +5,5 @@ "api_hash": "", "bot_token": "", "bot_name": "", - "use_compiled_page_saver": false + "selenium_address": "http://localhost:4444" } \ No newline at end of file diff --git a/modules/app.py b/modules/app.py new file mode 100644 index 0000000..2040868 --- /dev/null +++ b/modules/app.py @@ -0,0 +1,7 @@ +#-*- coding: utf-8 -*- + +from pyrogram.client import Client +from functions import jsonLoad + +config = jsonLoad("config.json") +app = Client(config["bot_name"], api_id=config["api_id"], api_hash=config["api_hash"], bot_token=config["bot_token"]) \ No newline at end of file diff --git a/modules/bwt.py b/modules/bwt.py index c2a563c..99fad57 100644 --- a/modules/bwt.py +++ b/modules/bwt.py @@ -2,7 +2,13 @@ import os import traceback +from typing import Union +from selenium import webdriver +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC from functions import * +from modules.app import app from bs4 import BeautifulSoup config = jsonLoad("config.json") @@ -10,35 +16,38 @@ config = jsonLoad("config.json") class EmptyCardException(Exception): pass -async def getWaterLeft(cardid, filename, app=None): +async def getWaterLeft(driver: webdriver.Remote, wait: WebDriverWait, cardid: Union[str, int], filename: int): url = f"https://bwtaqua.com.ua/card-topup/?id={cardid}" try: - os.system(f'touch data/pages/{str(filename)}.html') - - if config["use_compiled_page_saver"]: - os.system(f'PageSaver/pageSaver "https://bwtaqua.com.ua/card-topup/?id={cardid}" > data/pages/{str(filename)}.html') - else: - os.system(f'node ./PageSaver/pageSaver.js "https://bwtaqua.com.ua/card-topup/?id={cardid}" > data/pages/{str(filename)}.html') + # driver.execute_script(f"window.open('about:blank', '{filename}');") + # driver.switch_to.window(f"{filename}") + driver.get(f"https://bwtaqua.com.ua/card-topup/?id={cardid}") - with open(f'data/pages/{str(filename)}.html') as f: - html_file = f.read() - f.close() + wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'js-payment-balance'))) + + page_source = driver.page_source + + driver.execute_script("window.stop();") + + with open(f'data/pages/{filename}.html', "w", encoding="utf-8") as f: + f.write(page_source) + + output = driver.find_element(By.CLASS_NAME, "js-payment-balance").text.replace("Твій баланс ", "").replace(" л", "") - soup = BeautifulSoup(html_file, 'html.parser') - - output = (soup.find_all("h3", class_="headline headline_center headline_pink js-payment-balance")[0].getText()).replace("Твій баланс ", "").replace(" л", "") + # soup = BeautifulSoup(page_source, 'html.parser') + # output = (soup.find_all("h3", class_="headline headline_center headline_pink js-payment-balance")[0].getText()).replace("Твій баланс ", "").replace(" л", "") - appendLog(f"Parsed {output} liters of water remaining (user: {str(filename)}, cardid: {cardid})") + appendLog(f"Parsed {output} liters of water remaining (user: {filename}, cardid: {cardid})") except Exception as exp: - appendLog(f"Exception occured: {exp} (user: {str(filename)}, cardid: {cardid})") + appendLog(f"Exception occured: {exp} (user: {filename}, cardid: {cardid})") if app != None: - await app.send_message(config["owner_id"], f"**Exception occured:**\n • User: `{str(filename)}`\n • Card: [{cardid}]({url})\n • Exception: `{exp}`\n • Traceback: `{traceback.format_exc()}`", disable_web_page_preview=True) + await app.send_message(config["owner_id"], f"**Exception occured:**\n • User: `{filename}`\n • Card: [{cardid}]({url})\n • Exception: `{exp}`\n • Traceback: `{traceback.format_exc()}`", disable_web_page_preview=True) else: print(f'Exception occured and could not send to user: {exp}') diff --git a/requirements.txt b/requirements.txt index 041f722..8a92f44 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -beautifulsoup4 \ No newline at end of file +beautifulsoup4==4.11.1 +selenium==4.7.2 \ No newline at end of file