82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
import os
|
|
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
|
|
|
|
API_ID = os.getenv("API_ID") # Your API ID from my.telegram.org
|
|
API_HASH = os.getenv("API_HASH") # Your API Hash from my.telegram.org
|
|
BOT_TOKEN = os.getenv("BOT_TOKEN") # Your bot token from BotFather
|
|
STORAGE = os.getenv("STORAGE") # Your bot token from BotFather
|
|
|
|
uvloop.install()
|
|
init_telemetry()
|
|
|
|
app = Client("downloader_bot",
|
|
api_id=API_ID,
|
|
api_hash=API_HASH,
|
|
bot_token=BOT_TOKEN,
|
|
workers=1)
|
|
|
|
|
|
@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!"
|
|
)
|
|
|
|
success_count = 0
|
|
for i, url in enumerate(urls):
|
|
msg = f"Downloading video {i+1}/{len(urls)}..."
|
|
log.info(msg)
|
|
await message.reply_text(msg)
|
|
outcome = utils.download_tt_video(STORAGE, url)
|
|
success_count += 1 if outcome else 0
|
|
|
|
await message.reply_text(f"{success_count}/{len(urls)} "
|
|
"video(s) downloaded")
|
|
|
|
|
|
@app.on_message(filters.media)
|
|
@security.protected
|
|
async def media_handler(client, message: Message):
|
|
|
|
await message.reply_text("Downloading media...")
|
|
|
|
utils.handle_media_message_contents(STORAGE, client, message)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
utils.make_fs(STORAGE)
|
|
app.run()
|