mirror of
https://github.com/Balshgit/mosgortrans.git
synced 2025-09-11 13:00:40 +03:00
reduce web_parser time to 1 sec
This commit is contained in:
parent
8456281e70
commit
50bdc01b9d
@ -31,6 +31,6 @@ killall python
|
|||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- [ ] Добавить очередь сообщений
|
- [ ] Добавить очередь сообщений
|
||||||
- [ ] Исправить запуск локально
|
- [x] Исправить запуск локально
|
||||||
- [ ] Добавить тестов
|
- [ ] Добавить тестов
|
||||||
- [ ] Close connection
|
- [ ] Close connection
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from concurrent.futures.thread import ThreadPoolExecutor
|
|
||||||
|
|
||||||
from aiogram import Bot, types
|
from aiogram import Bot, types
|
||||||
from aiogram.contrib.middlewares.logging import LoggingMiddleware
|
from aiogram.contrib.middlewares.logging import LoggingMiddleware
|
||||||
@ -22,8 +21,6 @@ driver = configure_firefox_driver()
|
|||||||
|
|
||||||
stations_cb = CallbackData('station', 'direction')
|
stations_cb = CallbackData('station', 'direction')
|
||||||
|
|
||||||
executor = ThreadPoolExecutor(10)
|
|
||||||
|
|
||||||
|
|
||||||
def get_keyboard() -> types.InlineKeyboardMarkup:
|
def get_keyboard() -> types.InlineKeyboardMarkup:
|
||||||
"""
|
"""
|
||||||
|
@ -52,7 +52,7 @@ def parse_site(url: str, message: str, driver: WebDriver | None = None) -> str:
|
|||||||
logger.error('Driver is not configured')
|
logger.error('Driver is not configured')
|
||||||
return 'Что-то пошло не так. :( Драйвер Firefox не сконфигурирован.'
|
return 'Что-то пошло не так. :( Драйвер Firefox не сконфигурирован.'
|
||||||
driver.get(url)
|
driver.get(url)
|
||||||
time.sleep(4)
|
time.sleep(1)
|
||||||
elements = driver.find_elements(
|
elements = driver.find_elements(
|
||||||
by='class name', value='masstransit-vehicle-snippet-view'
|
by='class name', value='masstransit-vehicle-snippet-view'
|
||||||
)
|
)
|
||||||
|
13
app/main.py
13
app/main.py
@ -1,9 +1,15 @@
|
|||||||
|
import asyncio
|
||||||
|
import sys
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher
|
from aiogram import Bot, Dispatcher
|
||||||
from aiogram.types import Update
|
from aiogram.types import Update
|
||||||
from aiogram.utils.executor import start_polling
|
from aiogram.utils.executor import start_polling
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
|
sys.path.append(str(Path(__file__).parent.parent))
|
||||||
|
|
||||||
from app.core.bot import bot, dispatcher
|
from app.core.bot import bot, dispatcher
|
||||||
from app.core.logger import logger
|
from app.core.logger import logger
|
||||||
from app.core.scheduler import asyncio_schedule
|
from app.core.scheduler import asyncio_schedule
|
||||||
@ -19,7 +25,7 @@ from app.settings import (
|
|||||||
|
|
||||||
async def bot_startup() -> None:
|
async def bot_startup() -> None:
|
||||||
await bot.set_webhook(WEBHOOK_URL)
|
await bot.set_webhook(WEBHOOK_URL)
|
||||||
logger.info(f'Webhook set to {WEBHOOK_URL}')
|
logger.info(f'Webhook set to {WEBHOOK_URL}'.replace(API_TOKEN, '{BOT_API_TOKEN}'))
|
||||||
asyncio_schedule()
|
asyncio_schedule()
|
||||||
|
|
||||||
|
|
||||||
@ -33,6 +39,11 @@ async def bot_shutdown() -> None:
|
|||||||
await dispatcher.storage.close()
|
await dispatcher.storage.close()
|
||||||
await dispatcher.storage.wait_closed()
|
await dispatcher.storage.wait_closed()
|
||||||
|
|
||||||
|
session = await bot.get_session()
|
||||||
|
if session and not session.closed:
|
||||||
|
await session.close()
|
||||||
|
await asyncio.sleep(0.2)
|
||||||
|
|
||||||
logger.warning('Bye!')
|
logger.warning('Bye!')
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,9 +22,10 @@ ignore =
|
|||||||
B008,
|
B008,
|
||||||
per-file-ignores =
|
per-file-ignores =
|
||||||
; too complex queries
|
; too complex queries
|
||||||
*/repositories.py: ECE001
|
|
||||||
./app/tests/*: TAE001, S101, S311
|
./app/tests/*: TAE001, S101, S311
|
||||||
tests/*/factories/*: S5720
|
tests/*/factories/*: S5720
|
||||||
|
app/main.py: E402
|
||||||
|
|
||||||
|
|
||||||
[mypy]
|
[mypy]
|
||||||
# Mypy configuration:
|
# Mypy configuration:
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from aiogram import Bot, types
|
from aiogram import Bot, Dispatcher, types
|
||||||
from aiogram.dispatcher.filters.builtin import Command
|
from aiogram.dispatcher.filters.builtin import Command
|
||||||
|
from aiogram.types import Update
|
||||||
from app.core.bot import dispatcher
|
from app.core.bot import dispatcher
|
||||||
from tests.conftest import FakeTelegram
|
from tests.conftest import FakeTelegram
|
||||||
from tests.data.factories import UserFactory
|
from tests.data.factories import UserFactory
|
||||||
@ -35,7 +36,8 @@ async def test_command1(bot: Bot) -> None:
|
|||||||
assert command
|
assert command
|
||||||
|
|
||||||
|
|
||||||
async def test_update() -> None:
|
async def test_update(dispatcher_fixture: Dispatcher, bot: Bot) -> None:
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"update_id": 957250703,
|
"update_id": 957250703,
|
||||||
"message": {
|
"message": {
|
||||||
@ -60,4 +62,9 @@ async def test_update() -> None:
|
|||||||
"entities": [{"type": "bot_command", "offset": 0, "length": 7}],
|
"entities": [{"type": "bot_command", "offset": 0, "length": 7}],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
assert data
|
async with FakeTelegram(message_data=data):
|
||||||
|
update = Update(**data)
|
||||||
|
dispatcher_fixture.message_handler()
|
||||||
|
await dispatcher_fixture.process_update(update)
|
||||||
|
|
||||||
|
assert True
|
||||||
|
@ -3,7 +3,7 @@ from typing import Any
|
|||||||
|
|
||||||
import aresponses
|
import aresponses
|
||||||
import pytest
|
import pytest
|
||||||
from aiogram import Bot
|
from aiogram import Bot, Dispatcher
|
||||||
|
|
||||||
BOT_ID = 123456789
|
BOT_ID = 123456789
|
||||||
TOKEN = f'{BOT_ID}:AABBCCDDEEFFaabbccddeeff-1234567890'
|
TOKEN = f'{BOT_ID}:AABBCCDDEEFFaabbccddeeff-1234567890'
|
||||||
@ -61,3 +61,15 @@ async def bot_fixture() -> Bot:
|
|||||||
if session and not session.closed:
|
if session and not session.closed:
|
||||||
await session.close()
|
await session.close()
|
||||||
await asyncio.sleep(0.2)
|
await asyncio.sleep(0.2)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
async def dispatcher_fixture(bot: Bot) -> Dispatcher:
|
||||||
|
"""Dispatcher fixture."""
|
||||||
|
|
||||||
|
dp = Dispatcher(bot)
|
||||||
|
yield dp
|
||||||
|
session = await bot.get_session()
|
||||||
|
if session and not session.closed:
|
||||||
|
await session.close()
|
||||||
|
await asyncio.sleep(0.2)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user