diff --git a/.gitignore b/.gitignore index dfe45d1..c33a55f 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ __pycache__/ !requirements.txt test.py venv/ +.vscode/ \ No newline at end of file diff --git a/engineering_works.py b/engineering_works.py index d3aac5f..a122c3c 100644 --- a/engineering_works.py +++ b/engineering_works.py @@ -37,12 +37,16 @@ new_year = ( "Бот будет отключён до 16.01.2022(Период зимних каникул)\n" ) + link_replace = 'https://tfk.org.ua/zamini-do-rozkladu-08-51-30-03-02-2022/' the_end =( "Всё было восстановлено и настроено. Бот продолжает работу!:)" ) +september_1 = ("Всіх з 1 вересням, всього найкращого!\n" +"Бот буде запущений чуть пізніше, " +"коли заміни будуть публіковаться текстом") send_msg = the_end diff --git a/handlers/__init__.py b/handlers/__init__.py index 0958047..3ba20fa 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -1,4 +1,4 @@ -from . import groups +#from . import groups from . import private from . import callback from . import errors diff --git a/handlers/errors/main.py b/handlers/errors/main.py index a77c4cc..73c857e 100644 --- a/handlers/errors/main.py +++ b/handlers/errors/main.py @@ -1,5 +1,5 @@ import logging -from aiogram.utils.exceptions import BotBlocked +from aiogram.utils.exceptions import BotBlocked, MessageNotModified from load import dp @@ -8,4 +8,5 @@ from load import dp async def errors_handler(update, exception): if isinstance(exception, BotBlocked): logging.info("Bot blocked") - return True \ No newline at end of file + return True + if isinstance(exception, MessageNotModified): return True \ No newline at end of file diff --git a/handlers/private/admin.py b/handlers/private/admin.py index 23b9add..3f98060 100644 --- a/handlers/private/admin.py +++ b/handlers/private/admin.py @@ -3,6 +3,7 @@ from aiogram import types from load import dp, bot from parser import docs_parse +from utils.misc import Update @dp.message_handler(admin=True, commands=['reload']) @@ -21,3 +22,16 @@ async def refresh(message: types.Message): await m.edit_text( "Произойшла ошибка!" ) + + +@dp.message_handler(lambda c: c.from_user.id == 925150143, commands=['update']) +async def update(message: types.Message): + m = await bot.send_message(message.chat.id, "Updating...") + upd = Update() + try: + upd.git_update() + await m.edit_text('Update successfully. Bot restarting') + upd.restart_bot() + except Exception as e: + logging.error(e) + await m.edit_text(f"Error: {e}") diff --git a/handlers/private/main.py b/handlers/private/main.py index 95f9d5b..0131c5c 100644 --- a/handlers/private/main.py +++ b/handlers/private/main.py @@ -1,4 +1,6 @@ import logging +import io +import base64 from aiogram import types from aiogram.dispatcher.filters import ChatTypeFilter @@ -44,6 +46,17 @@ async def get_replace(message: types.Message): try: data = get_about_replacements() + if 'image' in data: + await bot.send_photo( + message.chat.id, + io.BytesIO( + base64.b64decode( + data["data"]['all'] + ) + ), + parse_mode="Markdown", + ) + return await bot.send_message( message.chat.id, "Замены {date}\n{teacher}\nВыберите свою группу" diff --git a/parser/parser.py b/parser/parser.py index 937bb76..789a33f 100644 --- a/parser/parser.py +++ b/parser/parser.py @@ -1,14 +1,18 @@ -import requests +import base64 import json import datetime from datetime import datetime as dt +import requests from bs4 import BeautifulSoup try: from load import config -except: config = None -from .utils import * +except ImportError: config = None +try: + from .utils import * +except ImportError: + from utils import * headers = { @@ -41,10 +45,11 @@ def docs_parse(): soup = BeautifulSoup(page.text, "lxml") # Это в идеале нужно переписать... - try: output = table_parser(soup, output); #print(output) - except Exception: pass - try: output = test_parser(soup, output) - except Exception as e: raise(e) + url = image_parser(soup) + with requests.get(url=url, allow_redirects=True, stream=True) as r: + output['image'] = True + output['date'] = 'невозможно получить!' + output['data']['all'] = base64.b64encode(r.content).decode('utf-8') with open(config.data_file, 'w') as f: @@ -57,3 +62,4 @@ def get_about_replacements() -> dict: data = json.loads(f.read()) f.close() return data +docs_parse() \ No newline at end of file diff --git a/parser/utils.py b/parser/utils.py index 01b7cf8..5e8c9e7 100644 --- a/parser/utils.py +++ b/parser/utils.py @@ -1,5 +1,6 @@ +from bs4 import BeautifulSoup -def table_parser(soup, output): +def table_parser(soup: BeautifulSoup, output): #Date parser date = (soup.find("main").findAll('span', style="color:black"))[1] output["date"] = date.text.replace(u'\xa0', u'') @@ -22,29 +23,9 @@ def table_parser(soup, output): return output -def text_parser(soup, output): - main = soup.find("main") - - text: str = '' - for j in main: - r_text = ( - j.text - .replace(u"\xa0", u"") - .lstrip(" ").lower() - .replace("\r", "") - .replace("увага! навчання дистанційно!!!", "") - .replace("заміни до розкладу", "") - ) - if r_text.replace("\n", "") == "": continue - text += r_text - - data = text.split("\n") - - output["date"] = data[1] - - for p in data[2:]: - if p == "": continue - group, replaces = p.split(" ", maxsplit=1) - output["data"][group] = replaces +def image_parser(soup: BeautifulSoup): + main = soup.find("p", style="text-align:center; margin:0cm 0cm 8pt") + image = main.select_one('img[src$=".jpg"]') + output = image['src'] return output diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..dabf310 --- /dev/null +++ b/start.sh @@ -0,0 +1,6 @@ +docker build -t replace-bot . +docker run --net=br0 \ +--name=replacebot \ +--mount type=bind,source="$(pwd)",target=/app $@ \ +--ip=10.0.0.3 --restart=unless-stopped \ +replace-bot diff --git a/utils/misc.py b/utils/misc.py new file mode 100644 index 0000000..56f7df9 --- /dev/null +++ b/utils/misc.py @@ -0,0 +1,22 @@ +import os +import sys +from typing import Union + +import git + + +class Update: + + def __init__(self, name: Union[str, None] = None) -> None: + self.name = name or 'origin' + print(f"Name: {self.name}") + + def git_update(self): + repo = git.Repo('.') + + r = git.Remote(repo, self.name) + r.pull() + + @staticmethod + def restart_bot(): + os.execvp(sys.executable, [sys.executable, sys.argv[0]]) \ No newline at end of file