-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtt_download_bot.py
80 lines (59 loc) · 2.32 KB
/
tt_download_bot.py
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
import logging
import time
import os
from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.types import InlineQuery, InputTextMessageContent, InlineQueryResultArticle, InlineQueryResultVideo, InlineQueryResultAudio
from re import findall
from httpx import AsyncClient
from hashlib import md5
from tt_video import tt_videos_or_images, convert_image, divide_chunks, yt_dlp, get_url_of_yt_dlp
from settings import languages, API_TOKEN
storage = MemoryStorage()
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot, storage=storage)
def is_tool(name):
from shutil import which
return which(name) is not None
def get_user_lang(locale):
user_lang = locale.language
if user_lang not in languages:
user_lang = "en"
return user_lang
@dp.message_handler(commands=['start', 'help'])
@dp.throttled(rate=2)
async def send_welcome(message: types.Message):
user_lang = get_user_lang(message.from_user.locale)
await message.reply(languages[user_lang]["help"])
@dp.message_handler(regexp='https://\w{1,3}?\.?\w+\.\w{1,3}/')
@dp.throttled(rate=3)
async def tt_download2(message: types.Message):
user_lang = get_user_lang(message.from_user.locale)
await message.reply(languages[user_lang]["wait"])
link = findall(r'\bhttps?://.*\w{1,30}\S+', message.text)[0]
try:
response = await yt_dlp(link)
if response.endswith(".mp3"):
await message.reply_audio(open(response, 'rb'), caption='@XLR_TT_BOT', title=link)
# video
else:
logging.info(f"VIDEO: {response}")
await message.reply_video(open(response, 'rb'), caption='@XLR_TT_BOT',)
os.remove(response)
except Exception as e:
logging.error(e)
await message.reply(f"error: {e}")
os.remove(response)
@dp.message_handler()
@dp.throttled(rate=3)
async def echo(message: types.Message):
user_lang = get_user_lang(message.from_user.locale)
await message.answer(languages[user_lang]["invalid_link"])
if __name__ == '__main__':
if is_tool("yt-dlp"):
logging.info("yt-dlp installed")
executor.start_polling(dp, skip_updates=True)
else:
logging.error("yt-dlp not installed! Run: sudo apt install yt-dlp")