add proxies
Browse files
app.py
CHANGED
|
@@ -7,6 +7,21 @@ import re, shutil, subprocess, textwrap, os, tempfile
|
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import List, Optional
|
| 9 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# ---------------- CONFIG ----------------
|
| 12 |
TITLE_DUR = 3 # seconds
|
|
@@ -35,23 +50,70 @@ def run_cmd(cmd: list[str], timeout: int = 120) -> bool:
|
|
| 35 |
print(f"Command failed:\n{' '.join(cmd)}\nError:\n{e.stderr if hasattr(e, 'stderr') else str(e)}")
|
| 36 |
return False
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def yt_urls(query: str, max_results: int) -> List[str]:
|
| 39 |
-
"""Get YouTube URLs for search query"""
|
| 40 |
try:
|
|
|
|
| 41 |
from youtube_search import YoutubeSearch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
results = YoutubeSearch(query, max_results=max_results).to_dict()
|
|
|
|
|
|
|
| 43 |
return ["https://www.youtube.com" + r["url_suffix"] for r in results]
|
| 44 |
except Exception as e:
|
| 45 |
print(f"YouTube search failed: {e}")
|
| 46 |
return []
|
| 47 |
|
|
|
|
| 48 |
def safe_filename(name: str) -> str:
|
| 49 |
"""Create safe filename"""
|
| 50 |
return re.sub(r"[^\w\-\.]", "_", name)[:50] # Limit length
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def dl_video(url: str, out: Path) -> bool:
|
| 53 |
-
"""Download video with length limit"""
|
| 54 |
out.parent.mkdir(exist_ok=True)
|
|
|
|
|
|
|
| 55 |
cmd = [
|
| 56 |
"yt-dlp",
|
| 57 |
"--match-filter", f"duration<{MAX_VIDEO_LENGTH}",
|
|
@@ -60,10 +122,12 @@ def dl_video(url: str, out: Path) -> bool:
|
|
| 60 |
"-o", str(out),
|
| 61 |
"--no-playlist",
|
| 62 |
"--no-check-certificate",
|
|
|
|
| 63 |
url,
|
| 64 |
]
|
| 65 |
return run_cmd(cmd, timeout=60)
|
| 66 |
|
|
|
|
| 67 |
def make_card(text: str, out: Path, dur: int = TITLE_DUR) -> bool:
|
| 68 |
"""Create title card with text"""
|
| 69 |
# Wrap text for better display
|
|
|
|
| 7 |
from pathlib import Path
|
| 8 |
from typing import List, Optional
|
| 9 |
import gradio as gr
|
| 10 |
+
import random
|
| 11 |
+
|
| 12 |
+
# --- Oxylabs Proxy Configuration ---
|
| 13 |
+
PROXY_USERNAME = "ujwal"
|
| 14 |
+
PROXY_PASSWORD = "xJv4DChht5P6y+u"
|
| 15 |
+
PROXY_COUNTRY = "US"
|
| 16 |
+
|
| 17 |
+
# List of proxy endpoints (Oxylabs DC Proxies)
|
| 18 |
+
PROXY_PORTS = [8001, 8002, 8003, 8004, 8005]
|
| 19 |
+
PROXY_HOST = "dc.oxylabs.io"
|
| 20 |
+
|
| 21 |
+
def get_random_proxy():
|
| 22 |
+
port = random.choice(PROXY_PORTS)
|
| 23 |
+
return f"http://user-{PROXY_USERNAME}-country-{PROXY_COUNTRY}:{PROXY_PASSWORD}@{PROXY_HOST}:{port}"
|
| 24 |
+
|
| 25 |
|
| 26 |
# ---------------- CONFIG ----------------
|
| 27 |
TITLE_DUR = 3 # seconds
|
|
|
|
| 50 |
print(f"Command failed:\n{' '.join(cmd)}\nError:\n{e.stderr if hasattr(e, 'stderr') else str(e)}")
|
| 51 |
return False
|
| 52 |
|
| 53 |
+
# def yt_urls(query: str, max_results: int) -> List[str]:
|
| 54 |
+
# """Get YouTube URLs for search query"""
|
| 55 |
+
# try:
|
| 56 |
+
# from youtube_search import YoutubeSearch
|
| 57 |
+
# results = YoutubeSearch(query, max_results=max_results).to_dict()
|
| 58 |
+
# return ["https://www.youtube.com" + r["url_suffix"] for r in results]
|
| 59 |
+
# except Exception as e:
|
| 60 |
+
# print(f"YouTube search failed: {e}")
|
| 61 |
+
# return []
|
| 62 |
+
|
| 63 |
def yt_urls(query: str, max_results: int) -> List[str]:
|
| 64 |
+
"""Get YouTube URLs for search query via proxy"""
|
| 65 |
try:
|
| 66 |
+
import requests
|
| 67 |
from youtube_search import YoutubeSearch
|
| 68 |
+
|
| 69 |
+
proxy_url = get_random_proxy()
|
| 70 |
+
proxies = {
|
| 71 |
+
"http": proxy_url,
|
| 72 |
+
"https": proxy_url,
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# Monkey-patch YoutubeSearch to use proxy
|
| 76 |
+
original_get = requests.get
|
| 77 |
+
|
| 78 |
+
def proxied_get(*args, **kwargs):
|
| 79 |
+
kwargs["proxies"] = proxies
|
| 80 |
+
kwargs["verify"] = False
|
| 81 |
+
return original_get(*args, **kwargs)
|
| 82 |
+
|
| 83 |
+
requests.get = proxied_get
|
| 84 |
results = YoutubeSearch(query, max_results=max_results).to_dict()
|
| 85 |
+
requests.get = original_get # Restore
|
| 86 |
+
|
| 87 |
return ["https://www.youtube.com" + r["url_suffix"] for r in results]
|
| 88 |
except Exception as e:
|
| 89 |
print(f"YouTube search failed: {e}")
|
| 90 |
return []
|
| 91 |
|
| 92 |
+
|
| 93 |
def safe_filename(name: str) -> str:
|
| 94 |
"""Create safe filename"""
|
| 95 |
return re.sub(r"[^\w\-\.]", "_", name)[:50] # Limit length
|
| 96 |
|
| 97 |
+
# def dl_video(url: str, out: Path) -> bool:
|
| 98 |
+
# """Download video with length limit"""
|
| 99 |
+
# out.parent.mkdir(exist_ok=True)
|
| 100 |
+
# cmd = [
|
| 101 |
+
# "yt-dlp",
|
| 102 |
+
# "--match-filter", f"duration<{MAX_VIDEO_LENGTH}",
|
| 103 |
+
# "-f", "mp4",
|
| 104 |
+
# "--merge-output-format", "mp4",
|
| 105 |
+
# "-o", str(out),
|
| 106 |
+
# "--no-playlist",
|
| 107 |
+
# "--no-check-certificate",
|
| 108 |
+
# url,
|
| 109 |
+
# ]
|
| 110 |
+
# return run_cmd(cmd, timeout=60)
|
| 111 |
+
|
| 112 |
def dl_video(url: str, out: Path) -> bool:
|
| 113 |
+
"""Download video with length limit using rotating proxy"""
|
| 114 |
out.parent.mkdir(exist_ok=True)
|
| 115 |
+
proxy = get_random_proxy()
|
| 116 |
+
|
| 117 |
cmd = [
|
| 118 |
"yt-dlp",
|
| 119 |
"--match-filter", f"duration<{MAX_VIDEO_LENGTH}",
|
|
|
|
| 122 |
"-o", str(out),
|
| 123 |
"--no-playlist",
|
| 124 |
"--no-check-certificate",
|
| 125 |
+
"--proxy", proxy,
|
| 126 |
url,
|
| 127 |
]
|
| 128 |
return run_cmd(cmd, timeout=60)
|
| 129 |
|
| 130 |
+
|
| 131 |
def make_card(text: str, out: Path, dur: int = TITLE_DUR) -> bool:
|
| 132 |
"""Create title card with text"""
|
| 133 |
# Wrap text for better display
|