Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
import requests
|
| 3 |
+
import re
|
| 4 |
+
import os
|
| 5 |
+
from urllib.parse import urlparse
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
UA = {"User-Agent": "Mozilla/5.0"}
|
| 10 |
+
|
| 11 |
+
def get_buzz_info(buzz_url):
|
| 12 |
+
try:
|
| 13 |
+
# 1. Fetch page to extract filename
|
| 14 |
+
r = requests.get(buzz_url, headers=UA, timeout=10)
|
| 15 |
+
r.raise_for_status()
|
| 16 |
+
|
| 17 |
+
# Regex for filename
|
| 18 |
+
name = re.search(r'<span class="text-2xl">([^<]+)</span>', r.text)
|
| 19 |
+
if name:
|
| 20 |
+
filename = name.group(1).strip()
|
| 21 |
+
else:
|
| 22 |
+
filename = os.path.basename(urlparse(buzz_url).path) or "buzzheavier_file"
|
| 23 |
+
|
| 24 |
+
# 2. Get flashbang URL (The direct download link)
|
| 25 |
+
dl_url = buzz_url.rstrip("/") + "/download"
|
| 26 |
+
h = {"HX-Request": "true", "Referer": buzz_url, **UA}
|
| 27 |
+
|
| 28 |
+
r2 = requests.get(dl_url, headers=h, timeout=10, allow_redirects=False)
|
| 29 |
+
|
| 30 |
+
# Buzzheavier sends the link in the 'hx-redirect' header
|
| 31 |
+
link = r2.headers.get("hx-redirect")
|
| 32 |
+
|
| 33 |
+
if not link:
|
| 34 |
+
# Fallback: Sometimes it might just work without hx-redirect if the status is 200
|
| 35 |
+
if r2.status_code == 200:
|
| 36 |
+
print("Warning: No redirect, but got 200 OK. URL might be direct.")
|
| 37 |
+
else:
|
| 38 |
+
raise ValueError(f"No hx-redirect header found. Status: {r2.status_code}")
|
| 39 |
+
|
| 40 |
+
return {"filename": filename, "download_url": link}
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print(f"Error: {e}")
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
@app.get("/")
|
| 47 |
+
def home():
|
| 48 |
+
return {"status": "Running", "msg": "Send requests to /resolve?url=..."}
|
| 49 |
+
|
| 50 |
+
@app.get("/resolve")
|
| 51 |
+
def resolve_url(url: str):
|
| 52 |
+
data = get_buzz_info(url)
|
| 53 |
+
if not data:
|
| 54 |
+
raise HTTPException(status_code=500, detail="Failed to resolve URL")
|
| 55 |
+
return data
|