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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -21
app.py CHANGED
@@ -2,58 +2,88 @@ 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
- # 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
21
  name = re.search(r'<span class="text-2xl">([^<]+)</span>', r.text)
22
  if name:
23
  filename = name.group(1).strip()
24
  else:
25
  filename = os.path.basename(urlparse(buzz_url).path) or "buzzheavier_file"
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:
@@ -62,13 +92,10 @@ def get_buzz_info(buzz_url):
62
 
63
  @app.get("/")
64
  def home():
65
- return {"status": "Running", "msg": "Send requests to /resolve?url=..."}
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")
 
2
  import requests
3
  import re
4
  import os
5
+ import random
6
  from urllib.parse import urlparse
7
 
8
  app = FastAPI()
9
 
10
+ # --- 1. RANDOMIZATION TOOLS ---
11
+
12
+ # A pool of realistic User-Agents to rotate through
13
+ USER_AGENTS = [
14
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
15
+ "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",
16
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0",
17
+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
18
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/118.0"
19
+ ]
20
+
21
+ def get_random_ip():
22
+ """Generates a random IP address to bypass simple IP counters."""
23
+ return f"{random.randint(1, 200)}.{random.randint(1, 255)}.{random.randint(1, 255)}.{random.randint(1, 255)}"
24
+
25
+ # ------------------------------
26
 
27
  def get_buzz_info(buzz_url):
28
+ session = requests.Session()
29
+
30
+ # Generate a fresh identity for this request
31
+ current_ua = random.choice(USER_AGENTS)
32
+ fake_ip = get_random_ip()
33
+
34
+ # Headers that mimic a unique user coming from a unique IP
35
+ headers = {
36
+ "User-Agent": current_ua,
37
+ "X-Forwarded-For": fake_ip,
38
+ "X-Real-IP": fake_ip,
39
+ "Referer": "https://buzzheavier.com/"
40
+ }
41
+
42
+ session.headers.update(headers)
43
+
44
  try:
45
+ # 1. Fetch page (View Count)
46
+ # We add a random query param to prevent caching on their side: ?cachebuster=123
47
+ url_with_prevention = f"{buzz_url}?cb={random.randint(1, 100000)}"
48
+
49
+ r = session.get(url_with_prevention, timeout=10)
50
  r.raise_for_status()
51
 
 
52
  name = re.search(r'<span class="text-2xl">([^<]+)</span>', r.text)
53
  if name:
54
  filename = name.group(1).strip()
55
  else:
56
  filename = os.path.basename(urlparse(buzz_url).path) or "buzzheavier_file"
57
 
58
+ # 2. Trigger Download (Download Count)
59
  dl_url = buzz_url.rstrip("/") + "/download"
60
 
61
+ # Headers specifically for the download trigger
62
+ dl_headers = {
63
  "HX-Request": "true",
64
+ "Referer": buzz_url,
65
+ "X-Forwarded-For": fake_ip # Keep the IP consistent for this session
66
  }
67
 
68
+ r2 = session.get(dl_url, headers=dl_headers, timeout=10, allow_redirects=False)
 
69
 
70
  link = r2.headers.get("hx-redirect")
71
 
72
  if not link:
 
73
  if r2.status_code == 200:
74
  print("Warning: No redirect, but got 200 OK.")
75
  else:
76
  raise ValueError(f"No hx-redirect header found. Status: {r2.status_code}")
77
 
78
+ # 3. Extract Cookies
 
79
  cookie_dict = session.cookies.get_dict()
80
  cookie_string = "; ".join([f"{k}={v}" for k, v in cookie_dict.items()])
81
 
82
  return {
83
  "filename": filename,
84
  "download_url": link,
85
+ "cookies": cookie_string,
86
+ "user_agent": current_ua # Pass this to Worker so it matches!
87
  }
88
 
89
  except Exception as e:
 
92
 
93
  @app.get("/")
94
  def home():
95
+ return {"status": "Running"}
96
 
97
  @app.get("/resolve")
98
  def resolve_url(url: str):
 
 
 
99
  data = get_buzz_info(url)
100
  if not data:
101
  raise HTTPException(status_code=500, detail="Failed to resolve URL")