Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -28,15 +28,13 @@ ua = UserAgent(fallback='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/5
|
|
| 28 |
MEDIAFIRE_CACHE = {}
|
| 29 |
CACHE_TTL = 1800
|
| 30 |
|
| 31 |
-
|
| 32 |
-
MAX_CONCURRENT_REQUESTS = 20
|
| 33 |
semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
|
| 34 |
|
| 35 |
-
# Global Client with optimized pooling
|
| 36 |
client = httpx.AsyncClient(
|
| 37 |
timeout=httpx.Timeout(30.0, read=None),
|
| 38 |
follow_redirects=True,
|
| 39 |
-
limits=httpx.Limits(max_connections=500, max_keepalive_connections=
|
| 40 |
)
|
| 41 |
|
| 42 |
@app.get("/")
|
|
@@ -80,13 +78,26 @@ async def scrape_mediafire(url):
|
|
| 80 |
r = await temp_client.get(url)
|
| 81 |
if r.status_code != 200: return None
|
| 82 |
|
| 83 |
-
#
|
|
|
|
| 84 |
match = re.search(r'https?://download[^\s"\']+mediafire\.com/[^\s"\']+', r.text)
|
| 85 |
-
if match:
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
except: pass
|
| 91 |
return None
|
| 92 |
|
|
@@ -102,14 +113,13 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 102 |
|
| 103 |
# --- MediaFire Section ---
|
| 104 |
if "mediafire.com" in clean_url:
|
| 105 |
-
async with semaphore:
|
| 106 |
cached = MEDIAFIRE_CACHE.get(clean_url)
|
| 107 |
target_link = cached['link'] if (cached and (current_time - cached['time']) < CACHE_TTL) else None
|
| 108 |
|
| 109 |
if not target_link:
|
| 110 |
target_link = await scrape_mediafire(clean_url)
|
| 111 |
if target_link:
|
| 112 |
-
if target_link.startswith("//"): target_link = f"https:{target_link}"
|
| 113 |
MEDIAFIRE_CACHE[clean_url] = {'link': target_link, 'time': current_time}
|
| 114 |
|
| 115 |
if not target_link:
|
|
@@ -155,6 +165,10 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 155 |
return await stream_file(clean_url, range_header, filename)
|
| 156 |
|
| 157 |
async def stream_file(target_url, range_header, filename, referer=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
headers = {'User-Agent': ua.random}
|
| 159 |
if range_header: headers['Range'] = range_header
|
| 160 |
if referer: headers['Referer'] = referer
|
|
|
|
| 28 |
MEDIAFIRE_CACHE = {}
|
| 29 |
CACHE_TTL = 1800
|
| 30 |
|
| 31 |
+
MAX_CONCURRENT_REQUESTS = 30
|
|
|
|
| 32 |
semaphore = asyncio.Semaphore(MAX_CONCURRENT_REQUESTS)
|
| 33 |
|
|
|
|
| 34 |
client = httpx.AsyncClient(
|
| 35 |
timeout=httpx.Timeout(30.0, read=None),
|
| 36 |
follow_redirects=True,
|
| 37 |
+
limits=httpx.Limits(max_connections=500, max_keepalive_connections=100)
|
| 38 |
)
|
| 39 |
|
| 40 |
@app.get("/")
|
|
|
|
| 78 |
r = await temp_client.get(url)
|
| 79 |
if r.status_code != 200: return None
|
| 80 |
|
| 81 |
+
# Direct link search
|
| 82 |
+
target = None
|
| 83 |
match = re.search(r'https?://download[^\s"\']+mediafire\.com/[^\s"\']+', r.text)
|
| 84 |
+
if match:
|
| 85 |
+
target = match.group(0).strip().replace("'", "").replace('"', '')
|
| 86 |
|
| 87 |
+
if not target:
|
| 88 |
+
soup = BeautifulSoup(r.text, 'html.parser')
|
| 89 |
+
btn = soup.find('a', {'id': 'downloadButton'}) or soup.find('a', {'aria-label': re.compile(r'Download', re.I)})
|
| 90 |
+
if btn: target = btn.get('href')
|
| 91 |
+
|
| 92 |
+
# --- URL Fix Section ---
|
| 93 |
+
if target:
|
| 94 |
+
# အကယ်၍ // နဲ့စရင် https: ထည့်မယ်
|
| 95 |
+
if target.startswith("//"):
|
| 96 |
+
target = f"https:{target}"
|
| 97 |
+
# အကယ်၍ /file/... လို့ Relative path နဲ့လာရင် MediaFire domain ထည့်ပေးမယ်
|
| 98 |
+
elif target.startswith("/"):
|
| 99 |
+
target = f"https://www.mediafire.com{target}"
|
| 100 |
+
return target
|
| 101 |
except: pass
|
| 102 |
return None
|
| 103 |
|
|
|
|
| 113 |
|
| 114 |
# --- MediaFire Section ---
|
| 115 |
if "mediafire.com" in clean_url:
|
| 116 |
+
async with semaphore:
|
| 117 |
cached = MEDIAFIRE_CACHE.get(clean_url)
|
| 118 |
target_link = cached['link'] if (cached and (current_time - cached['time']) < CACHE_TTL) else None
|
| 119 |
|
| 120 |
if not target_link:
|
| 121 |
target_link = await scrape_mediafire(clean_url)
|
| 122 |
if target_link:
|
|
|
|
| 123 |
MEDIAFIRE_CACHE[clean_url] = {'link': target_link, 'time': current_time}
|
| 124 |
|
| 125 |
if not target_link:
|
|
|
|
| 165 |
return await stream_file(clean_url, range_header, filename)
|
| 166 |
|
| 167 |
async def stream_file(target_url, range_header, filename, referer=None):
|
| 168 |
+
# Ensure full URL for httpx
|
| 169 |
+
if not target_url.startswith("http"):
|
| 170 |
+
raise HTTPException(status_code=500, detail=f"Malformed URL: {target_url}")
|
| 171 |
+
|
| 172 |
headers = {'User-Agent': ua.random}
|
| 173 |
if range_header: headers['Range'] = range_header
|
| 174 |
if referer: headers['Referer'] = referer
|