-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTidal-dl-bot-v1.py
249 lines (210 loc) Β· 8.82 KB
/
Tidal-dl-bot-v1.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from typing import Optional
import os
from pathlib import Path
import asyncio
import logging
from datetime import datetime
import shutil
import subprocess
from collections import deque
# Modern Python Telegram Bot imports
from telegram import Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters
)
from telegram.constants import ParseMode
# Configure logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
# Configuration
API_ID = Paste here you telegram API_ID
API_HASH = 'Paste here you telegram API_HASH'
BOT_TOKEN = 'Paste here you telegram Bot token'
# Modern Path handling
DOWNLOAD_DIR = Path.home() / 'download' / 'Tracks'
DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
# Queue management with modern typing
class DownloadQueue:
def __init__(self):
self.queue: deque = deque()
self.is_processing: bool = False
self.current_task: Optional[dict] = None
def add_task(self, task: dict) -> int:
self.queue.append(task)
return len(self.queue)
def get_next_task(self) -> Optional[dict]:
return self.queue.popleft() if self.queue else None
def get_position(self, user_id: int) -> Optional[int]:
for i, task in enumerate(self.queue):
if task['user_id'] == user_id:
return i + 1
return None
@property
def length(self) -> int:
return len(self.queue)
# Create queue instance
download_queue = DownloadQueue()
class TidalDownloadBot:
def __init__(self):
self.application = Application.builder().token(BOT_TOKEN).build()
self.setup_handlers()
def setup_handlers(self):
"""Setup command handlers with error handling"""
self.application.add_handler(CommandHandler("start", self.start))
self.application.add_handler(CommandHandler("status", self.status))
self.application.add_handler(CommandHandler("dl", self.dl))
self.application.add_error_handler(self.error_handler)
async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Modern start command handler"""
await update.message.reply_text(
"*Welcome to Tidal Download Bot*\n\n"
"Available commands:\n"
"π΅ `/dl <Tidal URL>` - Download a track\n"
"π `/status` - Check queue status\n\n"
"_Send me a Tidal track URL to get started!_",
parse_mode=ParseMode.MARKDOWN
)
async def status(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Modern status command handler"""
status_message = [
"*π€ Bot Status Report*",
f"π Status: {'Busy' if download_queue.is_processing else 'Idle'}",
]
if download_queue.current_task:
status_message.append(
f"π₯ Currently downloading for: "
f"@{download_queue.current_task.get('username', 'Unknown')}"
)
if download_queue.length > 0:
status_message.extend([
f"π Queue length: {download_queue.length}",
f"π Your position: {download_queue.get_position(update.effective_user.id) or 'Not in queue'}"
])
else:
status_message.append("β
No pending downloads")
await update.message.reply_text(
"\n".join(status_message),
parse_mode=ParseMode.MARKDOWN
)
@staticmethod
def is_valid_tidal_url(url: str) -> bool:
"""Validate Tidal URL with modern string handling"""
return url.startswith('https://tidal.com/browse/track/')
async def process_download(self, task: dict) -> tuple[bool, str]:
"""Process download with modern async subprocess"""
try:
process = await asyncio.create_subprocess_exec(
'tidal-dl-ng', 'dl', task['url'],
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
if process.returncode != 0:
return False, f"Download failed with error: {stderr.decode()}"
# Check for downloaded file
files = list(DOWNLOAD_DIR.glob('*'))
if not files:
return False, "No files found after download"
return True, str(files[0])
except Exception as e:
logger.error(f"Download error: {str(e)}")
return False, f"Download error: {str(e)}"
async def dl(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Modern download command handler"""
if not context.args:
await update.message.reply_text(
"β Please provide a Tidal URL.\n"
"Example: `/dl https://tidal.com/browse/track/12345`",
parse_mode=ParseMode.MARKDOWN
)
return
url = context.args[0]
if not self.is_valid_tidal_url(url):
await update.message.reply_text(
"β Invalid Tidal URL. Please provide a valid Tidal track URL."
)
return
task = {
'url': url,
'user_id': update.effective_user.id,
'username': update.effective_user.username,
'context': context,
'timestamp': datetime.now(),
'message': update.message
}
position = download_queue.add_task(task)
if position == 1 and not download_queue.is_processing:
await update.message.reply_text("β
Download starting immediately!")
asyncio.create_task(self.process_queue())
else:
await update.message.reply_text(
f"π Added to queue. Position: {position}\n"
f"Use /status to check queue status."
)
async def process_queue(self):
"""Modern queue processor with async file handling"""
while True:
if not download_queue.is_processing and download_queue.length > 0:
download_queue.is_processing = True
task = download_queue.get_next_task()
download_queue.current_task = task
try:
status_message = await task['message'].reply_text(
"π΅ Starting download...",
parse_mode=ParseMode.MARKDOWN
)
success, result = await self.process_download(task)
if success:
file_path = Path(result)
await task['context'].bot.send_document(
task['user_id'],
document=file_path.open('rb'),
caption="β
Here's your track!",
filename=file_path.name
)
# Cleanup
file_path.unlink(missing_ok=True)
await status_message.edit_text("β
Download completed!")
else:
await status_message.edit_text(f"β {result}")
except Exception as e:
logger.error(f"Queue processing error: {str(e)}")
await task['message'].reply_text(f"β An error occurred: {str(e)}")
finally:
download_queue.current_task = None
download_queue.is_processing = False
# Clean download directory
if DOWNLOAD_DIR.exists():
shutil.rmtree(DOWNLOAD_DIR)
DOWNLOAD_DIR.mkdir(parents=True, exist_ok=True)
await asyncio.sleep(1)
async def error_handler(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Modern error handler with detailed logging"""
logger.error(f"Update {update} caused error {context.error}", exc_info=context.error)
error_message = "β An error occurred. Please try again later."
if update and update.effective_message:
await update.effective_message.reply_text(error_message)
def run(self):
"""Run the bot with modern async handling"""
logger.info("Starting bot...")
self.application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == '__main__':
try:
# Check for ffmpeg
subprocess.run(['ffmpeg', '-version'], check=True, capture_output=True)
except subprocess.CalledProcessError:
logger.error("FFmpeg is not installed. Please install it first.")
exit(1)
except FileNotFoundError:
logger.error("FFmpeg not found. Please install it first.")
exit(1)
# Start bot
bot = TidalDownloadBot()
bot.run()