Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -25,7 +25,7 @@ ua = UserAgent(fallback='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/5
|
|
| 25 |
|
| 26 |
# Global Async Client
|
| 27 |
client = httpx.AsyncClient(
|
| 28 |
-
timeout=httpx.Timeout(30.0, read=None),
|
| 29 |
follow_redirects=True,
|
| 30 |
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20)
|
| 31 |
)
|
|
@@ -45,7 +45,6 @@ async def get_token_for_account(cred_dict):
|
|
| 45 |
try:
|
| 46 |
scopes = ['https://www.googleapis.com/auth/drive.readonly']
|
| 47 |
creds = service_account.Credentials.from_service_account_info(cred_dict, scopes=scopes)
|
| 48 |
-
# Token refresh ကို thread ထဲမှာ run ပါတယ်
|
| 49 |
loop = asyncio.get_event_loop()
|
| 50 |
auth_req = google.auth.transport.requests.Request()
|
| 51 |
await loop.run_in_executor(None, creds.refresh, auth_req)
|
|
@@ -76,14 +75,16 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 76 |
headers['Referer'] = 'https://www.mediafire.com/'
|
| 77 |
r = await client.get(url, headers=headers)
|
| 78 |
|
| 79 |
-
# Regex ဖြင့် Direct Link ကို ရှာဖွေခြင်း
|
| 80 |
match = re.search(r'href="(https?://[a-zA-Z0-9-]+\.mediafire\.com/download/[^"]+)"', r.text)
|
| 81 |
if match:
|
| 82 |
target_link = match.group(1)
|
| 83 |
else:
|
| 84 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 85 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
print(f"MediaFire Error: {e}")
|
| 89 |
|
|
@@ -140,24 +141,20 @@ async def stream_file(target_url, range_header):
|
|
| 140 |
raise HTTPException(status_code=500, detail=str(e))
|
| 141 |
|
| 142 |
async def process_response(r):
|
| 143 |
-
# မူလလာတဲ့ content-disposition ကို ဖယ်ထုတ်ဖို့ excluded ထဲ ထည့်ပါမယ်
|
| 144 |
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection', 'content-disposition']
|
| 145 |
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
| 146 |
|
| 147 |
-
# Video Seeking အတွက် မရှိမဖြစ်လိုအပ်သော Headers များ
|
| 148 |
response_headers['Accept-Ranges'] = 'bytes'
|
| 149 |
if 'Content-Length' in r.headers:
|
| 150 |
response_headers['Content-Length'] = r.headers['Content-Length']
|
| 151 |
if 'Content-Range' in r.headers:
|
| 152 |
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 153 |
|
| 154 |
-
# Direct Download ဖြစ်စေရန်
|
| 155 |
response_headers['Content-Disposition'] = 'attachment'
|
| 156 |
|
| 157 |
-
# Connection များကို Memory Leak မဖြစ်စေဘဲ သေချာပိတ်ပေးမည့် Generator
|
| 158 |
async def stream_generator():
|
| 159 |
try:
|
| 160 |
-
async for chunk in r.aiter_bytes(chunk_size=131072):
|
| 161 |
yield chunk
|
| 162 |
finally:
|
| 163 |
await r.aclose()
|
|
|
|
| 25 |
|
| 26 |
# Global Async Client
|
| 27 |
client = httpx.AsyncClient(
|
| 28 |
+
timeout=httpx.Timeout(30.0, read=None),
|
| 29 |
follow_redirects=True,
|
| 30 |
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20)
|
| 31 |
)
|
|
|
|
| 45 |
try:
|
| 46 |
scopes = ['https://www.googleapis.com/auth/drive.readonly']
|
| 47 |
creds = service_account.Credentials.from_service_account_info(cred_dict, scopes=scopes)
|
|
|
|
| 48 |
loop = asyncio.get_event_loop()
|
| 49 |
auth_req = google.auth.transport.requests.Request()
|
| 50 |
await loop.run_in_executor(None, creds.refresh, auth_req)
|
|
|
|
| 75 |
headers['Referer'] = 'https://www.mediafire.com/'
|
| 76 |
r = await client.get(url, headers=headers)
|
| 77 |
|
| 78 |
+
# Regex ဖြင့် Direct Link ကို ရှာဖွေခြင်း
|
| 79 |
match = re.search(r'href="(https?://[a-zA-Z0-9-]+\.mediafire\.com/download/[^"]+)"', r.text)
|
| 80 |
if match:
|
| 81 |
target_link = match.group(1)
|
| 82 |
else:
|
| 83 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 84 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 85 |
+
if btn and btn.get('href'):
|
| 86 |
+
# ပေးလာတဲ့ href မှာ https:// မပါရင် မူလ URL နဲ့ ပေါင်းပြီး အပြည့်အစုံဖြစ်အောင် ပြုလုပ်ခြင်း
|
| 87 |
+
target_link = urllib.parse.urljoin(url, btn.get('href'))
|
| 88 |
except Exception as e:
|
| 89 |
print(f"MediaFire Error: {e}")
|
| 90 |
|
|
|
|
| 141 |
raise HTTPException(status_code=500, detail=str(e))
|
| 142 |
|
| 143 |
async def process_response(r):
|
|
|
|
| 144 |
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection', 'content-disposition']
|
| 145 |
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
| 146 |
|
|
|
|
| 147 |
response_headers['Accept-Ranges'] = 'bytes'
|
| 148 |
if 'Content-Length' in r.headers:
|
| 149 |
response_headers['Content-Length'] = r.headers['Content-Length']
|
| 150 |
if 'Content-Range' in r.headers:
|
| 151 |
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 152 |
|
|
|
|
| 153 |
response_headers['Content-Disposition'] = 'attachment'
|
| 154 |
|
|
|
|
| 155 |
async def stream_generator():
|
| 156 |
try:
|
| 157 |
+
async for chunk in r.aiter_bytes(chunk_size=131072):
|
| 158 |
yield chunk
|
| 159 |
finally:
|
| 160 |
await r.aclose()
|