6 Commits

Author SHA1 Message Date
dffa29705f - 2022-12-15 12:40:15 +01:00
6ee5c227c6 Added notice that this is a dev branch 2022-12-15 12:39:56 +01:00
57d4d1ce5c Working on selenium migration 2022-12-15 12:00:01 +01:00
fe1c6984b2 Option to use compiled page saver 2022-09-08 13:04:37 +02:00
e7ef1d4613 Added encoding header 2022-09-08 12:54:43 +02:00
85a756dcab Added license badge 2022-09-08 12:54:13 +02:00
8 changed files with 60 additions and 23 deletions

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

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

View File

@@ -1,6 +1,10 @@
# BWTAqua
[![License: GPL v3](https://img.shields.io/badge/License-GPL_v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.html)
Simple yet helpful bot to check BWT Aqua's card balance
> ⚠️ Please note that this branch is made for testing only so it can sometimes work strange or do not work at all.
## Requirements
* nodejs & npm
* python3
@@ -12,9 +16,7 @@ Simple yet helpful bot to check BWT Aqua's card balance
2. `cd BWTAqua`
2. Install needed modules:
* `python3 -m pip install -r requirements.txt`
3. Install PageSaver:
1. `cd PageSaver`
2. `npm install`
3. Install and configure Selenium Server
4. Configure the bot:
1. `cd ..`
2. `nano config.json` (You can use any other text editor actually, for example `vim`)
@@ -22,5 +24,5 @@ Simple yet helpful bot to check BWT Aqua's card balance
* `python3 bwtbot.py`
## Configuration
You can edit with vim, nano, on Windows it's Notepad or Notepad++. Whatever.
You can edit with vim, nano, whatever.
If you don't know where to find bot_token and your id - here you can find some hints: [get bot token](https://www.siteguarding.com/en/how-to-get-telegram-bot-api-token), [get your id](https://www.alphr.com/telegram-find-user-id/), [get api_hash and api_id](https://core.telegram.org/api/obtaining_api_id).

View File

@@ -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}')

View File

@@ -4,5 +4,6 @@
"api_id": 0,
"api_hash": "",
"bot_token": "",
"bot_name": ""
"bot_name": "",
"selenium_address": "http://localhost:4444"
}

View File

@@ -1,3 +1,5 @@
#-*- coding: utf-8 -*-
import json
import os
import shutil

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 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,31 +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')
os.system(f'PageSaver/pageSaver "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}')

View File

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