Working on selenium migration

This commit is contained in:
Profitroll 2022-12-15 12:00:01 +01:00
parent fe1c6984b2
commit 57d4d1ce5c
6 changed files with 51 additions and 23 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"python.analysis.typeCheckingMode": "basic"
}

View File

@ -1,21 +1,27 @@
#-*- coding: utf-8 -*- #-*- coding: utf-8 -*-
import traceback import traceback
from pyrogram import Client, filters, idle from pyrogram import filters, idle
from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat from pyrogram.types import ForceReply, BotCommand, BotCommandScopeChat
from pyrogram.enums.chat_action import ChatAction from pyrogram.enums.chat_action import ChatAction
from functions import * from functions import *
from modules.colors import * from modules.colors import *
from modules.bwt import * from modules.bwt import *
from modules.app import app
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import subprocess import subprocess
import os import os
config = jsonLoad("config.json") config = jsonLoad("config.json")
owner_id = config["owner_id"] 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=["/", ""])) @app.on_message(~ filters.scheduled & filters.command(["setcard", "задать карту"], prefixes=["/", ""]))
async def setcard(_, msg): async def setcard(_, msg):
@ -55,7 +61,7 @@ async def balance(_, msg):
try: try:
if "card" in jsonLoad("data/database.json")[str(msg.from_user.id)]: 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) 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 == "": if water_left == "":
raise EmptyCardException("Card information is empty") raise EmptyCardException("Card information is empty")
elif water_left == "Failure": elif water_left == "Failure":
@ -109,6 +115,7 @@ pid = os.getpid()
async def kill(_, msg): async def kill(_, msg):
if msg.from_user.id == owner_id: if msg.from_user.id == owner_id:
await msg.reply_text(f"Shutting down bot with pid **{pid}**") await msg.reply_text(f"Shutting down bot with pid **{pid}**")
driver.quit()
os.system(f"kill -9 {pid}") os.system(f"kill -9 {pid}")
@ -167,6 +174,7 @@ app.set_bot_commands([
idle() idle()
driver.quit()
app.send_message(owner_id, f"Shutting down bot with pid **{pid}**") app.send_message(owner_id, f"Shutting down bot with pid **{pid}**")
print(f'\n{nowtime()} {WHITE}Shutting down with PID {YELLOW}{pid}{RESET}') print(f'\n{nowtime()} {WHITE}Shutting down with PID {YELLOW}{pid}{RESET}')

View File

@ -5,5 +5,5 @@
"api_hash": "", "api_hash": "",
"bot_token": "", "bot_token": "",
"bot_name": "", "bot_name": "",
"use_compiled_page_saver": false "selenium_address": "http://localhost:4444"
} }

7
modules/app.py Normal file
View File

@ -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"])

View File

@ -2,7 +2,13 @@
import os import os
import traceback 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 functions import *
from modules.app import app
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
config = jsonLoad("config.json") config = jsonLoad("config.json")
@ -10,35 +16,38 @@ config = jsonLoad("config.json")
class EmptyCardException(Exception): class EmptyCardException(Exception):
pass 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}" url = f"https://bwtaqua.com.ua/card-topup/?id={cardid}"
try: try:
os.system(f'touch 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}")
if config["use_compiled_page_saver"]: wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'js-payment-balance')))
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')
with open(f'data/pages/{str(filename)}.html') as f: page_source = driver.page_source
html_file = f.read()
f.close()
soup = BeautifulSoup(html_file, 'html.parser') driver.execute_script("window.stop();")
output = (soup.find_all("h3", class_="headline headline_center headline_pink js-payment-balance")[0].getText()).replace("Твій баланс ", "").replace(" л", "") with open(f'data/pages/{filename}.html', "w", encoding="utf-8") as f:
f.write(page_source)
appendLog(f"Parsed {output} liters of water remaining (user: {str(filename)}, cardid: {cardid})") output = driver.find_element(By.CLASS_NAME, "js-payment-balance").text.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: {filename}, cardid: {cardid})")
except Exception as exp: 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: 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: else:
print(f'Exception occured and could not send to user: {exp}') print(f'Exception occured and could not send to user: {exp}')

View File

@ -1 +1,2 @@
beautifulsoup4 beautifulsoup4==4.11.1
selenium==4.7.2