import requests,bs4,sys,subprocess,os,base64,re,ast,json
from bs4 import BeautifulSoup
from urllib.parse import unquote

proxies = {
    'http': '',      # HTTP代理
    'https': '',     # HTTPS代理
}

url = input('url:')


burp0_headers = {"Connection": "close", "Pragma": "no-cache", "Cache-Control": "no-cache", "sec-ch-ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "DNT": "1", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36", "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", "Sec-Fetch-Site": "same-origin", "Sec-Fetch-Mode": "navigate", "Sec-Fetch-User": "?1", "Sec-Fetch-Dest": "document", "Referer": "https://xn--12cmb2cha4rsb7e.com/%E0%B8%84%E0%B8%A5%E0%B8%9B%E0%B8%AB%E0%B8%A5%E0%B8%94%E0%B8%97%E0%B8%B2%E0%B8%87%E0%B8%9A%E0%B8%B2%E0%B8%99-%E0%B8%84%E0%B8%A3%E0%B8%81-5/", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.9,ja;q=0.8,ru;q=0.7"}

r = requests.get(url, headers=burp0_headers, proxies=proxies)

soup = BeautifulSoup(r.text, 'html.parser')

# 获取所有 iframe 标签
iframes = soup.find_all('iframe')
iframes_url = iframes[2].get('src')
print(iframes_url)


r = requests.get(iframes_url, headers=burp0_headers, proxies=proxies)

# 解析 HTML
soup = BeautifulSoup(r.text, 'html.parser')
# 获取所有 iframe 标签
iframes = soup.find_all('iframe')
iframes_url = iframes[0].get('src')
print(iframes_url)


r = requests.get(iframes_url, headers=burp0_headers, proxies=proxies)
#print(r.text)

# 解析 HTML
soup = BeautifulSoup(r.text, 'html.parser')
# 查找所有 <script> 标签
scripts = soup.find_all('script')

for script in scripts:
    src = script.get('src')
    #print(src)
    if src and src.startswith('data:text/javascript;base64,'):
        # 提取 base64 内容
        base64_data = src.split(',', 1)[1]
        # 解码
        decoded_js = base64.b64decode(base64_data).decode('utf-8')
        print(decoded_js)
        break

# 提取 JS 对象部分
match = re.search(r'const\s+\w+\s*=\s*(\{.*\});?', decoded_js, re.DOTALL)
if not match:
    raise ValueError("无法匹配 JS 对象")

js_object_str = match.group(1)

# 将字段名加上双引号，使其变成合法 JSON
# 替换类似：uuid: 'xxx'  →  "uuid": "xxx"
json_like_str = re.sub(r"(\w+)\s*:\s*'([^']*)'", r'"\1": "\2"', js_object_str)

# 使用 json.loads 解析
python_dict = json.loads(json_like_str)
downUrl = python_dict['file']
print(downUrl)


name = unquote(url.split('/')[-2])
print(name)

p = subprocess.Popen(['yt-dlp', '--concurrent-fragments', '32', downUrl, '-o', name+".mp4"], stdout=subprocess.PIPE, text=True) 
for line in p.stdout:
    print(line.strip())


