refactor app

This commit is contained in:
Dmitry Afanasyev 2022-08-09 23:23:28 +03:00
parent 9731401eb2
commit bad1d79fcf
8 changed files with 74 additions and 94 deletions

View File

@ -5,25 +5,15 @@ from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.webhook import SendMessage
from aiogram.utils.callback_data import CallbackData
from aiogram.utils.executor import start_webhook
from mos_gor import logger, parse_site, download_gecko_driver, configure_firefox_driver
from settings import API_TOKEN, WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT
from core.parse_web import parse_site, configure_firefox_driver
from settings import API_TOKEN
bot = Bot(token=API_TOKEN)
dispatcher = Dispatcher(bot)
dispatcher.middleware.setup(LoggingMiddleware())
cron_jobs = [
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 8, 'minute': 59, 'second': 0},
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 9, 'minute': 4, 'second': 0},
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 9, 'minute': 9, 'second': 0},
]
user_chat_ids = {'chat_ids': [417070387, # me
431571617, # Lenok
]}
driver = configure_firefox_driver()
stations_cb = CallbackData('station', 'direction')
@ -73,8 +63,8 @@ async def chat_id(message: types.Message) -> SendMessage:
@dispatcher.message_handler()
async def echo(message: types.Message) -> None:
await message.reply('Выбери остановку', reply_markup=get_keyboard())
async def echo(message: types.Message) -> SendMessage:
return SendMessage(message.chat.id, 'Выбери остановку', reply_markup=get_keyboard())
async def send_message(chat_ids: list[int]) -> None:
@ -87,45 +77,3 @@ async def send_message(chat_ids: list[int]) -> None:
await asyncio.gather(
*[bot.send_message(chat_id=chat_id, text=text, parse_mode=types.ParseMode.HTML) for chat_id in chat_ids]
)
def asyncio_schedule() -> None:
from apscheduler.schedulers.asyncio import AsyncIOScheduler
scheduler = AsyncIOScheduler()
for cron in cron_jobs:
scheduler.add_job(send_message, kwargs=user_chat_ids, **cron)
scheduler.start()
async def on_startup(dispatcher) -> None:
await bot.set_webhook(WEBHOOK_URL)
asyncio_schedule()
async def on_shutdown(dispatcher):
logger.warning('Shutting down..')
# Remove webhook (not acceptable in some cases)
await bot.delete_webhook()
# Close DB connection (if used)
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
logger.warning('Bye!')
if __name__ == '__main__':
download_gecko_driver()
driver = configure_firefox_driver()
start_webhook(
dispatcher=dispatcher,
webhook_path=WEBHOOK_PATH,
on_startup=on_startup,
on_shutdown=on_shutdown,
skip_updates=True,
host=WEBAPP_HOST,
port=WEBAPP_PORT,
)

7
app/core/logger.py Normal file
View File

@ -0,0 +1,7 @@
import sys
from loguru import logger
logger.remove()
logger.add(sink=sys.stdout, colorize=True, level='DEBUG',
format="<cyan>{time:DD.MM.YYYY HH:mm:ss}</cyan> | <level>{level}</level> | "
"<magenta>{message}</magenta>")

View File

@ -1,25 +1,18 @@
import os
import sys
import tarfile
import time
from datetime import datetime
from pathlib import Path
import wget
from loguru import logger
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.firefox import options
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.webdriver import WebDriver
from core.logger import logger
from settings import BASE_DIR, GECKO_DRIVER_VERSION
logger.remove()
logger.add(sink=sys.stdout, colorize=True, level='DEBUG',
format="<cyan>{time:DD.MM.YYYY HH:mm:ss}</cyan> | <level>{level}</level> | "
"<magenta>{message}</magenta>")
def download_gecko_driver():
gecko_driver = f'https://github.com/mozilla/geckodriver/releases/download/v{GECKO_DRIVER_VERSION}/' \

20
app/core/scheduler.py Normal file
View File

@ -0,0 +1,20 @@
from core.bot import send_message
cron_jobs = [
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 8, 'minute': 59, 'second': 0},
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 9, 'minute': 4, 'second': 0},
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 9, 'minute': 9, 'second': 0},
]
user_chat_ids = {'chat_ids': [417070387, # me
431571617, # Lenok
]}
def asyncio_schedule() -> None:
from apscheduler.schedulers.asyncio import AsyncIOScheduler
scheduler = AsyncIOScheduler()
for cron in cron_jobs:
scheduler.add_job(send_message, kwargs=user_chat_ids, **cron)
scheduler.start()

39
app/main.py Normal file
View File

@ -0,0 +1,39 @@
from aiogram.utils.executor import start_webhook
from core.bot import bot, dispatcher
from core.logger import logger
from core.parse_web import download_gecko_driver
from core.scheduler import asyncio_schedule
from settings import WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT
async def on_startup(dispatcher) -> None:
await bot.set_webhook(WEBHOOK_URL)
asyncio_schedule()
async def on_shutdown(dispatcher):
logger.warning('Shutting down..')
# Remove webhook (not acceptable in some cases)
await bot.delete_webhook()
# Close DB connection (if used)
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
logger.warning('Bye!')
if __name__ == '__main__':
download_gecko_driver()
start_webhook(
dispatcher=dispatcher,
webhook_path=WEBHOOK_PATH,
on_startup=on_startup,
on_shutdown=on_shutdown,
skip_updates=True,
host=WEBAPP_HOST,
port=WEBAPP_PORT,
)

View File

@ -4,7 +4,7 @@ from decouple import AutoConfig
# Build paths inside the project like this: BASE_DIR.joinpath('some')
# `pathlib` is better than writing: dirname(dirname(dirname(__file__)))
BASE_DIR = Path(__file__).parent
BASE_DIR = Path(__file__).parent.parent
# Loading `.env` files
# See docs: https://gitlab.com/mkleehammer/autoconfig

View File

@ -1,27 +0,0 @@
#!/usr/bin/env sh
set -o errexit
set -o nounset
# We are using `gunicorn` for production, see:
# http://docs.gunicorn.org/en/stable/configure.html
# Check that $DJANGO_ENV is set to "production",
# fail otherwise, since it may break things:
# Start gunicorn:
# Docs: http://docs.gunicorn.org/en/stable/settings.html
# Concerning `workers` setting see:
# https://github.com/wemake-services/wemake-django-template/issues/1022
/usr/local/bin/gunicorn server.wsgi \
--workers 9 \
--timeout 150 \
--max-requests 2000 \
--max-requests-jitter 400 \
--bind '0.0.0.0:8000' \
--chdir '/opt/telebot_balsh/telebot' \
--log-level info \
--error-logfile "$SHARED_DIR/logs/gunicorn.log" \
--worker-tmp-dir '/dev/shm' \
--pid "$SHARED_DIR/logs/gunicorn_pid"

View File

@ -2,6 +2,6 @@
echo "starting the bot"
cd /opt/mosgortrans \
cd /opt/mosgortrans/app \
&& source /home/balsh/.cache/pypoetry/virtualenvs/mosgortrans-3eZxMcY3-py3.10/bin/activate \
&& python main.py