Update parser and new feature

This commit is contained in:
tema 2022-10-07 17:50:42 +03:00
parent ae25f2e2a5
commit 8f0c7e0d9a
Signed by: tema
GPG Key ID: 21FDB6D162488F6F
10 changed files with 83 additions and 35 deletions

1
.gitignore vendored
View File

@ -16,3 +16,4 @@ __pycache__/
!requirements.txt !requirements.txt
test.py test.py
venv/ venv/
.vscode/

View File

@ -37,12 +37,16 @@ new_year = (
"Бот будет отключён до 16.01.2022(Период зимних каникул)\n" "Бот будет отключён до 16.01.2022(Период зимних каникул)\n"
) )
link_replace = 'https://tfk.org.ua/zamini-do-rozkladu-08-51-30-03-02-2022/' link_replace = 'https://tfk.org.ua/zamini-do-rozkladu-08-51-30-03-02-2022/'
the_end =( the_end =(
"Всё было восстановлено и настроено. Бот продолжает работу!:)" "Всё было восстановлено и настроено. Бот продолжает работу!:)"
) )
september_1 = ("Всіх з 1 вересням, всього найкращого!\n"
"Бот буде запущений чуть пізніше, "
"коли заміни будуть публіковаться текстом")
send_msg = the_end send_msg = the_end

View File

@ -1,4 +1,4 @@
from . import groups #from . import groups
from . import private from . import private
from . import callback from . import callback
from . import errors from . import errors

View File

@ -1,5 +1,5 @@
import logging import logging
from aiogram.utils.exceptions import BotBlocked from aiogram.utils.exceptions import BotBlocked, MessageNotModified
from load import dp from load import dp
@ -8,4 +8,5 @@ from load import dp
async def errors_handler(update, exception): async def errors_handler(update, exception):
if isinstance(exception, BotBlocked): if isinstance(exception, BotBlocked):
logging.info("Bot blocked") logging.info("Bot blocked")
return True return True
if isinstance(exception, MessageNotModified): return True

View File

@ -3,6 +3,7 @@ from aiogram import types
from load import dp, bot from load import dp, bot
from parser import docs_parse from parser import docs_parse
from utils.misc import Update
@dp.message_handler(admin=True, commands=['reload']) @dp.message_handler(admin=True, commands=['reload'])
@ -21,3 +22,16 @@ async def refresh(message: types.Message):
await m.edit_text( 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}")

View File

@ -1,4 +1,6 @@
import logging import logging
import io
import base64
from aiogram import types from aiogram import types
from aiogram.dispatcher.filters import ChatTypeFilter from aiogram.dispatcher.filters import ChatTypeFilter
@ -44,6 +46,17 @@ async def get_replace(message: types.Message):
try: try:
data = get_about_replacements() 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( await bot.send_message(
message.chat.id, message.chat.id,
"Замены {date}\n{teacher}\nВыберите свою группу" "Замены {date}\n{teacher}\nВыберите свою группу"

View File

@ -1,14 +1,18 @@
import requests import base64
import json import json
import datetime import datetime
from datetime import datetime as dt from datetime import datetime as dt
import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
try: try:
from load import config from load import config
except: config = None except ImportError: config = None
from .utils import * try:
from .utils import *
except ImportError:
from utils import *
headers = { headers = {
@ -41,10 +45,11 @@ def docs_parse():
soup = BeautifulSoup(page.text, "lxml") soup = BeautifulSoup(page.text, "lxml")
# Это в идеале нужно переписать... # Это в идеале нужно переписать...
try: output = table_parser(soup, output); #print(output) url = image_parser(soup)
except Exception: pass with requests.get(url=url, allow_redirects=True, stream=True) as r:
try: output = test_parser(soup, output) output['image'] = True
except Exception as e: raise(e) output['date'] = 'невозможно получить!'
output['data']['all'] = base64.b64encode(r.content).decode('utf-8')
with open(config.data_file, 'w') as f: with open(config.data_file, 'w') as f:
@ -57,3 +62,4 @@ def get_about_replacements() -> dict:
data = json.loads(f.read()) data = json.loads(f.read())
f.close() f.close()
return data return data
docs_parse()

View File

@ -1,5 +1,6 @@
from bs4 import BeautifulSoup
def table_parser(soup, output): def table_parser(soup: BeautifulSoup, output):
#Date parser #Date parser
date = (soup.find("main").findAll('span', style="color:black"))[1] date = (soup.find("main").findAll('span', style="color:black"))[1]
output["date"] = date.text.replace(u'\xa0', u'') output["date"] = date.text.replace(u'\xa0', u'')
@ -22,29 +23,9 @@ def table_parser(soup, output):
return output return output
def text_parser(soup, output): def image_parser(soup: BeautifulSoup):
main = soup.find("main") main = soup.find("p", style="text-align:center; margin:0cm 0cm 8pt")
image = main.select_one('img[src$=".jpg"]')
text: str = '' output = image['src']
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
return output return output

6
start.sh Normal file
View File

@ -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

22
utils/misc.py Normal file
View File

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