Update parser and new feature
This commit is contained in:
parent
ae25f2e2a5
commit
8f0c7e0d9a
1
.gitignore
vendored
1
.gitignore
vendored
@ -16,3 +16,4 @@ __pycache__/
|
||||
!requirements.txt
|
||||
test.py
|
||||
venv/
|
||||
.vscode/
|
@ -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
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
from . import groups
|
||||
#from . import groups
|
||||
from . import private
|
||||
from . import callback
|
||||
from . import errors
|
||||
|
@ -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
|
||||
return True
|
||||
if isinstance(exception, MessageNotModified): return True
|
@ -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}")
|
||||
|
@ -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Выберите свою группу"
|
||||
|
@ -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()
|
@ -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
|
||||
|
6
start.sh
Normal file
6
start.sh
Normal 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
22
utils/misc.py
Normal 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]])
|
Loading…
Reference in New Issue
Block a user