ytp-dl 0.6.9__tar.gz → 0.7.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ytp-dl
3
- Version: 0.6.9
3
+ Version: 0.7.0
4
4
  Summary: YouTube video downloader with Mullvad VPN integration and Flask API
5
5
  Home-page: https://github.com/yourusername/ytp-dl
6
6
  Author: dumgum82
@@ -57,7 +57,7 @@ A lightweight YouTube downloader with Mullvad VPN integration and an HTTP API.
57
57
  ## Installation
58
58
 
59
59
  ```bash
60
- pip install ytp-dl==0.6.9 yt-dlp[default]
60
+ pip install ytp-dl==0.7.0 yt-dlp[default]
61
61
  ```
62
62
 
63
63
  Requirements:
@@ -242,7 +242,7 @@ When Mullvad connects/disconnects, Linux routing can change in a way that breaks
242
242
 
243
243
  * Installs Python, FFmpeg, Mullvad CLI, and Deno
244
244
  * Creates a virtualenv at `/opt/yt-dlp-mullvad/venv`
245
- * Installs `ytp-dl==0.6.9` + `yt-dlp[default]` + `gunicorn`
245
+ * Installs `ytp-dl==0.7.0` + `yt-dlp[default]` + `gunicorn`
246
246
  * Installs a policy-routing oneshot service to keep the public API reachable
247
247
  * Sets up a systemd service on port 5000
248
248
  * Runs Gunicorn with `gthread` (threaded) workers
@@ -258,7 +258,7 @@ Note: `gthread` is a built-in Gunicorn worker class (no extra Python dependency)
258
258
  # - Installs Deno system-wide (JS runtime required for modern YouTube extraction via yt-dlp)
259
259
  # - Configures policy routing so the public API stays reachable while Mullvad toggles
260
260
  # - Creates a virtualenv at /opt/yt-dlp-mullvad/venv
261
- # - Installs ytp-dl==0.6.9 + yt-dlp[default] + gunicorn in that venv
261
+ # - Installs ytp-dl==0.7.0 + yt-dlp[default] + gunicorn in that venv
262
262
  # - Creates a systemd service ytp-dl-api.service on port 5000
263
263
  #
264
264
  # Mullvad connect/disconnect is handled per-job by downloader.py.
@@ -394,7 +394,7 @@ mkdir -p "${APP_DIR}"
394
394
  python3 -m venv "${VENV_DIR}"
395
395
  source "${VENV_DIR}/bin/activate"
396
396
  pip install --upgrade pip
397
- pip install "ytp-dl==0.6.9" "yt-dlp[default]" gunicorn
397
+ pip install "ytp-dl==0.7.0" "yt-dlp[default]" gunicorn
398
398
  deactivate
399
399
 
400
400
  echo "==> 3) API environment file (/etc/default/ytp-dl-api)"
@@ -24,7 +24,7 @@ A lightweight YouTube downloader with Mullvad VPN integration and an HTTP API.
24
24
  ## Installation
25
25
 
26
26
  ```bash
27
- pip install ytp-dl==0.6.9 yt-dlp[default]
27
+ pip install ytp-dl==0.7.0 yt-dlp[default]
28
28
  ```
29
29
 
30
30
  Requirements:
@@ -209,7 +209,7 @@ When Mullvad connects/disconnects, Linux routing can change in a way that breaks
209
209
 
210
210
  * Installs Python, FFmpeg, Mullvad CLI, and Deno
211
211
  * Creates a virtualenv at `/opt/yt-dlp-mullvad/venv`
212
- * Installs `ytp-dl==0.6.9` + `yt-dlp[default]` + `gunicorn`
212
+ * Installs `ytp-dl==0.7.0` + `yt-dlp[default]` + `gunicorn`
213
213
  * Installs a policy-routing oneshot service to keep the public API reachable
214
214
  * Sets up a systemd service on port 5000
215
215
  * Runs Gunicorn with `gthread` (threaded) workers
@@ -225,7 +225,7 @@ Note: `gthread` is a built-in Gunicorn worker class (no extra Python dependency)
225
225
  # - Installs Deno system-wide (JS runtime required for modern YouTube extraction via yt-dlp)
226
226
  # - Configures policy routing so the public API stays reachable while Mullvad toggles
227
227
  # - Creates a virtualenv at /opt/yt-dlp-mullvad/venv
228
- # - Installs ytp-dl==0.6.9 + yt-dlp[default] + gunicorn in that venv
228
+ # - Installs ytp-dl==0.7.0 + yt-dlp[default] + gunicorn in that venv
229
229
  # - Creates a systemd service ytp-dl-api.service on port 5000
230
230
  #
231
231
  # Mullvad connect/disconnect is handled per-job by downloader.py.
@@ -361,7 +361,7 @@ mkdir -p "${APP_DIR}"
361
361
  python3 -m venv "${VENV_DIR}"
362
362
  source "${VENV_DIR}/bin/activate"
363
363
  pip install --upgrade pip
364
- pip install "ytp-dl==0.6.9" "yt-dlp[default]" gunicorn
364
+ pip install "ytp-dl==0.7.0" "yt-dlp[default]" gunicorn
365
365
  deactivate
366
366
 
367
367
  echo "==> 3) API environment file (/etc/default/ytp-dl-api)"
@@ -0,0 +1,346 @@
1
+ #!/usr/bin/env python3
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import os
6
+ import re
7
+ import shutil
8
+ import tempfile
9
+ import time
10
+ from threading import BoundedSemaphore, Lock
11
+
12
+ from flask import Flask, Response, jsonify, request, send_file, stream_with_context
13
+
14
+ from .downloader import (
15
+ download_video,
16
+ download_video_stream,
17
+ validate_environment,
18
+ )
19
+
20
+ app = Flask(__name__)
21
+
22
+ BASE_DOWNLOAD_DIR = os.environ.get("YTPDL_JOB_BASE_DIR", "/root/ytpdl_jobs")
23
+ os.makedirs(BASE_DOWNLOAD_DIR, exist_ok=True)
24
+
25
+ MAX_CONCURRENT = int(os.environ.get("YTPDL_MAX_CONCURRENT", "1"))
26
+
27
+ # Thread-safe concurrency gate (caps actual download jobs).
28
+ _sem = BoundedSemaphore(MAX_CONCURRENT)
29
+
30
+ # Track in-flight jobs for /healthz reporting.
31
+ _in_use = 0
32
+ _in_use_lock = Lock()
33
+
34
+ # Failsafe: delete abandoned job dirs older than this many seconds.
35
+ STALE_JOB_TTL_S = int(os.environ.get("YTPDL_STALE_JOB_TTL_S", "3600"))
36
+
37
+ _ALLOWED_EXTENSIONS = {"mp3", "mp4", "best"}
38
+
39
+ _JOB_ID_RX = re.compile(r"^[A-Za-z0-9_-]{1,64}$")
40
+
41
+
42
+ def _cleanup_stale_jobs() -> None:
43
+ now = time.time()
44
+ try:
45
+ for name in os.listdir(BASE_DOWNLOAD_DIR):
46
+ p = os.path.join(BASE_DOWNLOAD_DIR, name)
47
+ if not os.path.isdir(p):
48
+ continue
49
+ try:
50
+ age = now - os.path.getmtime(p)
51
+ except Exception:
52
+ continue
53
+ if age > STALE_JOB_TTL_S:
54
+ shutil.rmtree(p, ignore_errors=True)
55
+ except Exception:
56
+ pass
57
+
58
+
59
+ def _safe_job_id(raw: str | None) -> str:
60
+ s = (raw or "").strip()
61
+ if _JOB_ID_RX.match(s):
62
+ return s
63
+ # fallback to timestamp-based id (stable & filesystem-safe)
64
+ return str(int(time.time() * 1000))
65
+
66
+
67
+ def _try_acquire_job_slot() -> bool:
68
+ global _in_use
69
+ if not _sem.acquire(blocking=False):
70
+ return False
71
+ with _in_use_lock:
72
+ _in_use += 1
73
+ return True
74
+
75
+
76
+ def _release_job_slot() -> None:
77
+ global _in_use
78
+ with _in_use_lock:
79
+ if _in_use > 0:
80
+ _in_use -= 1
81
+ _sem.release()
82
+
83
+
84
+ @app.route("/api/download", methods=["POST"])
85
+ def handle_download():
86
+ """
87
+ Backwards-compatible direct download endpoint:
88
+ - runs yt-dlp
89
+ - returns the file body directly
90
+ """
91
+ _cleanup_stale_jobs()
92
+
93
+ if not _try_acquire_job_slot():
94
+ return jsonify(error="Server busy, try again later"), 503
95
+
96
+ job_dir: str | None = None
97
+ released = False
98
+
99
+ def _release_once() -> None:
100
+ nonlocal released
101
+ if not released:
102
+ released = True
103
+ _release_job_slot()
104
+
105
+ try:
106
+ data = request.get_json(force=True)
107
+ url = (data.get("url") or "").strip()
108
+ resolution = data.get("resolution")
109
+
110
+ # extension is now a "mode": mp3 | mp4 | best
111
+ extension = (data.get("extension") or "mp4").strip().lower()
112
+
113
+ if not url:
114
+ _release_once()
115
+ return jsonify(error="Missing 'url'"), 400
116
+
117
+ if extension not in _ALLOWED_EXTENSIONS:
118
+ _release_once()
119
+ return jsonify(
120
+ error=f"Invalid 'extension'. Allowed: {sorted(_ALLOWED_EXTENSIONS)}"
121
+ ), 400
122
+
123
+ job_dir = tempfile.mkdtemp(prefix="ytpdl_", dir=BASE_DOWNLOAD_DIR)
124
+
125
+ # yt-dlp work (guarded by semaphore)
126
+ filename = download_video(
127
+ url=url,
128
+ resolution=resolution,
129
+ extension=extension,
130
+ out_dir=job_dir,
131
+ )
132
+
133
+ if not (filename and os.path.exists(filename)):
134
+ raise RuntimeError("Download failed")
135
+
136
+ # Release slot as soon as yt-dlp is done.
137
+ _release_once()
138
+
139
+ response = send_file(filename, as_attachment=True)
140
+
141
+ # Cleanup directory after client finishes consuming the response.
142
+ def _cleanup() -> None:
143
+ try:
144
+ if job_dir:
145
+ shutil.rmtree(job_dir, ignore_errors=True)
146
+ except Exception:
147
+ pass
148
+
149
+ response.call_on_close(_cleanup)
150
+ return response
151
+
152
+ except RuntimeError as e:
153
+ if job_dir:
154
+ shutil.rmtree(job_dir, ignore_errors=True)
155
+ _release_once()
156
+
157
+ msg = str(e)
158
+ if "Mullvad not logged in" in msg:
159
+ return jsonify(error=msg), 503
160
+ return jsonify(error=f"Download failed: {msg}"), 500
161
+
162
+ except Exception as e:
163
+ if job_dir:
164
+ shutil.rmtree(job_dir, ignore_errors=True)
165
+ _release_once()
166
+ return jsonify(error=f"Download failed: {str(e)}"), 500
167
+
168
+
169
+ @app.route("/api/download_sse", methods=["POST"])
170
+ def handle_download_sse():
171
+ """
172
+ New SSE endpoint:
173
+ - streams raw yt-dlp stdout lines in real-time (SSE)
174
+ - stores result.json with the final file path in a stable job dir
175
+ - releases concurrency slot immediately when yt-dlp completes
176
+ - client fetches the final file via /api/fetch/<job_id>
177
+ """
178
+ _cleanup_stale_jobs()
179
+
180
+ if not _try_acquire_job_slot():
181
+ return jsonify(error="Server busy, try again later"), 503
182
+
183
+ released = False
184
+
185
+ def _release_once() -> None:
186
+ nonlocal released
187
+ if not released:
188
+ released = True
189
+ _release_job_slot()
190
+
191
+ try:
192
+ data = request.get_json(force=True)
193
+ url = (data.get("url") or "").strip()
194
+ resolution = data.get("resolution")
195
+ extension = (data.get("extension") or "mp4").strip().lower()
196
+
197
+ if not url:
198
+ _release_once()
199
+ return jsonify(error="Missing 'url'"), 400
200
+
201
+ if extension not in _ALLOWED_EXTENSIONS:
202
+ _release_once()
203
+ return jsonify(
204
+ error=f"Invalid 'extension'. Allowed: {sorted(_ALLOWED_EXTENSIONS)}"
205
+ ), 400
206
+
207
+ job_id = _safe_job_id(data.get("job_id"))
208
+ job_dir = os.path.join(BASE_DOWNLOAD_DIR, job_id)
209
+ shutil.rmtree(job_dir, ignore_errors=True)
210
+ os.makedirs(job_dir, exist_ok=True)
211
+
212
+ result_path = os.path.join(job_dir, "result.json")
213
+ err_path = os.path.join(job_dir, "error.txt")
214
+
215
+ def gen():
216
+ file_path: str | None = None
217
+ try:
218
+ # optional "ready" marker
219
+ yield f"data: [vps_ready] {job_id}\n\n"
220
+
221
+ dl = download_video_stream(
222
+ url=url,
223
+ resolution=resolution,
224
+ extension=extension,
225
+ out_dir=job_dir,
226
+ )
227
+
228
+ # manually iterate so we can capture StopIteration.value (the final path)
229
+ while True:
230
+ try:
231
+ line = next(dl)
232
+ except StopIteration as si:
233
+ file_path = si.value
234
+ break
235
+
236
+ if line:
237
+ yield f"data: {line}\n\n"
238
+
239
+ if not (file_path and os.path.exists(file_path)):
240
+ raise RuntimeError("Download completed but output file not found")
241
+
242
+ # Persist result for /api/fetch/<job_id>
243
+ try:
244
+ with open(result_path, "w", encoding="utf-8") as f:
245
+ json.dump(
246
+ {
247
+ "job_id": job_id,
248
+ "file_path": os.path.abspath(file_path),
249
+ "basename": os.path.basename(file_path),
250
+ "created_ts": time.time(),
251
+ },
252
+ f,
253
+ ensure_ascii=False,
254
+ )
255
+ except Exception:
256
+ pass
257
+
258
+ # Release slot as soon as yt-dlp is done (do NOT hold slot for fetch transfer)
259
+ _release_once()
260
+
261
+ yield f"data: [vps_done] {os.path.basename(file_path)}\n\n"
262
+ yield "data: [vps_end]\n\n"
263
+ return
264
+
265
+ except Exception as e:
266
+ try:
267
+ with open(err_path, "w", encoding="utf-8") as f:
268
+ f.write(str(e))
269
+ except Exception:
270
+ pass
271
+
272
+ _release_once()
273
+ msg = str(e)
274
+ # keep it as a single SSE line
275
+ yield f"data: [vps_error] {msg}\n\n"
276
+ yield "data: [vps_end]\n\n"
277
+ return
278
+
279
+ finally:
280
+ # ensure slot is released even if client disconnects mid-stream
281
+ _release_once()
282
+
283
+ resp = Response(stream_with_context(gen()), content_type="text/event-stream")
284
+ resp.headers["Cache-Control"] = "no-cache"
285
+ resp.headers["X-Accel-Buffering"] = "no"
286
+ return resp
287
+
288
+ except Exception as e:
289
+ _release_once()
290
+ return jsonify(error=f"Download failed: {str(e)}"), 500
291
+
292
+
293
+ @app.route("/api/fetch/<job_id>", methods=["GET"])
294
+ def fetch_job_file(job_id: str):
295
+ """
296
+ Fetch the finished file for a job_id created by /api/download_sse.
297
+ Cleans up the job dir after response is fully consumed.
298
+ """
299
+ job_id = _safe_job_id(job_id)
300
+ job_dir = os.path.join(BASE_DOWNLOAD_DIR, job_id)
301
+ result_path = os.path.join(job_dir, "result.json")
302
+
303
+ if not os.path.exists(result_path):
304
+ return jsonify(error="Job not found or not complete"), 404
305
+
306
+ try:
307
+ with open(result_path, "r", encoding="utf-8") as f:
308
+ meta = json.load(f)
309
+ file_path = meta.get("file_path") or ""
310
+ except Exception:
311
+ return jsonify(error="Job metadata unreadable"), 500
312
+
313
+ file_path = os.path.abspath(file_path)
314
+ if not file_path.startswith(os.path.abspath(job_dir) + os.sep):
315
+ return jsonify(error="Invalid job file path"), 500
316
+
317
+ if not os.path.exists(file_path):
318
+ return jsonify(error="Job file missing"), 404
319
+
320
+ response = send_file(file_path, as_attachment=True)
321
+
322
+ def _cleanup() -> None:
323
+ try:
324
+ shutil.rmtree(job_dir, ignore_errors=True)
325
+ except Exception:
326
+ pass
327
+
328
+ response.call_on_close(_cleanup)
329
+ return response
330
+
331
+
332
+ @app.route("/healthz", methods=["GET"])
333
+ def healthz():
334
+ with _in_use_lock:
335
+ in_use = _in_use
336
+ return jsonify(ok=True, in_use=in_use, capacity=MAX_CONCURRENT), 200
337
+
338
+
339
+ def main():
340
+ validate_environment()
341
+ print("Starting ytp-dl API server...")
342
+ app.run(host="0.0.0.0", port=5000)
343
+
344
+
345
+ if __name__ == "__main__":
346
+ main()
@@ -6,7 +6,8 @@ import shlex
6
6
  import shutil
7
7
  import subprocess
8
8
  import time
9
- from typing import Optional, List, Tuple
9
+ from collections import deque
10
+ from typing import Deque, Generator, List, Optional, Tuple
10
11
 
11
12
  # =========================
12
13
  # Config / constants
@@ -60,6 +61,13 @@ def _tail(out: str) -> str:
60
61
  return txt.strip()
61
62
 
62
63
 
64
+ def _tail_deque(lines: Deque[str]) -> str:
65
+ txt = "\n".join(list(lines)[-_MAX_ERR_LINES:])
66
+ if len(txt) > _MAX_ERR_CHARS:
67
+ txt = txt[-_MAX_ERR_CHARS:]
68
+ return txt.strip()
69
+
70
+
63
71
  def _is_youtube_url(url: str) -> bool:
64
72
  u = (url or "").lower()
65
73
  return any(h in u for h in ("youtube.com", "youtu.be", "youtube-nocookie.com"))
@@ -129,14 +137,19 @@ def _common_flags() -> List[str]:
129
137
  # --no-playlist prevents accidental channel/playlist pulls (and disk blowups)
130
138
  return [
131
139
  "--no-playlist",
132
- "--retries", "10",
133
- "--fragment-retries", "10",
134
- "--retry-sleep", "exp=1:30",
135
- "--user-agent", MODERN_UA,
140
+ "--retries",
141
+ "10",
142
+ "--fragment-retries",
143
+ "10",
144
+ "--retry-sleep",
145
+ "exp=1:30",
146
+ "--user-agent",
147
+ MODERN_UA,
136
148
  "--no-cache-dir",
137
149
  "--ignore-config",
138
150
  "--embed-metadata",
139
- "--sleep-interval", "1",
151
+ "--sleep-interval",
152
+ "1",
140
153
  ]
141
154
 
142
155
 
@@ -249,11 +262,14 @@ def _download_with_format(
249
262
 
250
263
  argv = [
251
264
  YTDLP_BIN,
252
- "-f", fmt,
265
+ "-f",
266
+ fmt,
253
267
  *(_common_flags()),
254
- "--output", out_tpl,
268
+ "--output",
269
+ out_tpl,
255
270
  # Ensure we can reliably pick the final output path.
256
- "--print", "after_move:filepath",
271
+ "--print",
272
+ "after_move:filepath",
257
273
  ]
258
274
 
259
275
  if extract_mp3:
@@ -292,6 +308,108 @@ def _fmt_best(cap: int) -> str:
292
308
  return f"bv*[height<={cap}]+ba/b[height<={cap}]"
293
309
 
294
310
 
311
+ def _download_with_format_stream(
312
+ url: str,
313
+ out_dir: str,
314
+ fmt: str,
315
+ merge_output_format: Optional[str] = None,
316
+ extract_mp3: bool = False,
317
+ ) -> Generator[str, None, str]:
318
+ """
319
+ Stream yt-dlp stdout lines in real-time.
320
+ Yields: raw stdout lines
321
+ Returns (StopIteration.value): absolute final output path
322
+ """
323
+ out_dir = os.path.abspath(out_dir)
324
+ os.makedirs(out_dir, exist_ok=True)
325
+ out_tpl = os.path.join(out_dir, "%(title)s.%(ext)s")
326
+
327
+ argv: List[str] = [
328
+ YTDLP_BIN,
329
+ "-f",
330
+ fmt,
331
+ *(_common_flags()),
332
+ # ensure progress is emitted as real lines (no carriage-return overwrites)
333
+ "--progress",
334
+ "--newline",
335
+ "--output",
336
+ out_tpl,
337
+ "--print",
338
+ "after_move:filepath",
339
+ ]
340
+
341
+ if extract_mp3:
342
+ argv.extend(["--extract-audio", "--audio-format", "mp3"])
343
+
344
+ if merge_output_format:
345
+ argv.extend(["--merge-output-format", merge_output_format])
346
+
347
+ argv.append(url)
348
+
349
+ tail_lines: Deque[str] = deque(maxlen=_MAX_ERR_LINES)
350
+ # Keep only a bounded capture for path extraction.
351
+ cap_lines: Deque[str] = deque(maxlen=2000)
352
+
353
+ proc = subprocess.Popen(
354
+ argv,
355
+ stdout=subprocess.PIPE,
356
+ stderr=subprocess.STDOUT,
357
+ text=True,
358
+ bufsize=1,
359
+ universal_newlines=True,
360
+ )
361
+
362
+ try:
363
+ assert proc.stdout is not None
364
+ for raw in iter(proc.stdout.readline, ""):
365
+ line = (raw or "").rstrip("\n")
366
+ if not line:
367
+ continue
368
+
369
+ tail_lines.append(line)
370
+ cap_lines.append(line)
371
+ yield line
372
+
373
+ proc.stdout.close()
374
+ rc = proc.wait()
375
+
376
+ out_joined = "\n".join(cap_lines)
377
+ path = _extract_final_path(out_joined, out_dir)
378
+
379
+ if path and os.path.exists(path):
380
+ return os.path.abspath(path)
381
+
382
+ # fallback: newest non-temp file in out_dir
383
+ try:
384
+ best_path = None
385
+ best_mtime = -1.0
386
+ for name in os.listdir(out_dir):
387
+ if name.endswith((".part", ".ytdl", ".tmp")):
388
+ continue
389
+ full = os.path.join(out_dir, name)
390
+ if not os.path.isfile(full):
391
+ continue
392
+ mt = os.path.getmtime(full)
393
+ if mt > best_mtime:
394
+ best_mtime = mt
395
+ best_path = full
396
+ if best_path and os.path.exists(best_path):
397
+ return os.path.abspath(best_path)
398
+ except Exception:
399
+ pass
400
+
401
+ if rc != 0:
402
+ raise RuntimeError(f"yt-dlp failed (format: {fmt})\n{_tail_deque(tail_lines)}")
403
+ raise RuntimeError(f"Download completed but output file not found (format: {fmt})\n{_tail_deque(tail_lines)}")
404
+
405
+ finally:
406
+ try:
407
+ if proc.poll() is None:
408
+ proc.kill()
409
+ except Exception:
410
+ pass
411
+
412
+
295
413
  # =========================
296
414
  # Public API
297
415
  # =========================
@@ -361,3 +479,71 @@ def download_video(
361
479
  finally:
362
480
  if _mullvad_present():
363
481
  _run_argv(["mullvad", "disconnect"], check=False)
482
+
483
+
484
+ def download_video_stream(
485
+ url: str,
486
+ resolution: int | None = 1080,
487
+ extension: Optional[str] = None,
488
+ out_dir: str = DEFAULT_OUT_DIR,
489
+ ) -> Generator[str, None, str]:
490
+ """
491
+ Streaming variant for SSE usage.
492
+ Yields raw yt-dlp stdout lines; returns final file path as StopIteration.value.
493
+ """
494
+ if not url:
495
+ raise RuntimeError("Missing URL")
496
+
497
+ out_dir = os.path.abspath(out_dir)
498
+ os.makedirs(out_dir, exist_ok=True)
499
+
500
+ validate_environment()
501
+
502
+ require_mullvad_login()
503
+ mullvad_connect(MULLVAD_LOCATION)
504
+ if not mullvad_wait_connected():
505
+ raise RuntimeError("Mullvad connection failed")
506
+
507
+ try:
508
+ mode = (extension or "mp4").lower().strip()
509
+
510
+ if mode == "mp3":
511
+ return (yield from _download_with_format_stream(
512
+ url=url,
513
+ out_dir=out_dir,
514
+ fmt="bestaudio",
515
+ merge_output_format=None,
516
+ extract_mp3=True,
517
+ ))
518
+
519
+ cap = int(resolution or 1080)
520
+
521
+ if mode == "best":
522
+ try:
523
+ return (yield from _download_with_format_stream(
524
+ url=url,
525
+ out_dir=out_dir,
526
+ fmt=_fmt_best(cap),
527
+ merge_output_format=None,
528
+ extract_mp3=False,
529
+ ))
530
+ except Exception:
531
+ return (yield from _download_with_format_stream(
532
+ url=url,
533
+ out_dir=out_dir,
534
+ fmt=_fmt_mp4_apple_safe(cap),
535
+ merge_output_format="mp4",
536
+ extract_mp3=False,
537
+ ))
538
+
539
+ return (yield from _download_with_format_stream(
540
+ url=url,
541
+ out_dir=out_dir,
542
+ fmt=_fmt_mp4_apple_safe(cap),
543
+ merge_output_format="mp4",
544
+ extract_mp3=False,
545
+ ))
546
+
547
+ finally:
548
+ if _mullvad_present():
549
+ _run_argv(["mullvad", "disconnect"], check=False)
@@ -8,7 +8,7 @@ with open("requirements.txt", "r", encoding="utf-8") as fh:
8
8
 
9
9
  setup(
10
10
  name="ytp-dl",
11
- version="0.6.9",
11
+ version="0.7.0",
12
12
  author="dumgum82",
13
13
  author_email="dumgum42@gmail.com",
14
14
  description="YouTube video downloader with Mullvad VPN integration and Flask API",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ytp-dl
3
- Version: 0.6.9
3
+ Version: 0.7.0
4
4
  Summary: YouTube video downloader with Mullvad VPN integration and Flask API
5
5
  Home-page: https://github.com/yourusername/ytp-dl
6
6
  Author: dumgum82
@@ -57,7 +57,7 @@ A lightweight YouTube downloader with Mullvad VPN integration and an HTTP API.
57
57
  ## Installation
58
58
 
59
59
  ```bash
60
- pip install ytp-dl==0.6.9 yt-dlp[default]
60
+ pip install ytp-dl==0.7.0 yt-dlp[default]
61
61
  ```
62
62
 
63
63
  Requirements:
@@ -242,7 +242,7 @@ When Mullvad connects/disconnects, Linux routing can change in a way that breaks
242
242
 
243
243
  * Installs Python, FFmpeg, Mullvad CLI, and Deno
244
244
  * Creates a virtualenv at `/opt/yt-dlp-mullvad/venv`
245
- * Installs `ytp-dl==0.6.9` + `yt-dlp[default]` + `gunicorn`
245
+ * Installs `ytp-dl==0.7.0` + `yt-dlp[default]` + `gunicorn`
246
246
  * Installs a policy-routing oneshot service to keep the public API reachable
247
247
  * Sets up a systemd service on port 5000
248
248
  * Runs Gunicorn with `gthread` (threaded) workers
@@ -258,7 +258,7 @@ Note: `gthread` is a built-in Gunicorn worker class (no extra Python dependency)
258
258
  # - Installs Deno system-wide (JS runtime required for modern YouTube extraction via yt-dlp)
259
259
  # - Configures policy routing so the public API stays reachable while Mullvad toggles
260
260
  # - Creates a virtualenv at /opt/yt-dlp-mullvad/venv
261
- # - Installs ytp-dl==0.6.9 + yt-dlp[default] + gunicorn in that venv
261
+ # - Installs ytp-dl==0.7.0 + yt-dlp[default] + gunicorn in that venv
262
262
  # - Creates a systemd service ytp-dl-api.service on port 5000
263
263
  #
264
264
  # Mullvad connect/disconnect is handled per-job by downloader.py.
@@ -394,7 +394,7 @@ mkdir -p "${APP_DIR}"
394
394
  python3 -m venv "${VENV_DIR}"
395
395
  source "${VENV_DIR}/bin/activate"
396
396
  pip install --upgrade pip
397
- pip install "ytp-dl==0.6.9" "yt-dlp[default]" gunicorn
397
+ pip install "ytp-dl==0.7.0" "yt-dlp[default]" gunicorn
398
398
  deactivate
399
399
 
400
400
  echo "==> 3) API environment file (/etc/default/ytp-dl-api)"
@@ -1,162 +0,0 @@
1
- #!/usr/bin/env python3
2
- from __future__ import annotations
3
-
4
- import os
5
- import shutil
6
- import tempfile
7
- import time
8
- from threading import BoundedSemaphore, Lock
9
-
10
- from flask import Flask, request, send_file, jsonify
11
-
12
- from .downloader import validate_environment, download_video
13
-
14
- app = Flask(__name__)
15
-
16
- BASE_DOWNLOAD_DIR = os.environ.get("YTPDL_JOB_BASE_DIR", "/root/ytpdl_jobs")
17
- os.makedirs(BASE_DOWNLOAD_DIR, exist_ok=True)
18
-
19
- MAX_CONCURRENT = int(os.environ.get("YTPDL_MAX_CONCURRENT", "1"))
20
-
21
- # Thread-safe concurrency gate (caps actual download jobs).
22
- _sem = BoundedSemaphore(MAX_CONCURRENT)
23
-
24
- # Track in-flight jobs for /healthz reporting.
25
- _in_use = 0
26
- _in_use_lock = Lock()
27
-
28
- # Failsafe: delete abandoned job dirs older than this many seconds.
29
- STALE_JOB_TTL_S = int(os.environ.get("YTPDL_STALE_JOB_TTL_S", "3600"))
30
-
31
- _ALLOWED_EXTENSIONS = {"mp3", "mp4", "best"}
32
-
33
-
34
- def _cleanup_stale_jobs() -> None:
35
- now = time.time()
36
- try:
37
- for name in os.listdir(BASE_DOWNLOAD_DIR):
38
- p = os.path.join(BASE_DOWNLOAD_DIR, name)
39
- if not os.path.isdir(p):
40
- continue
41
- try:
42
- age = now - os.path.getmtime(p)
43
- except Exception:
44
- continue
45
- if age > STALE_JOB_TTL_S:
46
- shutil.rmtree(p, ignore_errors=True)
47
- except Exception:
48
- pass
49
-
50
-
51
- def _try_acquire_job_slot() -> bool:
52
- global _in_use
53
- if not _sem.acquire(blocking=False):
54
- return False
55
- with _in_use_lock:
56
- _in_use += 1
57
- return True
58
-
59
-
60
- def _release_job_slot() -> None:
61
- global _in_use
62
- with _in_use_lock:
63
- if _in_use > 0:
64
- _in_use -= 1
65
- _sem.release()
66
-
67
-
68
- @app.route("/api/download", methods=["POST"])
69
- def handle_download():
70
- _cleanup_stale_jobs()
71
-
72
- if not _try_acquire_job_slot():
73
- return jsonify(error="Server busy, try again later"), 503
74
-
75
- job_dir: str | None = None
76
- released = False
77
-
78
- def _release_once() -> None:
79
- nonlocal released
80
- if not released:
81
- released = True
82
- _release_job_slot()
83
-
84
- try:
85
- data = request.get_json(force=True)
86
- url = (data.get("url") or "").strip()
87
- resolution = data.get("resolution")
88
-
89
- # extension is now a "mode": mp3 | mp4 | best
90
- extension = (data.get("extension") or "mp4").strip().lower()
91
-
92
- if not url:
93
- _release_once()
94
- return jsonify(error="Missing 'url'"), 400
95
-
96
- if extension not in _ALLOWED_EXTENSIONS:
97
- _release_once()
98
- return jsonify(
99
- error=f"Invalid 'extension'. Allowed: {sorted(_ALLOWED_EXTENSIONS)}"
100
- ), 400
101
-
102
- job_dir = tempfile.mkdtemp(prefix="ytpdl_", dir=BASE_DOWNLOAD_DIR)
103
-
104
- # yt-dlp work (guarded by semaphore)
105
- filename = download_video(
106
- url=url,
107
- resolution=resolution,
108
- extension=extension,
109
- out_dir=job_dir,
110
- )
111
-
112
- if not (filename and os.path.exists(filename)):
113
- raise RuntimeError("Download failed")
114
-
115
- # Release slot as soon as yt-dlp is done.
116
- _release_once()
117
-
118
- response = send_file(filename, as_attachment=True)
119
-
120
- # Cleanup directory after client finishes consuming the response.
121
- def _cleanup() -> None:
122
- try:
123
- if job_dir:
124
- shutil.rmtree(job_dir, ignore_errors=True)
125
- except Exception:
126
- pass
127
-
128
- response.call_on_close(_cleanup)
129
- return response
130
-
131
- except RuntimeError as e:
132
- if job_dir:
133
- shutil.rmtree(job_dir, ignore_errors=True)
134
- _release_once()
135
-
136
- msg = str(e)
137
- if "Mullvad not logged in" in msg:
138
- return jsonify(error=msg), 503
139
- return jsonify(error=f"Download failed: {msg}"), 500
140
-
141
- except Exception as e:
142
- if job_dir:
143
- shutil.rmtree(job_dir, ignore_errors=True)
144
- _release_once()
145
- return jsonify(error=f"Download failed: {str(e)}"), 500
146
-
147
-
148
- @app.route("/healthz", methods=["GET"])
149
- def healthz():
150
- with _in_use_lock:
151
- in_use = _in_use
152
- return jsonify(ok=True, in_use=in_use, capacity=MAX_CONCURRENT), 200
153
-
154
-
155
- def main():
156
- validate_environment()
157
- print("Starting ytp-dl API server...")
158
- app.run(host="0.0.0.0", port=5000)
159
-
160
-
161
- if __name__ == "__main__":
162
- main()
File without changes
File without changes