-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload.py
More file actions
286 lines (236 loc) · 11.4 KB
/
Copy pathDownload.py
File metadata and controls
286 lines (236 loc) · 11.4 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import asyncio
import websockets
import os
import tempfile
import json
import base64
import re
import requests
import traceback
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, TIT2, TPE1, TALB, APIC
#EXPRESS_WS_URL = Constants.expoConfig.extra.SERVER.replace("http", "ws")/download-progress;
EXPRESS_WS_URL = "ws://127.0.0.1:80/download-progress"
def add_metadata_to_mp3(mp3_path, title, artist=None, album="YouTube Downloads", image_url=None):
try:
# Check if file exists
if not os.path.exists(mp3_path):
print(f"[METADATA ERROR] File does not exist: {mp3_path}")
return False
# Load the MP3 file
audio = MP3(mp3_path, ID3=ID3)
# Add tags if they don't exist
if audio.tags is None:
print("[METADATA] No existing tags found, adding new tags")
audio.add_tags()
else:
print("[METADATA] Existing tags found, updating")
# Clear existing tags to avoid conflicts
audio.tags.clear()
# Add basic metadata
if title:
audio.tags.add(TIT2(encoding=3, text=title))
print(f"[METADATA] Added title: {title}")
if artist:
audio.tags.add(TPE1(encoding=3, text=artist))
print(f"[METADATA] Added artist: {artist}")
if album:
audio.tags.add(TALB(encoding=3, text=album))
print(f"[METADATA] Added album: {album}")
# Add cover art if URL provided
if image_url:
try:
print(f"[METADATA] Downloading cover art from: {image_url}")
# Download image with headers to avoid blocking
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
}
img_response = requests.get(image_url, headers=headers, timeout=10)
img_response.raise_for_status()
img_data = img_response.content
print(f"[METADATA] Downloaded {len(img_data)} bytes of image data")
# Determine MIME type from content or URL
mime_type = "image/jpeg"
if image_url.lower().endswith('.png'):
mime_type = "image/png"
elif image_url.lower().endswith('.webp'):
mime_type = "image/webp"
audio.tags.add(APIC(
encoding=3,
mime=mime_type,
type=3, # Cover (front)
desc="Cover",
data=img_data
))
print(f"[METADATA] Added cover art ({mime_type})")
except requests.exceptions.RequestException as e:
print(f"[METADATA WARNING] Failed to download cover art: {e}")
except Exception as e:
print(f"[METADATA WARNING] Failed to add cover art: {e}")
# Save the tags
audio.save()
print(f"[METADATA] Successfully saved metadata to: {mp3_path}")
except Exception as e:
print(f"[METADATA ERROR] Failed to add metadata: {e}")
print(f"[METADATA ERROR] Traceback: {traceback.format_exc()}")
return False
async def download_youtube_audio(items: list[dict], client_id: str):
print(f"[DEBUG] Starting download for {len(items)} items")
print(f"[DEBUG] Client ID: {client_id}")
print(f"[DEBUG] WebSocket URL: {EXPRESS_WS_URL}")
tmp_dir = tempfile.mkdtemp()
print(f"[DEBUG] Temporary directory: {tmp_dir}")
result = {
"status": "success",
"files": [],
"error": None
}
try:
print("[DEBUG] Attempting to connect to WebSocket...")
# Add connection timeout and better error handling
async with websockets.connect(
EXPRESS_WS_URL,
max_size=2**25,
ping_timeout=20,
ping_interval=30
) as ws:
print("[DEBUG] WebSocket connection established successfully!")
print("[DEBUG] Received URLs (with metadata):")
for i, item in enumerate(items):
print(f" [{i}] Title: {item.get('title')}, Artist: {item.get('uploader') or item.get('artist')}, Image: {item.get('image')}, URL: {item.get('url')}")
for index, item in enumerate(items):
try:
url = item["url"]
title = item.get("title", f"Track {index}")
artist = item.get("uploader") or item.get("artist") or "Unknown Artist"
thumbnail = item.get("image")
print(f"[DEBUG] Processing item {index}: {title} by {artist}")
await ws.send(json.dumps({
"type": "start",
"value": {"index": index, "url": url},
"clientId": client_id
}))
# Create a safer filename template
safe_title = re.sub(r'[<>:"/\\|?*]', '_', title) # Replace invalid chars
audio_filename_template = os.path.join(tmp_dir, f"{safe_title}_{index}.%(ext)s")
print(f"[DEBUG] Output template: {audio_filename_template}")
ytdlp_cmd = [
"yt-dlp",
"-f", "bestaudio",
"-o", audio_filename_template,
"--no-playlist",
"--extract-audio",
"--audio-format", "mp3",
"--newline",
"--no-warnings", # Reduce noise
url
]
print(f"[DEBUG] Running command: {' '.join(ytdlp_cmd)}")
process = await asyncio.create_subprocess_exec(
*ytdlp_cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT
)
# Process yt-dlp output
while True:
line = await process.stdout.readline()
if not line:
break
decoded_line = line.decode().strip()
print(f"[yt-dlp:{index}]:", decoded_line)
# Extract progress
match = re.search(r"\[download\]\s+(\d{1,3}\.\d)%", decoded_line)
if match:
percent = match.group(1)
print(percent)
await ws.send(json.dumps({
"type": "progress",
"value": {"index": index, "percent": percent},
"clientId": client_id
}))
# Wait for process to complete
await process.wait()
print(f"[DEBUG] yt-dlp process completed with return code: {process.returncode}")
# Find the downloaded file
downloaded_file = None
print(f"[DEBUG] Looking for downloaded files in: {tmp_dir}")
for file in os.listdir(tmp_dir):
print(f"[DEBUG] Found file: {file}")
if file.endswith(".mp3") and f"_{index}." in file:
downloaded_file = file
break
if not downloaded_file:
print(f"[ERROR] No MP3 file found for index {index}")
continue
full_path = os.path.join(tmp_dir, downloaded_file)
print(f"[DEBUG] Processing downloaded file: {full_path}")
print(f"[DEBUG] File size: {os.path.getsize(full_path)} bytes")
# Add metadata BEFORE sending file
print(f"[DEBUG] Adding metadata to: {full_path}")
print(f"[DEBUG] Metadata - Title: {title}, Artist: {artist}, Thumbnail: {thumbnail}")
metadata_success = add_metadata_to_mp3(full_path, title, artist, "YouTube Downloads", thumbnail)
if not metadata_success:
print(f"[WARNING] Metadata addition failed for {full_path}")
# Read and encode file
try:
with open(full_path, "rb") as f:
file_data = f.read()
encoded = base64.b64encode(file_data).decode("utf-8")
print(f"[DEBUG] File encoded, size: {len(encoded)} characters")
# Send file via WebSocket
await ws.send(json.dumps({
"type": "file",
"value": {
"index": index,
"filename": downloaded_file,
"data": encoded
},
"clientId": client_id
}))
await ws.send(json.dumps({
"type": "progress",
"value": {"index": index, "percent": 100},
"clientId": client_id
}))
result["files"].append(full_path)
print(f"[DEBUG] Successfully processed item {index}")
except Exception as e:
print(f"[ERROR] Failed to read/encode file {full_path}: {e}")
print(f"[ERROR] Traceback: {traceback.format_exc()}")
except Exception as e:
print(f"[ERROR] Error processing item {index}: {e}")
print(f"[ERROR] Traceback: {traceback.format_exc()}")
continue
print("[DEBUG] All items processed successfully")
return result
except websockets.exceptions.InvalidHandshake as e:
error_msg = f"WebSocket handshake failed: {str(e)}"
print(f"[ERROR] {error_msg}")
result["status"] = "error"
result["error"] = error_msg
return result
except websockets.exceptions.ConnectionClosedError as e:
error_msg = f"WebSocket connection closed: {str(e)}"
print(f"[ERROR] {error_msg}")
result["status"] = "error"
result["error"] = error_msg
return result
except asyncio.TimeoutError as e:
error_msg = "WebSocket connection timed out - check if the WebSocket server is running"
print(f"[ERROR] {error_msg}")
result["status"] = "error"
result["error"] = error_msg
return result
except ConnectionRefusedError as e:
error_msg = f"Connection refused - WebSocket server may not be running at {EXPRESS_WS_URL}"
print(f"[ERROR] {error_msg}")
result["status"] = "error"
result["error"] = error_msg
return result
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
print(f"[ERROR] {error_msg}")
print(f"[ERROR] Full traceback: {traceback.format_exc()}")
result["status"] = "error"
result["error"] = error_msg
return result