77 lines
2.0 KiB
Python
77 lines
2.0 KiB
Python
import uvloop
|
|
|
|
from pyrogram import Client, filters
|
|
from pyrogram.types import Message
|
|
|
|
from telegram_downloader_bot.logger import log
|
|
from telegram_downloader_bot.telemetry import init_telemetry
|
|
from telegram_downloader_bot import utils, security
|
|
from telegram_downloader_bot.settings import settings
|
|
|
|
uvloop.install()
|
|
|
|
if settings.app_env == "production":
|
|
log.info("Starting telemetry server, in production mode.")
|
|
init_telemetry()
|
|
else:
|
|
log.info("Not starting telemetry server, not in production mode.")
|
|
|
|
app = Client("downloader_bot",
|
|
api_id=settings.api_id,
|
|
api_hash=settings.api_hash,
|
|
bot_token=settings.bot_token,
|
|
workers=settings.workers)
|
|
|
|
|
|
@app.on_message(filters.command("start"))
|
|
@security.protected
|
|
async def start_handler(_, message: Message):
|
|
await message.reply_text(
|
|
"This bot downloads TikTok videos to my personal server"
|
|
)
|
|
|
|
|
|
@app.on_message(filters.command("help"))
|
|
@security.protected
|
|
async def help_handler(_, message: Message):
|
|
await message.reply_text("I won't help you!")
|
|
|
|
|
|
@app.on_message(filters.text)
|
|
@security.protected
|
|
async def message_handler(_, message: Message):
|
|
|
|
urls = utils.extract_urls(message.text)
|
|
|
|
if not urls:
|
|
return await message.reply_text(
|
|
"No links found in the message. Nothing to download!"
|
|
)
|
|
|
|
tt_urls = utils.filter_tt_urls(urls)
|
|
|
|
if not tt_urls:
|
|
return await message.reply_text(
|
|
"No TikTok URLs found! Nothing to download!"
|
|
)
|
|
|
|
for i, url in enumerate(urls):
|
|
msg = f"Downloading video {i+1}/{len(urls)}..."
|
|
log.info(msg)
|
|
await message.reply_text(msg)
|
|
status = await utils.download_tt_video(url)
|
|
|
|
await message.reply_text(f"Done. {status}")
|
|
|
|
|
|
@app.on_message(filters.media)
|
|
@security.protected
|
|
async def media_handler(client, message: Message):
|
|
await message.reply_text("Downloading media...")
|
|
await utils.handle_media_message_contents(client, message)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
utils.make_fs(settings.storage)
|
|
app.run()
|