nexacore commited on
Commit
608d13b
·
verified ·
1 Parent(s): 82d3ff4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -6,12 +6,15 @@ 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
@@ -23,21 +26,35 @@ def get_buzz_info(buzz_url):
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}")
@@ -49,6 +66,9 @@ def home():
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")
 
6
 
7
  app = FastAPI()
8
 
9
+ # Use a session to persist cookies automatically during the extraction phase
10
+ session = requests.Session()
11
+ session.headers.update({"User-Agent": "Mozilla/5.0"})
12
 
13
  def get_buzz_info(buzz_url):
14
  try:
15
  # 1. Fetch page to extract filename
16
+ # We use the session object so cookies are stored
17
+ r = session.get(buzz_url, timeout=10)
18
  r.raise_for_status()
19
 
20
  # Regex for filename
 
26
 
27
  # 2. Get flashbang URL (The direct download link)
28
  dl_url = buzz_url.rstrip("/") + "/download"
 
29
 
30
+ # Add required headers for the HTMX request
31
+ h = {
32
+ "HX-Request": "true",
33
+ "Referer": buzz_url
34
+ }
35
+
36
+ # Perform the request to get the download link
37
+ r2 = session.get(dl_url, headers=h, timeout=10, allow_redirects=False)
38
 
 
39
  link = r2.headers.get("hx-redirect")
40
 
41
  if not link:
42
+ # Fallback if status is 200 but no redirect header
43
  if r2.status_code == 200:
44
+ print("Warning: No redirect, but got 200 OK.")
45
  else:
46
  raise ValueError(f"No hx-redirect header found. Status: {r2.status_code}")
47
+
48
+ # 3. Extract Cookies to pass to the Worker
49
+ # Convert cookie jar to a standard "Cookie: name=value;" string
50
+ cookie_dict = session.cookies.get_dict()
51
+ cookie_string = "; ".join([f"{k}={v}" for k, v in cookie_dict.items()])
52
+
53
+ return {
54
+ "filename": filename,
55
+ "download_url": link,
56
+ "cookies": cookie_string # <--- NEW: Sending cookies back
57
+ }
58
 
59
  except Exception as e:
60
  print(f"Error: {e}")
 
66
 
67
  @app.get("/resolve")
68
  def resolve_url(url: str):
69
+ # Clear cookies between requests to ensure a fresh session for every user
70
+ session.cookies.clear()
71
+
72
  data = get_buzz_info(url)
73
  if not data:
74
  raise HTTPException(status_code=500, detail="Failed to resolve URL")