Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import json
|
|
|
|
| 4 |
import httpx
|
| 5 |
import random
|
| 6 |
import asyncio
|
|
@@ -23,6 +24,10 @@ ACCESS_KEY = os.getenv("ACCESS_KEY", "0000")
|
|
| 23 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 24 |
ua = UserAgent(fallback='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
client = httpx.AsyncClient(
|
| 27 |
timeout=httpx.Timeout(60.0, read=None),
|
| 28 |
follow_redirects=True,
|
|
@@ -55,7 +60,6 @@ def get_google_file_id(url):
|
|
| 55 |
return fid.group(1) if fid else None
|
| 56 |
|
| 57 |
def get_clean_filename(url):
|
| 58 |
-
"""URL ထဲကနေ ဖိုင်နာမည်ကို သန့်စင်စွာထုတ်ယူခြင်း"""
|
| 59 |
decoded_url = urllib.parse.unquote(url)
|
| 60 |
name = decoded_url.split('/')[-1].split('?')[0]
|
| 61 |
if not name or '.' not in name:
|
|
@@ -71,13 +75,30 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 71 |
filename = get_clean_filename(clean_url)
|
| 72 |
range_header = request.headers.get('range')
|
| 73 |
|
| 74 |
-
# 1. သတ်မှတ်ထားသော User-Agent ကို Request တစ်ခုလုံးအတွက် အသုံးပြုရန်
|
| 75 |
req_ua = ua.random
|
|
|
|
| 76 |
|
| 77 |
# --- MediaFire Section ---
|
| 78 |
if "mediafire.com" in clean_url:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
try:
|
| 80 |
-
# Browser အသွင်ဖြင့် စာမျက်နှာကိုအရင်ဖတ်ခြင်း (Cookies များကို သိမ်းဆည်းရန် temp_client ကိုသုံးမည်)
|
| 81 |
async with httpx.AsyncClient(headers={'User-Agent': req_ua}, follow_redirects=True) as temp_client:
|
| 82 |
page_res = await temp_client.get(clean_url)
|
| 83 |
if page_res.status_code != 200:
|
|
@@ -85,8 +106,6 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 85 |
|
| 86 |
html_content = page_res.text
|
| 87 |
target_link = None
|
| 88 |
-
|
| 89 |
-
# 2. MediaFire မှပေးသော Session Cookies ကို ယူထားခြင်း
|
| 90 |
cookies = temp_client.cookies
|
| 91 |
|
| 92 |
match = re.search(r"https?://download[0-9]+\.mediafire\.com/[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+/[^\s'\"]+", html_content)
|
|
@@ -103,14 +122,21 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 103 |
if target_link.startswith("//"): target_link = f"https:{target_link}"
|
| 104 |
elif target_link.startswith("/"): target_link = f"https://www.mediafire.com{target_link}"
|
| 105 |
|
| 106 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return await stream_file(target_link, range_header, filename, referer=clean_url, req_ua=req_ua, cookies=cookies)
|
| 108 |
else:
|
| 109 |
raise HTTPException(status_code=404, detail="Could not extract direct download link")
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
print(f"MediaFire Logic Error: {str(e)}")
|
| 113 |
-
raise HTTPException(status_code=500, detail=
|
| 114 |
|
| 115 |
# --- Google Drive Section ---
|
| 116 |
elif "drive.google.com" in clean_url:
|
|
@@ -141,17 +167,14 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 141 |
return await stream_file(clean_url, range_header, filename, req_ua=req_ua)
|
| 142 |
|
| 143 |
async def stream_file(target_url, range_header, filename, referer=None, req_ua=None, cookies=None):
|
| 144 |
-
# တူညီသော User-Agent ကို ပြန်သုံးခြင်း
|
| 145 |
headers = {'User-Agent': req_ua if req_ua else ua.random}
|
| 146 |
if range_header: headers['Range'] = range_header
|
| 147 |
if referer: headers['Referer'] = referer
|
| 148 |
|
| 149 |
try:
|
| 150 |
-
# Cookies ပါထည့်၍ Request လုပ်ခြင်း
|
| 151 |
req = client.build_request("GET", target_url, headers=headers, cookies=cookies)
|
| 152 |
r = await client.send(req, stream=True)
|
| 153 |
|
| 154 |
-
# HTML ပြန်လာရင် ဗီဒီယိုမဟုတ်ကြောင်း သေချာစေရန်
|
| 155 |
ctype = r.headers.get('content-type', '').lower()
|
| 156 |
if 'text/html' in ctype:
|
| 157 |
await r.aclose()
|
|
|
|
| 1 |
import os
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
+
import time
|
| 5 |
import httpx
|
| 6 |
import random
|
| 7 |
import asyncio
|
|
|
|
| 24 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 25 |
ua = UserAgent(fallback='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36')
|
| 26 |
|
| 27 |
+
# MediaFire အတွက် Cache Memory
|
| 28 |
+
MEDIAFIRE_CACHE = {}
|
| 29 |
+
CACHE_TTL = 1800 # Cache သက်တမ်း စက္ကန့် 1800 (မိနစ် 30)
|
| 30 |
+
|
| 31 |
client = httpx.AsyncClient(
|
| 32 |
timeout=httpx.Timeout(60.0, read=None),
|
| 33 |
follow_redirects=True,
|
|
|
|
| 60 |
return fid.group(1) if fid else None
|
| 61 |
|
| 62 |
def get_clean_filename(url):
|
|
|
|
| 63 |
decoded_url = urllib.parse.unquote(url)
|
| 64 |
name = decoded_url.split('/')[-1].split('?')[0]
|
| 65 |
if not name or '.' not in name:
|
|
|
|
| 75 |
filename = get_clean_filename(clean_url)
|
| 76 |
range_header = request.headers.get('range')
|
| 77 |
|
|
|
|
| 78 |
req_ua = ua.random
|
| 79 |
+
current_time = time.time()
|
| 80 |
|
| 81 |
# --- MediaFire Section ---
|
| 82 |
if "mediafire.com" in clean_url:
|
| 83 |
+
|
| 84 |
+
# 1. Cache ထဲမှာရှိမရှိ အရင်စစ်ဆေးခြင်း (Player များ Seek လုပ်ရာတွင် လွယ်ကူစေရန်)
|
| 85 |
+
cached_data = MEDIAFIRE_CACHE.get(clean_url)
|
| 86 |
+
if cached_data and (current_time - cached_data['time']) < CACHE_TTL:
|
| 87 |
+
try:
|
| 88 |
+
return await stream_file(
|
| 89 |
+
cached_data['link'], range_header, filename,
|
| 90 |
+
referer=clean_url, req_ua=cached_data['ua'], cookies=cached_data['cookies']
|
| 91 |
+
)
|
| 92 |
+
except HTTPException as e:
|
| 93 |
+
# အကယ်၍ Cached Link သည် သက်တမ်းကုန်သွား၍ 415 ပြန်လာပါက Cache ကိုဖျက်ပြီး အသစ်ပြန်ရှာမည်
|
| 94 |
+
if e.status_code == 415:
|
| 95 |
+
print(f"Cache expired for {clean_url}, refetching...")
|
| 96 |
+
del MEDIAFIRE_CACHE[clean_url]
|
| 97 |
+
else:
|
| 98 |
+
raise
|
| 99 |
+
|
| 100 |
+
# 2. Cache မရှိပါက သို့မဟုတ် Expire ဖြစ်သွားပါက အသစ်ပြန်ရှာခြင်း
|
| 101 |
try:
|
|
|
|
| 102 |
async with httpx.AsyncClient(headers={'User-Agent': req_ua}, follow_redirects=True) as temp_client:
|
| 103 |
page_res = await temp_client.get(clean_url)
|
| 104 |
if page_res.status_code != 200:
|
|
|
|
| 106 |
|
| 107 |
html_content = page_res.text
|
| 108 |
target_link = None
|
|
|
|
|
|
|
| 109 |
cookies = temp_client.cookies
|
| 110 |
|
| 111 |
match = re.search(r"https?://download[0-9]+\.mediafire\.com/[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+/[^\s'\"]+", html_content)
|
|
|
|
| 122 |
if target_link.startswith("//"): target_link = f"https:{target_link}"
|
| 123 |
elif target_link.startswith("/"): target_link = f"https://www.mediafire.com{target_link}"
|
| 124 |
|
| 125 |
+
# နောက်ထပ် Range Request များအတွက် Memory ထဲတွင် မှတ်သားထားခြင်း
|
| 126 |
+
MEDIAFIRE_CACHE[clean_url] = {
|
| 127 |
+
'link': target_link,
|
| 128 |
+
'ua': req_ua,
|
| 129 |
+
'cookies': dict(cookies),
|
| 130 |
+
'time': current_time
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
return await stream_file(target_link, range_header, filename, referer=clean_url, req_ua=req_ua, cookies=cookies)
|
| 134 |
else:
|
| 135 |
raise HTTPException(status_code=404, detail="Could not extract direct download link")
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
print(f"MediaFire Logic Error: {str(e)}")
|
| 139 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 140 |
|
| 141 |
# --- Google Drive Section ---
|
| 142 |
elif "drive.google.com" in clean_url:
|
|
|
|
| 167 |
return await stream_file(clean_url, range_header, filename, req_ua=req_ua)
|
| 168 |
|
| 169 |
async def stream_file(target_url, range_header, filename, referer=None, req_ua=None, cookies=None):
|
|
|
|
| 170 |
headers = {'User-Agent': req_ua if req_ua else ua.random}
|
| 171 |
if range_header: headers['Range'] = range_header
|
| 172 |
if referer: headers['Referer'] = referer
|
| 173 |
|
| 174 |
try:
|
|
|
|
| 175 |
req = client.build_request("GET", target_url, headers=headers, cookies=cookies)
|
| 176 |
r = await client.send(req, stream=True)
|
| 177 |
|
|
|
|
| 178 |
ctype = r.headers.get('content-type', '').lower()
|
| 179 |
if 'text/html' in ctype:
|
| 180 |
await r.aclose()
|