mirror of
https://github.com/Balshgit/mosgortrans.git
synced 2025-09-11 13:00:40 +03:00
refactor app
This commit is contained in:
parent
9731401eb2
commit
bad1d79fcf
@ -5,25 +5,15 @@ from aiogram.contrib.middlewares.logging import LoggingMiddleware
|
|||||||
from aiogram.dispatcher import Dispatcher
|
from aiogram.dispatcher import Dispatcher
|
||||||
from aiogram.dispatcher.webhook import SendMessage
|
from aiogram.dispatcher.webhook import SendMessage
|
||||||
from aiogram.utils.callback_data import CallbackData
|
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 core.parse_web import parse_site, configure_firefox_driver
|
||||||
from settings import API_TOKEN, WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT
|
from settings import API_TOKEN
|
||||||
|
|
||||||
bot = Bot(token=API_TOKEN)
|
bot = Bot(token=API_TOKEN)
|
||||||
dispatcher = Dispatcher(bot)
|
dispatcher = Dispatcher(bot)
|
||||||
dispatcher.middleware.setup(LoggingMiddleware())
|
dispatcher.middleware.setup(LoggingMiddleware())
|
||||||
|
|
||||||
cron_jobs = [
|
driver = configure_firefox_driver()
|
||||||
{'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
|
|
||||||
]}
|
|
||||||
|
|
||||||
|
|
||||||
stations_cb = CallbackData('station', 'direction')
|
stations_cb = CallbackData('station', 'direction')
|
||||||
|
|
||||||
@ -73,8 +63,8 @@ async def chat_id(message: types.Message) -> SendMessage:
|
|||||||
|
|
||||||
|
|
||||||
@dispatcher.message_handler()
|
@dispatcher.message_handler()
|
||||||
async def echo(message: types.Message) -> None:
|
async def echo(message: types.Message) -> SendMessage:
|
||||||
await message.reply('Выбери остановку', reply_markup=get_keyboard())
|
return SendMessage(message.chat.id, 'Выбери остановку', reply_markup=get_keyboard())
|
||||||
|
|
||||||
|
|
||||||
async def send_message(chat_ids: list[int]) -> None:
|
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(
|
await asyncio.gather(
|
||||||
*[bot.send_message(chat_id=chat_id, text=text, parse_mode=types.ParseMode.HTML) for chat_id in chat_ids]
|
*[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
7
app/core/logger.py
Normal 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>")
|
@ -1,25 +1,18 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import tarfile
|
import tarfile
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import wget
|
import wget
|
||||||
from loguru import logger
|
|
||||||
from selenium import webdriver
|
from selenium import webdriver
|
||||||
from selenium.common.exceptions import NoSuchElementException
|
from selenium.common.exceptions import NoSuchElementException
|
||||||
from selenium.webdriver.firefox import options
|
from selenium.webdriver.firefox import options
|
||||||
from selenium.webdriver.firefox.service import Service
|
from selenium.webdriver.firefox.service import Service
|
||||||
from selenium.webdriver.firefox.webdriver import WebDriver
|
from selenium.webdriver.firefox.webdriver import WebDriver
|
||||||
|
|
||||||
|
from core.logger import logger
|
||||||
from settings import BASE_DIR, GECKO_DRIVER_VERSION
|
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():
|
def download_gecko_driver():
|
||||||
gecko_driver = f'https://github.com/mozilla/geckodriver/releases/download/v{GECKO_DRIVER_VERSION}/' \
|
gecko_driver = f'https://github.com/mozilla/geckodriver/releases/download/v{GECKO_DRIVER_VERSION}/' \
|
20
app/core/scheduler.py
Normal file
20
app/core/scheduler.py
Normal 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
39
app/main.py
Normal 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,
|
||||||
|
)
|
@ -4,7 +4,7 @@ from decouple import AutoConfig
|
|||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR.joinpath('some')
|
# Build paths inside the project like this: BASE_DIR.joinpath('some')
|
||||||
# `pathlib` is better than writing: dirname(dirname(dirname(__file__)))
|
# `pathlib` is better than writing: dirname(dirname(dirname(__file__)))
|
||||||
BASE_DIR = Path(__file__).parent
|
BASE_DIR = Path(__file__).parent.parent
|
||||||
|
|
||||||
# Loading `.env` files
|
# Loading `.env` files
|
||||||
# See docs: https://gitlab.com/mkleehammer/autoconfig
|
# See docs: https://gitlab.com/mkleehammer/autoconfig
|
27
gunicorn.sh
27
gunicorn.sh
@ -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"
|
|
@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
|
|
||||||
echo "starting the bot"
|
echo "starting the bot"
|
||||||
cd /opt/mosgortrans \
|
cd /opt/mosgortrans/app \
|
||||||
&& source /home/balsh/.cache/pypoetry/virtualenvs/mosgortrans-3eZxMcY3-py3.10/bin/activate \
|
&& source /home/balsh/.cache/pypoetry/virtualenvs/mosgortrans-3eZxMcY3-py3.10/bin/activate \
|
||||||
&& python main.py
|
&& python main.py
|
Loading…
x
Reference in New Issue
Block a user