File size: 3,131 Bytes
82d3ff4
 
 
 
7090159
82d3ff4
 
 
 
7090159
 
 
852243c
7090159
 
 
 
 
82d3ff4
7090159
 
 
 
d15a20c
 
7090159
852243c
 
 
7090159
d15a20c
82d3ff4
d15a20c
 
82d3ff4
 
d15a20c
 
 
 
 
 
 
82d3ff4
 
d15a20c
 
852243c
 
d15a20c
7090159
852243c
 
 
d15a20c
608d13b
d15a20c
7090159
82d3ff4
d15a20c
 
82d3ff4
d15a20c
 
 
 
 
 
 
608d13b
d15a20c
 
608d13b
 
 
d15a20c
7090159
852243c
608d13b
82d3ff4
 
d15a20c
82d3ff4
 
 
 
d15a20c
82d3ff4
 
 
d15a20c
 
 
82d3ff4
 
d15a20c
82d3ff4
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
from fastapi import FastAPI, HTTPException
import requests
import re
import os
import random
from urllib.parse import urlparse

app = FastAPI()

USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
    "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
]

def get_random_ip():
    return f"{random.randint(1, 200)}.{random.randint(1, 255)}.{random.randint(1, 255)}.{random.randint(1, 255)}"

def get_buzz_info(buzz_url):
    session = requests.Session()
    current_ua = random.choice(USER_AGENTS)
    fake_ip = get_random_ip()
    
    # Common headers
    base_headers = {
        "User-Agent": current_ua,
        "Accept-Language": "en-US,en;q=0.5",
        "X-Forwarded-For": fake_ip, 
        "X-Real-IP": fake_ip
    }

    try:
        # --- Step 1: Visit Page ---
        r = session.get(buzz_url, headers=base_headers, timeout=10)
        r.raise_for_status()
        
        # Robust Filename Extraction
        filename = "buzzheavier_file"
        name_match = re.search(r'<span class="text-2xl">([^<]+)</span>', r.text)
        if name_match:
            filename = name_match.group(1).strip()
        
        # --- Step 2: Trigger HTMX Download ---
        dl_url = buzz_url.rstrip("/") + "/download"
        
        dl_headers = base_headers.copy()
        dl_headers.update({
            "Accept": "*/*",
            "HX-Request": "true",
            "HX-Current-URL": buzz_url,
            "Referer": buzz_url,
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
        })
        
        # Allow redirects=False to catch the 302 location
        r2 = session.get(dl_url, headers=dl_headers, timeout=10, allow_redirects=False)
        
        # Logic: Either 302 Redirect OR JSON response with location
        download_link = None
        
        if r2.status_code in [301, 302, 303, 307, 308]:
            download_link = r2.headers.get("Location")
        elif "hx-redirect" in r2.headers:
            download_link = r2.headers.get("hx-redirect")
            
        if not download_link:
            raise ValueError(f"Could not resolve link. Status: {r2.status_code}")

        # Cookies
        cookie_string = "; ".join([f"{k}={v}" for k, v in session.cookies.get_dict().items()])

        return {
            "filename": filename, 
            "download_url": download_link,
            "cookies": cookie_string,
            "user_agent": current_ua 
        }

    except Exception as e:
        print(f"Resolver Error: {e}")
        return None

@app.get("/")
def home():
    return {"status": "Buzz Resolver Active"}

@app.get("/resolve")
def resolve_url(url: str):
    if not url:
        raise HTTPException(status_code=400, detail="Missing URL")
        
    data = get_buzz_info(url)
    if not data:
        raise HTTPException(status_code=500, detail="Resolution failed")
    return data