reworked to poetry run

This commit is contained in:
Dmitry Afanasyev 2022-08-13 15:49:50 +03:00
parent 43c2144a84
commit e8a01d1507
7 changed files with 17 additions and 19 deletions

View File

@ -5,9 +5,8 @@ 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 core.parse_web import parse_site, configure_firefox_driver, download_gecko_driver
from settings import API_TOKEN
from app.core.parse_web import configure_firefox_driver, download_gecko_driver, parse_site
from app.settings import API_TOKEN
bot = Bot(token=API_TOKEN)
dispatcher = Dispatcher(bot)

View File

@ -1,4 +1,5 @@
import sys
from loguru import logger
logger.remove()

View File

@ -4,14 +4,13 @@ import time
from pathlib import Path
import wget
from app.core.logger 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
from app.settings import BASE_DIR, GECKO_DRIVER_VERSION
def download_gecko_driver():
@ -20,13 +19,13 @@ def download_gecko_driver():
f'geckodriver-v{GECKO_DRIVER_VERSION}-linux64.tar.gz'
)
if not Path(f'{BASE_DIR}/geckodriver').exists():
if not Path(BASE_DIR / 'geckodriver').exists():
logger.info(f'Downloading gecodriver v {GECKO_DRIVER_VERSION}...')
geckodriver_file = wget.download(url=gecko_driver, out=BASE_DIR)
geckodriver_file = wget.download(url=gecko_driver, out=BASE_DIR.resolve().as_posix())
with tarfile.open(geckodriver_file) as tar:
tar.extractall(BASE_DIR)
os.remove(f'{BASE_DIR}/geckodriver-v{GECKO_DRIVER_VERSION}-linux64.tar.gz')
os.remove(f'{BASE_DIR / "geckodriver"}-v{GECKO_DRIVER_VERSION}-linux64.tar.gz')
logger.info(f'\ngeckodriver has been downloaded to folder {BASE_DIR}')
@ -37,7 +36,7 @@ def configure_firefox_driver(private_window: bool = False) -> WebDriver:
opt.add_argument(f'{Path.home()}/snap/firefox/common/.mozilla/firefox')
if private_window:
opt.set_preference("browser.privatebrowsing.autostart", True)
service = Service(executable_path=f'{BASE_DIR}/geckodriver')
service = Service(executable_path=BASE_DIR / 'geckodriver')
firefox_driver = webdriver.Firefox(service=service, options=opt)
return firefox_driver
@ -80,3 +79,5 @@ def parse_site(driver: WebDriver, url: str, message: str) -> str:
if bus_t19:
answer += f'Автобус {bus_t19.text} - {bus_t19_arrival.text}'
return answer
download_gecko_driver()

View File

@ -1,4 +1,4 @@
from core.bot import morning_bus_mailing
from app.core.bot import morning_bus_mailing
cron_jobs = [
{'trigger': 'cron', 'day_of_week': 'mon-fri', 'hour': 8, 'minute': 59, 'second': 0},

View File

@ -1,17 +1,16 @@
from aiogram.utils.executor import start_webhook
from core.bot import bot, dispatcher
from core.logger import logger
from core.scheduler import asyncio_schedule
from settings import WEBHOOK_URL, WEBHOOK_PATH, WEBAPP_HOST, WEBAPP_PORT
from settings import WEBAPP_HOST, WEBAPP_PORT, WEBHOOK_PATH, WEBHOOK_URL
async def on_startup(dispatcher) -> None:
async def on_startup() -> None:
await bot.set_webhook(WEBHOOK_URL)
asyncio_schedule()
async def on_shutdown(dispatcher):
async def on_shutdown() -> None:
logger.warning('Shutting down..')
# Remove webhook (not acceptable in some cases)

View File

@ -14,7 +14,6 @@ config = AutoConfig(search_path=env_path)
GECKO_DRIVER_VERSION = config('GECKO_DRIVER_VERSION')
BASE_DIR = Path(__file__).parent.resolve().as_posix()
API_TOKEN = config('API_TOKEN')

View File

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