add support for multilink download and rate limiting
This commit is contained in:
1 parent
bda6bb84d0
commit
d3b391e529
1 file changed
+41
-6
@@ -23,6 +23,10 @@ YT_DLP_PATH = './yt-dlp'
|
|||||||
AUTH_PASSWORD = os.getenv('BOT_AUTH_PASSWORD')
|
AUTH_PASSWORD = os.getenv('BOT_AUTH_PASSWORD')
|
||||||
AUTHORIZED_USERS_FILE = 'authorized_users.txt'
|
AUTHORIZED_USERS_FILE = 'authorized_users.txt'
|
||||||
|
|
||||||
|
# Rate limiting configuration
|
||||||
|
RATE_LIMIT_WINDOW = 60 # Time window in seconds
|
||||||
|
MAX_REQUESTS_PER_WINDOW = 5 # Maximum requests allowed per user in the time window
|
||||||
|
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||||
@@ -49,6 +53,28 @@ def save_authorized_users():
|
|||||||
def is_user_authorized(user_id: int) -> bool:
|
def is_user_authorized(user_id: int) -> bool:
|
||||||
return str(user_id) in AUTHORIZED_USERS
|
return str(user_id) in AUTHORIZED_USERS
|
||||||
|
|
||||||
|
# Rate limiting tracker
|
||||||
|
user_request_tracker = {}
|
||||||
|
|
||||||
|
# Check if user is rate-limited
|
||||||
|
def is_rate_limited(user_id: int) -> bool:
|
||||||
|
current_time = time.time()
|
||||||
|
if user_id not in user_request_tracker:
|
||||||
|
user_request_tracker[user_id] = []
|
||||||
|
|
||||||
|
# Remove old requests outside the time window
|
||||||
|
user_request_tracker[user_id] = [
|
||||||
|
t for t in user_request_tracker[user_id] if current_time - t <= RATE_LIMIT_WINDOW
|
||||||
|
]
|
||||||
|
|
||||||
|
# Check if the user has exceeded the limit
|
||||||
|
if len(user_request_tracker[user_id]) >= MAX_REQUESTS_PER_WINDOW:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# Add the current request timestamp
|
||||||
|
user_request_tracker[user_id].append(current_time)
|
||||||
|
return False
|
||||||
|
|
||||||
# Command: /start
|
# Command: /start
|
||||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
await update.message.reply_text("Welcome! Use /help to see available commands.")
|
await update.message.reply_text("Welcome! Use /help to see available commands.")
|
||||||
@@ -60,7 +86,7 @@ Available Commands:
|
|||||||
/start - Start the bot
|
/start - Start the bot
|
||||||
/help - Show this help message
|
/help - Show this help message
|
||||||
/authenticate <password> - Authenticate with the bot using a password
|
/authenticate <password> - Authenticate with the bot using a password
|
||||||
Send a TikTok video URL to download the video.
|
Send one or more TikTok video URLs (separated by new lines) to download the videos.
|
||||||
"""
|
"""
|
||||||
await update.message.reply_text(help_text)
|
await update.message.reply_text(help_text)
|
||||||
|
|
||||||
@@ -88,15 +114,24 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||||||
await update.message.reply_text("You are not authorized. Use /authenticate <password> to authorize.")
|
await update.message.reply_text("You are not authorized. Use /authenticate <password> to authorize.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# Check rate limiting
|
||||||
|
if is_rate_limited(user_id):
|
||||||
|
await update.message.reply_text("You are sending too many requests. Please try again later.")
|
||||||
|
return
|
||||||
|
|
||||||
message = update.message
|
message = update.message
|
||||||
if not message or not message.text:
|
if not message or not message.text:
|
||||||
return
|
return
|
||||||
|
|
||||||
url = message.text.strip()
|
# Split the message into multiple lines and process each URL
|
||||||
if not is_valid_tiktok_url(url):
|
urls = message.text.strip().split('\n')
|
||||||
await message.reply_text("Please send a valid TikTok video URL.")
|
valid_urls = [url.strip() for url in urls if is_valid_tiktok_url(url.strip())]
|
||||||
|
|
||||||
|
if not valid_urls:
|
||||||
|
await message.reply_text("Please send valid TikTok video URLs.")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
for url in valid_urls:
|
||||||
video_path = None
|
video_path = None
|
||||||
try:
|
try:
|
||||||
video_path = await download_video(url)
|
video_path = await download_video(url)
|
||||||
@@ -108,7 +143,6 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||||||
try:
|
try:
|
||||||
await message.reply_video(video=video_file)
|
await message.reply_video(video=video_file)
|
||||||
except Exception as send_error:
|
except Exception as send_error:
|
||||||
# logger.error(f"Error sending video: {str(send_error)}")
|
|
||||||
# Check file size
|
# Check file size
|
||||||
file_size = os.path.getsize(video_path) / (1024 * 1024) # Size in MB
|
file_size = os.path.getsize(video_path) / (1024 * 1024) # Size in MB
|
||||||
if file_size > 50:
|
if file_size > 50:
|
||||||
@@ -123,7 +157,8 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|||||||
finally:
|
finally:
|
||||||
if video_path and os.path.exists(video_path):
|
if video_path and os.path.exists(video_path):
|
||||||
try:
|
try:
|
||||||
os.remove(video_path)
|
print("skipping delete")
|
||||||
|
# os.remove(video_path)
|
||||||
except Exception as del_error:
|
except Exception as del_error:
|
||||||
logger.error(f"Error removing file {video_path}: {str(del_error)}")
|
logger.error(f"Error removing file {video_path}: {str(del_error)}")
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user