This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
BWTAqua/modules/bwt.py

69 lines
2.6 KiB
Python
Raw Normal View History

2022-09-08 13:12:25 +03:00
#-*- coding: utf-8 -*-
2022-12-30 23:13:10 +02:00
from os import makedirs, path
from subprocess import check_output
2022-12-30 22:03:04 +02:00
from traceback import format_exc
2022-12-30 23:13:10 +02:00
from uuid import uuid4
2022-09-08 13:12:25 +03:00
from functions import *
from bs4 import BeautifulSoup
config = jsonLoad("config.json")
class EmptyCardException(Exception):
pass
async def getWaterLeft(cardid, filename, app=None):
url = f"https://bwtaqua.com.ua/card-topup/?id={cardid}"
try:
2022-12-30 23:13:10 +02:00
# if path.exists(f"data/pages/{str(filename)}.html") is False:
# run(["touch", f"data/pages/{str(filename)}.html"])
2022-09-08 14:04:37 +03:00
2022-12-30 23:13:10 +02:00
if config["use_compiled_page_saver"] is True:
proc = check_output(["PageSaver/pageSaver", f"https://bwtaqua.com.ua/card-topup/?id={cardid}"]) #, ">", f"data/pages/{str(filename)}.html"])
html_file = proc.decode("utf-8")
2022-09-08 14:04:37 +03:00
else:
2022-12-30 23:13:10 +02:00
proc = check_output(["node", "./PageSaver/pageSaver.js", f"https://bwtaqua.com.ua/card-topup/?id={cardid}"]) #, ">", f"data/pages/{str(filename)}.html"])
html_file = proc.decode("utf-8")
2022-09-08 13:12:25 +03:00
2022-12-30 23:13:10 +02:00
# with open(f'data/pages/{str(filename)}.html') as f:
# html_file = f.read()
# f.close()
2022-09-08 13:12:25 +03:00
soup = BeautifulSoup(html_file, '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})")
except Exception as exp:
appendLog(f"Exception occured: {exp} (user: {str(filename)}, cardid: {cardid})")
2022-12-30 23:13:10 +02:00
try:
tmp_name = str(uuid4())
makedirs("tmp", exist_ok=True)
with open(path.join("tmp", tmp_name), "w", encoding="utf-8") as f:
f.write(html_file)
except NameError:
tmp_name = "N/A"
appendLog(f"'html_file' is not defined so I won't gather any tmp data")
2022-09-08 13:12:25 +03:00
if app != None:
2022-12-30 23:13:10 +02:00
await app.send_message(config["owner_id"], f"**Exception occured:**\n • User: `{str(filename)}`\n • Card: [{cardid}]({url})\n • Exception: `{exp}`\n • TMP UUID: `{tmp_name}`\n • Traceback: `{format_exc()}`", disable_web_page_preview=True)
2022-09-08 13:12:25 +03:00
else:
2022-12-30 23:13:10 +02:00
appendLog(f'Exception occured and could not send to user: {exp}')
2022-09-08 13:12:25 +03:00
output = "Failure"
return output
if __name__ == "__main__":
cardid = input("Enter card number: ")
userid = input("Enter Telegram ID (optional): ")
print(f"Card has {str(getWaterLeft(cardid, userid, app=None))} l. left")