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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
+
from functools import wraps
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from tqdm import tqdm
|
| 5 |
+
import json
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
def start_tor_and_wait(tor_path="tor"):
|
| 9 |
+
process = subprocess.Popen(
|
| 10 |
+
[tor_path],
|
| 11 |
+
stdout=subprocess.PIPE,
|
| 12 |
+
stderr=subprocess.STDOUT,
|
| 13 |
+
text=True,
|
| 14 |
+
bufsize=1
|
| 15 |
+
)
|
| 16 |
+
for line in process.stdout:
|
| 17 |
+
if "Bootstrapped 100% (done)" in line:
|
| 18 |
+
print("✅ Tor fully started")
|
| 19 |
+
return process
|
| 20 |
+
|
| 21 |
+
return None
|
| 22 |
+
|
| 23 |
+
class SocksProxy:
|
| 24 |
+
def __init__(self, proxy_url):
|
| 25 |
+
self.proxies = {"http": proxy_url, "https": proxy_url}
|
| 26 |
+
self._original_request = None
|
| 27 |
+
|
| 28 |
+
def __enter__(self):
|
| 29 |
+
self._original_request = requests.sessions.Session.request
|
| 30 |
+
|
| 31 |
+
def patched_request(session, method, url, **kwargs):
|
| 32 |
+
kwargs.setdefault("proxies", self.proxies)
|
| 33 |
+
return self._original_request(session, method, url, **kwargs)
|
| 34 |
+
|
| 35 |
+
requests.sessions.Session.request = patched_request
|
| 36 |
+
|
| 37 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 38 |
+
requests.sessions.Session.request = self._original_request
|
| 39 |
+
|
| 40 |
+
def use_proxy(proxy_url="socks5h://127.0.0.1:9050"):
|
| 41 |
+
def decorator(func):
|
| 42 |
+
@wraps(func)
|
| 43 |
+
def wrapper(*args, **kwargs):
|
| 44 |
+
with SocksProxy(proxy_url):
|
| 45 |
+
return func(*args, **kwargs)
|
| 46 |
+
return wrapper
|
| 47 |
+
return decorator
|
| 48 |
+
|
| 49 |
+
class YTDownloader:
|
| 50 |
+
@use_proxy()
|
| 51 |
+
def __init__(self):
|
| 52 |
+
self.api = self.get_api()
|
| 53 |
+
|
| 54 |
+
def get_between(self,src,left_str,right_str):
|
| 55 |
+
return src.split(left_str)[1].split(right_str)[0]
|
| 56 |
+
|
| 57 |
+
@use_proxy()
|
| 58 |
+
def __call__(self, id, download_file="output.opus"):
|
| 59 |
+
data = [v for i,v in self.get_yt_audio_data(id).items() if "OPUS-audio" in i][-1]
|
| 60 |
+
return self.download_file(self.get_yt_link(self.get_task_id(data['resource_content'])),download_file)
|
| 61 |
+
|
| 62 |
+
def download_file(self, url, output_file):
|
| 63 |
+
response = requests.get(url, stream=True)
|
| 64 |
+
total_size = int(response.headers.get('content-length', 0))
|
| 65 |
+
chunk_size = 1024 # 1 KB
|
| 66 |
+
|
| 67 |
+
with open(output_file, 'wb') as file, tqdm(
|
| 68 |
+
desc=output_file,
|
| 69 |
+
total=total_size,
|
| 70 |
+
unit='B',
|
| 71 |
+
unit_scale=True,
|
| 72 |
+
unit_divisor=1024,
|
| 73 |
+
) as bar:
|
| 74 |
+
for chunk in response.iter_content(chunk_size=chunk_size):
|
| 75 |
+
if chunk:
|
| 76 |
+
file.write(chunk)
|
| 77 |
+
bar.update(len(chunk))
|
| 78 |
+
return output_file
|
| 79 |
+
|
| 80 |
+
def get_yt_audio_data(self,id):
|
| 81 |
+
return {"-".join([i['quality'],i['format'],i['type']]):i for i in requests.post('https://api.vidssave.com/api/contentsite_api/media/parse', headers={
|
| 82 |
+
'accept': '*/*',
|
| 83 |
+
'accept-language': 'en-GB,en;q=0.9,gu;q=0.8,en-US;q=0.7,hi;q=0.6',
|
| 84 |
+
'content-type': 'application/x-www-form-urlencoded',
|
| 85 |
+
'dnt': '1',
|
| 86 |
+
'origin': 'https://vidssave.com',
|
| 87 |
+
'priority': 'u=1, i',
|
| 88 |
+
'referer': 'https://vidssave.com/',
|
| 89 |
+
'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
|
| 90 |
+
'sec-ch-ua-mobile': '?0',
|
| 91 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 92 |
+
'sec-fetch-dest': 'empty',
|
| 93 |
+
'sec-fetch-mode': 'cors',
|
| 94 |
+
'sec-fetch-site': 'same-site',
|
| 95 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36',
|
| 96 |
+
}, data={
|
| 97 |
+
'auth': self.api,
|
| 98 |
+
'domain': 'api-ak.vidssave.com',
|
| 99 |
+
'origin': 'source',
|
| 100 |
+
'link': f'https://youtu.be/{id}',
|
| 101 |
+
}).json()['data']['resources']}
|
| 102 |
+
|
| 103 |
+
def get_api(self):
|
| 104 |
+
for i in BeautifulSoup(requests.get('https://vidssave.com/yt', headers={
|
| 105 |
+
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
| 106 |
+
'accept-language': 'en-GB,en;q=0.9,gu;q=0.8,en-US;q=0.7,hi;q=0.6',
|
| 107 |
+
'cache-control': 'max-age=0',
|
| 108 |
+
'dnt': '1',
|
| 109 |
+
'if-modified-since': 'Thu, 05 Feb 2026 09:20:05 GMT',
|
| 110 |
+
'priority': 'u=0, i',
|
| 111 |
+
'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
|
| 112 |
+
'sec-ch-ua-mobile': '?0',
|
| 113 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 114 |
+
'sec-fetch-dest': 'document',
|
| 115 |
+
'sec-fetch-mode': 'navigate',
|
| 116 |
+
'sec-fetch-site': 'same-origin',
|
| 117 |
+
'sec-fetch-user': '?1',
|
| 118 |
+
'upgrade-insecure-requests': '1',
|
| 119 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36',
|
| 120 |
+
}).text,"html.parser").find_all("script"):
|
| 121 |
+
try:
|
| 122 |
+
if i.has_attr("async"):
|
| 123 |
+
i = i['src'].split("/")[4]
|
| 124 |
+
if ".js" in i:
|
| 125 |
+
return self.get_between(requests.get("https://vidssave.com/_next/static/chunks/"+i, headers={'sec-ch-ua-platform': '"Windows"', 'Referer': 'https://vidssave.com/yt', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36', 'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"', 'DNT': '1', 'sec-ch-ua-mobile': '?0'}).text,'auth:"','",')
|
| 126 |
+
except:
|
| 127 |
+
pass
|
| 128 |
+
|
| 129 |
+
def get_task_id(self,resource_id):
|
| 130 |
+
return requests.post('https://api.vidssave.com/api/contentsite_api/media/download', headers={
|
| 131 |
+
'accept': '*/*',
|
| 132 |
+
'accept-language': 'en-GB,en;q=0.9,gu;q=0.8,en-US;q=0.7,hi;q=0.6',
|
| 133 |
+
'content-type': 'application/x-www-form-urlencoded',
|
| 134 |
+
'dnt': '1',
|
| 135 |
+
'origin': 'https://vidssave.com',
|
| 136 |
+
'priority': 'u=1, i',
|
| 137 |
+
'referer': 'https://vidssave.com/',
|
| 138 |
+
'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
|
| 139 |
+
'sec-ch-ua-mobile': '?0',
|
| 140 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 141 |
+
'sec-fetch-dest': 'empty',
|
| 142 |
+
'sec-fetch-mode': 'cors',
|
| 143 |
+
'sec-fetch-site': 'same-site',
|
| 144 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36',
|
| 145 |
+
}, data={
|
| 146 |
+
'auth': self.api,
|
| 147 |
+
'domain': 'api-ak.vidssave.com',
|
| 148 |
+
'request': resource_id,
|
| 149 |
+
'no_encrypt': '1',
|
| 150 |
+
}).json()['data']['task_id']
|
| 151 |
+
|
| 152 |
+
def get_yt_link(self,task_id):
|
| 153 |
+
return json.loads(requests.get(
|
| 154 |
+
f'https://api.vidssave.com/sse/contentsite_api/media/download_query?auth={self.api}&domain=api-ak.vidssave.com&task_id={task_id}&download_domain=vidssave.com&origin=content_site',
|
| 155 |
+
headers={
|
| 156 |
+
'accept': 'text/event-stream',
|
| 157 |
+
'accept-language': 'en-GB,en;q=0.9,gu;q=0.8,en-US;q=0.7,hi;q=0.6',
|
| 158 |
+
'cache-control': 'no-cache',
|
| 159 |
+
'dnt': '1',
|
| 160 |
+
'origin': 'https://vidssave.com',
|
| 161 |
+
'priority': 'u=1, i',
|
| 162 |
+
'referer': 'https://vidssave.com/',
|
| 163 |
+
'sec-ch-ua': '"Not(A:Brand";v="8", "Chromium";v="144", "Google Chrome";v="144"',
|
| 164 |
+
'sec-ch-ua-mobile': '?0',
|
| 165 |
+
'sec-ch-ua-platform': '"Windows"',
|
| 166 |
+
'sec-fetch-dest': 'empty',
|
| 167 |
+
'sec-fetch-mode': 'cors',
|
| 168 |
+
'sec-fetch-site': 'same-site',
|
| 169 |
+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36',
|
| 170 |
+
},
|
| 171 |
+
).text.strip().split("\n")[-1].lstrip("data: "))['download_link']
|
| 172 |
+
|
| 173 |
+
import gradio as gr
|
| 174 |
+
client = YTDownloader()
|
| 175 |
+
start_tor_and_wait()
|
| 176 |
+
gr.Interface(client,gr.Textbox(),gr.Audio()).launch()
|