Skip to content

Commit 4ce99e8

Browse files
authored
Merge pull request #8 from dmsnback/refactoring
refactoring
2 parents c47d1a1 + c7dc0ce commit 4ce99e8

19 files changed

Lines changed: 115 additions & 85 deletions

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ LABEL maintainer="Dmitry Titenkov <lt200711@yandex.ru>"
33
LABEL version="1.0"
44
LABEL description="Satire Pulp parser"
55
WORKDIR /app
6-
COPY requirements.txt /app
6+
COPY requirements.txt .
77
RUN pip3 install -r /app/requirements.txt --no-cache-dir
88
COPY . .

bot/__init__.py

Whitespace-only changes.

bot_storage.py renamed to bot/bot_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from models import LastSentNews, News
3+
from db.models import LastSentNews, News
44
from sqlalchemy import select
55
from sqlalchemy.exc import SQLAlchemyError
66
from sqlalchemy.ext.asyncio import AsyncSession

bot.py renamed to bot/handlers.py

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import logging
22

3-
from bot_storage import (
3+
from bot.bot_storage import (
44
get_all_users,
55
get_last_sent_id,
66
get_news_after_id,
77
save_last_sent_news_id,
88
)
9+
from bot.sender import send_news
910
from config import setup_logger
10-
from db_async import AsyncSessionLocal
11+
from db.db_async import AsyncSessionLocal
1112
from dotenv import load_dotenv
1213
from telegram import (
1314
BotCommand,
@@ -23,50 +24,6 @@
2324
logger = logging.getLogger(__name__)
2425

2526

26-
MAX_CAPTION_LENGTH = 1024
27-
28-
29-
def format_message(title, text):
30-
message = f"*{title}*\n\n{text}\n"
31-
if len(message) > MAX_CAPTION_LENGTH:
32-
message = f"*{title}*\n\n{text[:MAX_CAPTION_LENGTH]} ...✂️\n"
33-
return message
34-
35-
36-
async def send_news(
37-
chat_id: int, context: ContextTypes.DEFAULT_TYPE, title, image, text, url
38-
):
39-
message = format_message(title, text)
40-
keyboard = [
41-
[InlineKeyboardButton("Читать полную версию на сайте", url=url)]
42-
]
43-
reply_markup = InlineKeyboardMarkup(keyboard)
44-
if image:
45-
try:
46-
await context.bot.send_photo(
47-
chat_id=chat_id,
48-
photo=image,
49-
caption=message,
50-
parse_mode="Markdown",
51-
reply_markup=reply_markup,
52-
)
53-
logger.info("Новость отправлена с картинкой")
54-
return
55-
except Exception as e:
56-
logger.error(
57-
f"Не удалось отправить фото по ссылке, ошибка: {e}",
58-
)
59-
try:
60-
await context.bot.send_message(
61-
chat_id, message, parse_mode="Markdown", reply_markup=reply_markup
62-
)
63-
logger.info(f"Новость '{title[:25]}' отправлена без картинки")
64-
except Exception as e:
65-
logger.error(
66-
f"Не удалось отправить сообщение с новостью '{title[:25]}', ошибка: {e}"
67-
)
68-
69-
7027
async def auto_send_news(context: ContextTypes.DEFAULT_TYPE):
7128
async with AsyncSessionLocal() as session:
7229
users = await get_all_users(session)

bot/sender.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import logging
2+
3+
from config import setup_logger
4+
from dotenv import load_dotenv
5+
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
6+
from telegram.ext import ContextTypes
7+
8+
load_dotenv()
9+
10+
setup_logger()
11+
logger = logging.getLogger(__name__)
12+
13+
14+
MAX_CAPTION_LENGTH = 1024
15+
16+
17+
def format_message(title, text):
18+
message = f"*{title}*\n\n{text}\n"
19+
if len(message) > MAX_CAPTION_LENGTH:
20+
message = f"*{title}*\n\n{text[:MAX_CAPTION_LENGTH]} ...✂️\n"
21+
return message
22+
23+
24+
async def send_news(
25+
chat_id: int, context: ContextTypes.DEFAULT_TYPE, title, image, text, url
26+
):
27+
message = format_message(title, text)
28+
keyboard = [
29+
[InlineKeyboardButton("Читать полную версию на сайте", url=url)]
30+
]
31+
reply_markup = InlineKeyboardMarkup(keyboard)
32+
if image:
33+
try:
34+
await context.bot.send_photo(
35+
chat_id=chat_id,
36+
photo=image,
37+
caption=message,
38+
parse_mode="Markdown",
39+
reply_markup=reply_markup,
40+
)
41+
logger.info("Новость отправлена с картинкой")
42+
return
43+
except Exception as e:
44+
logger.error(
45+
f"Не удалось отправить фото по ссылке, ошибка: {e}",
46+
)
47+
try:
48+
await context.bot.send_message(
49+
chat_id, message, parse_mode="Markdown", reply_markup=reply_markup
50+
)
51+
logger.info(f"Новость '{title[:25]}' отправлена без картинки")
52+
except Exception as e:
53+
logger.error(
54+
f"Не удалось отправить сообщение с новостью '{title[:25]}', ошибка: {e}"
55+
)

db/__init__.py

Whitespace-only changes.

db_async.py renamed to db/db_async.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
load_dotenv()
77

88

9-
engine = create_async_engine(os.getenv("DATABASE_URL_ASYNC"), echo=False)
9+
DATABASE_URL_ASYNC = os.getenv("DATABASE_URL_ASYNC")
10+
11+
12+
engine = create_async_engine(DATABASE_URL_ASYNC, echo=False)
1013

1114

1215
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False)

db_sync.py renamed to db/db_sync.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
load_dotenv()
88

99

10-
engine = create_engine(os.getenv("DATABASE_URL_SYNC"))
10+
DATABASE_URL_SYNC = os.getenv("DATABASE_URL_SYNC")
11+
12+
13+
engine = create_engine(DATABASE_URL_SYNC)
1114

1215
SessionLocal = sessionmaker(engine)

init_db.py renamed to db/init_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import logging
33

44
from config import setup_logger
5-
from db_async import engine
5+
from db.db_async import engine
6+
from db.models import Base, LastSentNews, News # noqa
67
from dotenv import load_dotenv
7-
from models import Base, LastSentNews, News # noqa
88

99
load_dotenv()
1010

File renamed without changes.

0 commit comments

Comments
 (0)