Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -21,7 +21,15 @@ ACCESS_KEY = os.getenv("ACCESS_KEY", "0000")
|
|
| 21 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 22 |
|
| 23 |
# User-Agent Generator ကို တစ်ခါတည်း တည်ဆောက်ထားပါ
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
def index():
|
|
@@ -57,8 +65,9 @@ def get_google_file_id(url):
|
|
| 57 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 58 |
return fid.group(1) if fid else None
|
| 59 |
|
|
|
|
| 60 |
@app.get("/download")
|
| 61 |
-
|
| 62 |
if key != ACCESS_KEY:
|
| 63 |
raise HTTPException(status_code=403, detail="Access Denied")
|
| 64 |
|
|
@@ -93,7 +102,7 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 93 |
if not target_link:
|
| 94 |
raise HTTPException(status_code=400, detail="Could not resolve direct link")
|
| 95 |
|
| 96 |
-
return
|
| 97 |
|
| 98 |
# --- Google Drive အတွက် Multiple Service Accounts Logic ---
|
| 99 |
file_id = get_google_file_id(url)
|
|
@@ -118,6 +127,7 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 118 |
return process_response(r)
|
| 119 |
|
| 120 |
print(f"Account {account.get('client_email')} failed with {r.status_code}. Trying next...")
|
|
|
|
| 121 |
|
| 122 |
# --- Public Fallback ---
|
| 123 |
public_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
|
@@ -133,14 +143,16 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 133 |
if k.startswith("download_warning"):
|
| 134 |
confirm_token = v; break
|
| 135 |
if confirm_token:
|
|
|
|
| 136 |
r = requests.get(f"{public_url}&confirm={confirm_token}", headers=headers, stream=True)
|
| 137 |
|
| 138 |
if r.status_code < 400:
|
| 139 |
return process_response(r)
|
| 140 |
|
|
|
|
| 141 |
raise HTTPException(status_code=r.status_code, detail="All service accounts and public download failed.")
|
| 142 |
|
| 143 |
-
|
| 144 |
# ဖိုင်ကို တိုက်ရိုက်ဆွဲထုတ်ရာတွင်လည်း Identity မျိုးစုံ သုံးနိုင်ရန်
|
| 145 |
headers = {
|
| 146 |
'User-Agent': ua.random,
|
|
@@ -151,6 +163,7 @@ async def stream_file(target_url, range_header):
|
|
| 151 |
|
| 152 |
r = requests.get(target_url, headers=headers, stream=True, allow_redirects=True)
|
| 153 |
if r.status_code >= 400:
|
|
|
|
| 154 |
raise HTTPException(status_code=r.status_code, detail="External link fetch failed")
|
| 155 |
return process_response(r)
|
| 156 |
|
|
@@ -164,8 +177,11 @@ def process_response(r):
|
|
| 164 |
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 165 |
|
| 166 |
def iterfile():
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
return StreamingResponse(
|
| 171 |
iterfile(),
|
|
|
|
| 21 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 22 |
|
| 23 |
# User-Agent Generator ကို တစ်ခါတည်း တည်ဆောက်ထားပါ
|
| 24 |
+
# (အကယ်၍ Fake UA database error တက်ခဲ့လျှင် Fallback အနေဖြင့် သုံးရန်)
|
| 25 |
+
try:
|
| 26 |
+
ua = UserAgent()
|
| 27 |
+
except Exception:
|
| 28 |
+
class FallbackUA:
|
| 29 |
+
@property
|
| 30 |
+
def random(self):
|
| 31 |
+
return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"
|
| 32 |
+
ua = FallbackUA()
|
| 33 |
|
| 34 |
@app.get("/")
|
| 35 |
def index():
|
|
|
|
| 65 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 66 |
return fid.group(1) if fid else None
|
| 67 |
|
| 68 |
+
# အရေးကြီးသည်: requests သည် synchronous ဖြစ်သောကြောင့် async def အစား def ကိုပြောင်းသုံးထားပါသည်
|
| 69 |
@app.get("/download")
|
| 70 |
+
def download_proxy(request: Request, url: str, key: str = None):
|
| 71 |
if key != ACCESS_KEY:
|
| 72 |
raise HTTPException(status_code=403, detail="Access Denied")
|
| 73 |
|
|
|
|
| 102 |
if not target_link:
|
| 103 |
raise HTTPException(status_code=400, detail="Could not resolve direct link")
|
| 104 |
|
| 105 |
+
return stream_file(target_link, request.headers.get('range'))
|
| 106 |
|
| 107 |
# --- Google Drive အတွက် Multiple Service Accounts Logic ---
|
| 108 |
file_id = get_google_file_id(url)
|
|
|
|
| 127 |
return process_response(r)
|
| 128 |
|
| 129 |
print(f"Account {account.get('client_email')} failed with {r.status_code}. Trying next...")
|
| 130 |
+
r.close() # အလုပ်မလုပ်သော connection ကိုပိတ်ပစ်ပါ
|
| 131 |
|
| 132 |
# --- Public Fallback ---
|
| 133 |
public_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
|
|
|
| 143 |
if k.startswith("download_warning"):
|
| 144 |
confirm_token = v; break
|
| 145 |
if confirm_token:
|
| 146 |
+
r.close() # ယခင် request ကိုပိတ်ပါ
|
| 147 |
r = requests.get(f"{public_url}&confirm={confirm_token}", headers=headers, stream=True)
|
| 148 |
|
| 149 |
if r.status_code < 400:
|
| 150 |
return process_response(r)
|
| 151 |
|
| 152 |
+
r.close()
|
| 153 |
raise HTTPException(status_code=r.status_code, detail="All service accounts and public download failed.")
|
| 154 |
|
| 155 |
+
def stream_file(target_url, range_header):
|
| 156 |
# ဖိုင်ကို တိုက်ရိုက်ဆွဲထုတ်ရာတွင်လည်း Identity မျိုးစုံ သုံးနိုင်ရန်
|
| 157 |
headers = {
|
| 158 |
'User-Agent': ua.random,
|
|
|
|
| 163 |
|
| 164 |
r = requests.get(target_url, headers=headers, stream=True, allow_redirects=True)
|
| 165 |
if r.status_code >= 400:
|
| 166 |
+
r.close()
|
| 167 |
raise HTTPException(status_code=r.status_code, detail="External link fetch failed")
|
| 168 |
return process_response(r)
|
| 169 |
|
|
|
|
| 177 |
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 178 |
|
| 179 |
def iterfile():
|
| 180 |
+
try:
|
| 181 |
+
for chunk in r.iter_content(chunk_size=1024*1024):
|
| 182 |
+
if chunk: yield chunk
|
| 183 |
+
finally:
|
| 184 |
+
r.close() # Stream ပြီးဆုံးသွားချိန် (သို့) client ဘက်မှ cancel လုပ်သွားချိန် connection ပိတ်ရန်
|
| 185 |
|
| 186 |
return StreamingResponse(
|
| 187 |
iterfile(),
|