-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
176 lines (158 loc) · 7 KB
/
Copy pathbot.py
File metadata and controls
176 lines (158 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import asyncio
import pdfkit
from requests import get
from bs4 import BeautifulSoup
import string
import os
import zipfile
import re
from dotenv import load_dotenv
from aiogram import Bot, Dispatcher, types, F
from aiogram.filters import Command
from aiogram.exceptions import TelegramBadRequest
from aiogram.types import FSInputFile
from aiogram.fsm.context import FSMContext
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.enums import ParseMode
AUTHOR = "@allcodny"
MAX_URLS = 50
MAX_PAGE_SIZE = 400
MAX_CONVERSION_TIME = 60
options_mobile = {
'encoding': "UTF-8",
'user-style-sheet': 'resize.css'
}
options = {'encoding': "UTF-8",}
load_dotenv()
bot = Bot(os.getenv("TOKEN"))
dp = Dispatcher(storage=MemoryStorage())
url_pattern = r'https?://[^\s\)\]\}<>"]+'
async def convert_url_to_pdf(url: str, id: int, n: int, m: int, mobile: bool):
try:
response = get(url)
content_length = len(response.content)
if content_length > MAX_PAGE_SIZE * 1024:
size_mb = content_length / 1024
await bot.edit_message_text(
f'Страница слишком большая ({size_mb:.1f} КБ). Максимум: {MAX_PAGE_SIZE} КБ',
chat_id=id,
message_id=m
)
return False
title = BeautifulSoup(response.content, 'html.parser').title
if title is None:
title = "Сайт в pdf"
else:
title = title.string
title = str(n) + ". " + title.translate(str.maketrans('', '', string.punctuation)) + ".pdf"
if mobile:
options_ = options_mobile
else:
options_ = options
loop = asyncio.get_event_loop()
await asyncio.wait_for(
loop.run_in_executor(
None,
lambda: pdfkit.from_url(url, title, options=options_)
),
timeout=MAX_CONVERSION_TIME
)
return title
except TimeoutError:
await bot.edit_message_text(
f'Превышено время ожидания ({MAX_CONVERSION_TIME} секунд)',
chat_id=id,
message_id=m
)
return False
except Exception as e:
await bot.edit_message_text(f'Произошла ошибка: {e}', chat_id=id, message_id=m)
print(f'Error: {e}')
return False
@dp.message(Command("start"))
@dp.message(Command("help"))
async def delete_command(message: types.Message, state: FSMContext):
await state.clear()
await bot.send_message(message.from_user.id,
f'''Отправьте боту сообщение содержащее ссылку или несколько ссылок на web-страницу, бот сконвертирует и отправит вам pdf-файл.
\nПо ошибкам и вопросам писать: {AUTHOR}\n<a href="https://github.com/N0Fanru/URL-to-pdf-telebot">Исходный код бота</a>''',
parse_mode=ParseMode.HTML, disable_web_page_preview=True)
@dp.message()
async def echo_message(message: types.Message, state: FSMContext):
if message.chat.type == 'private':
if await state.get_data() != {}:
await bot.send_message(message.from_user.id, "Сначала дождитесь завершения предыдущих конвертаций.")
return 0
urls = re.findall(url_pattern, message.text)
if message.entities:
for entity in message.entities:
if entity.type == "text_link":
urls.append(entity.url)
if len(urls) > MAX_URLS:
await bot.send_message(message.from_user.id, f"Максимальное количество ссылок на обработку: {MAX_URLS}")
elif len(urls) == 0:
state.clear()
await bot.send_message(message.from_user.id, "Ни одного url не найдено")
else:
await state.update_data(urls=urls)
markup = types.InlineKeyboardMarkup(inline_keyboard=[
[types.InlineKeyboardButton(text='Обычный', callback_data='normal'),
types.InlineKeyboardButton(text='Для телефона', callback_data='mobile')],
[types.InlineKeyboardButton(text='Отмена', callback_data='cancel')]
])
await bot.send_message(message.from_user.id, f"Найдено {len(urls)} ссылок, выберите режим.", reply_markup=markup)
@dp.message(Command("cancel"))
async def callback_cancel(message: types.Message, state: FSMContext):
await state.clear()
await bot.send_message(message.from_user.id, "Действие отменено.")
@dp.callback_query(F.data == 'cancel')
async def callback_cancel(call: types.CallbackQuery, state: FSMContext):
try:
await state.clear()
await call.message.edit_text("Действие отменено.")
except TelegramBadRequest:
await call.answer("Сообщение устарело")
async def convert_and_send(urls: list, id: int, mes_id: int, mobile: bool, state):
await bot.delete_message(id, mes_id)
n = 0
com = 0
if len(urls) > 1 and len(urls) <= MAX_URLS:
name = f'archive-{id}{mes_id}.zip'
zipf = zipfile.ZipFile(name, 'w', zipfile.ZIP_DEFLATED)
for url in urls:
n += 1
m = await bot.send_message(id, f"Процесс над файлом номер {n}, это может занять некоторое время.")
file = await convert_url_to_pdf(url, id, n, m.message_id, mobile)
if file:
zipf.write(file)
os.remove(file)
com += 1
zipf.close()
await bot.send_document(
chat_id=id,
document=FSInputFile(name)
)
os.remove(name)
await bot.send_message(id, f"Успешно сконвертировано {com} из {n} ссылок")
elif len(urls) == 1:
m = await bot.send_message(id, f"Процесс над файлом, это может занять некоторое время.")
file = await convert_url_to_pdf(urls[0], id, "", m.message_id, mobile)
if file:
await bot.send_document(
chat_id=id,
document=FSInputFile(file)
)
os.remove(file)
await state.clear()
@dp.callback_query(F.data == 'normal')
async def callback_normal(call: types.CallbackQuery, state: FSMContext):
data = await state.get_data()
await convert_and_send(data.get('urls'), call.from_user.id, call.message.message_id, False, state)
@dp.callback_query(F.data == 'mobile')
async def callback_mobile(call: types.CallbackQuery, state: FSMContext):
data = await state.get_data()
await convert_and_send(data.get('urls'), call.from_user.id, call.message.message_id, True, state)
async def main():
await dp.start_polling(bot)
if __name__ == "__main__":
asyncio.run(main())