Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ import re
|
|
| 3 |
import json
|
| 4 |
import base64
|
| 5 |
import requests
|
|
|
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
from fastapi import FastAPI, HTTPException, Request
|
| 8 |
from fastapi.responses import StreamingResponse
|
|
@@ -16,6 +17,7 @@ app = FastAPI()
|
|
| 16 |
|
| 17 |
# --- Configurations ---
|
| 18 |
ACCESS_KEY = os.getenv("ACCESS_KEY", "786969")
|
|
|
|
| 19 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 20 |
|
| 21 |
@app.get("/")
|
|
@@ -23,14 +25,31 @@ def index():
|
|
| 23 |
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
| 24 |
|
| 25 |
def get_google_access_token():
|
|
|
|
| 26 |
if not SERVICE_ACCOUNT_JSON_STR:
|
|
|
|
| 27 |
return None
|
|
|
|
| 28 |
try:
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
scopes = ['https://www.googleapis.com/auth/drive.readonly']
|
| 31 |
-
creds = service_account.Credentials.from_service_account_info(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
auth_req = google.auth.transport.requests.Request()
|
| 33 |
creds.refresh(auth_req)
|
|
|
|
| 34 |
return creds.token
|
| 35 |
except Exception as e:
|
| 36 |
print(f"Service Account Auth Error: {e}")
|
|
@@ -45,7 +64,7 @@ def get_direct_url(url):
|
|
| 45 |
file_id = fid.group(1)
|
| 46 |
|
| 47 |
access_token = get_google_access_token()
|
| 48 |
-
#
|
| 49 |
target = f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media"
|
| 50 |
return target, access_token, file_id
|
| 51 |
|
|
@@ -85,36 +104,29 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 85 |
|
| 86 |
session = requests.Session()
|
| 87 |
|
| 88 |
-
# ၁။ Service Account ဖြင့်
|
| 89 |
r = session.get(target_link, headers=headers, stream=True, allow_redirects=True)
|
| 90 |
|
| 91 |
-
# ၂။
|
| 92 |
if r.status_code == 404 and "drive.google.com" in url and file_id:
|
| 93 |
-
print(f"SA 404. Trying Public Fallback for: {file_id}")
|
| 94 |
-
|
| 95 |
-
# Public Link သုံးမည်ဖြစ်၍ Authorization ကို ဖယ်ထုတ်ရပါမည်
|
| 96 |
public_headers = {k: v for k, v in headers.items() if k.lower() != 'authorization'}
|
| 97 |
public_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 98 |
|
| 99 |
-
# Public Link ကို စမ်းခေါ်ခြင်း
|
| 100 |
r = session.get(public_url, headers=public_headers, stream=True, allow_redirects=True)
|
| 101 |
|
| 102 |
-
#
|
| 103 |
if "text/html" in r.headers.get('Content-Type', '').lower():
|
| 104 |
confirm_token = None
|
| 105 |
for k, v in session.cookies.items():
|
| 106 |
if k.startswith("download_warning"):
|
| 107 |
confirm_token = v
|
| 108 |
break
|
| 109 |
-
|
| 110 |
if confirm_token:
|
| 111 |
r = session.get(f"{public_url}&confirm={confirm_token}", headers=public_headers, stream=True)
|
| 112 |
|
| 113 |
-
# Error ဆက်ရှိနေသေးပါက
|
| 114 |
if r.status_code >= 400:
|
| 115 |
-
raise HTTPException(status_code=r.status_code, detail=f"
|
| 116 |
|
| 117 |
-
# Header ပြန်ပို့ရန် ပြင်ဆင်ခြင်း
|
| 118 |
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 119 |
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
| 120 |
response_headers['Accept-Ranges'] = 'bytes'
|
|
|
|
| 3 |
import json
|
| 4 |
import base64
|
| 5 |
import requests
|
| 6 |
+
import random # အကောင့်တစ်ခုထက်ပိုပါက တစ်လှည့်စီရွေးရန်
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
from fastapi import FastAPI, HTTPException, Request
|
| 9 |
from fastapi.responses import StreamingResponse
|
|
|
|
| 17 |
|
| 18 |
# --- Configurations ---
|
| 19 |
ACCESS_KEY = os.getenv("ACCESS_KEY", "786969")
|
| 20 |
+
# ဤနေရာတွင် JSON တစ်ခုတည်းဖြစ်စေ၊ [{}, {}] ပုံစံဖြင့် Array ဖြစ်စေ ထည့်နိုင်သည်
|
| 21 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 22 |
|
| 23 |
@app.get("/")
|
|
|
|
| 25 |
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
| 26 |
|
| 27 |
def get_google_access_token():
|
| 28 |
+
"""Service Account(s) မှ Access Token ကို Random ရယူခြင်း"""
|
| 29 |
if not SERVICE_ACCOUNT_JSON_STR:
|
| 30 |
+
print("Missing GOOGLE_SERVICE_ACCOUNT_JSON credential!")
|
| 31 |
return None
|
| 32 |
+
|
| 33 |
try:
|
| 34 |
+
# Secret ထဲမှ Data ကို ဖတ်ခြင်း
|
| 35 |
+
creds_data = json.loads(SERVICE_ACCOUNT_JSON_STR)
|
| 36 |
+
|
| 37 |
+
# အကယ်၍ JSON Array (List) ဖြစ်နေပါက တစ်ခုကို Random ရွေးမည်
|
| 38 |
+
if isinstance(creds_data, list):
|
| 39 |
+
selected_cred = random.choice(creds_data)
|
| 40 |
+
else:
|
| 41 |
+
selected_cred = creds_data
|
| 42 |
+
|
| 43 |
scopes = ['https://www.googleapis.com/auth/drive.readonly']
|
| 44 |
+
creds = service_account.Credentials.from_service_account_info(
|
| 45 |
+
selected_cred,
|
| 46 |
+
scopes=scopes
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Token ရယူခြင်း
|
| 50 |
auth_req = google.auth.transport.requests.Request()
|
| 51 |
creds.refresh(auth_req)
|
| 52 |
+
|
| 53 |
return creds.token
|
| 54 |
except Exception as e:
|
| 55 |
print(f"Service Account Auth Error: {e}")
|
|
|
|
| 64 |
file_id = fid.group(1)
|
| 65 |
|
| 66 |
access_token = get_google_access_token()
|
| 67 |
+
# API v3 Link
|
| 68 |
target = f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media"
|
| 69 |
return target, access_token, file_id
|
| 70 |
|
|
|
|
| 104 |
|
| 105 |
session = requests.Session()
|
| 106 |
|
| 107 |
+
# ၁။ Service Account ဖြင့် ကြိုးစားခြင်း
|
| 108 |
r = session.get(target_link, headers=headers, stream=True, allow_redirects=True)
|
| 109 |
|
| 110 |
+
# ၂။ 404 ဖြစ်ပါက Public Fallback လုပ်ခြင်း
|
| 111 |
if r.status_code == 404 and "drive.google.com" in url and file_id:
|
|
|
|
|
|
|
|
|
|
| 112 |
public_headers = {k: v for k, v in headers.items() if k.lower() != 'authorization'}
|
| 113 |
public_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 114 |
|
|
|
|
| 115 |
r = session.get(public_url, headers=public_headers, stream=True, allow_redirects=True)
|
| 116 |
|
| 117 |
+
# Virus Warning ကျော်ခြင်း
|
| 118 |
if "text/html" in r.headers.get('Content-Type', '').lower():
|
| 119 |
confirm_token = None
|
| 120 |
for k, v in session.cookies.items():
|
| 121 |
if k.startswith("download_warning"):
|
| 122 |
confirm_token = v
|
| 123 |
break
|
|
|
|
| 124 |
if confirm_token:
|
| 125 |
r = session.get(f"{public_url}&confirm={confirm_token}", headers=public_headers, stream=True)
|
| 126 |
|
|
|
|
| 127 |
if r.status_code >= 400:
|
| 128 |
+
raise HTTPException(status_code=r.status_code, detail=f"Request failed with status {r.status_code}")
|
| 129 |
|
|
|
|
| 130 |
excluded = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 131 |
response_headers = {n: v for n, v in r.headers.items() if n.lower() not in excluded}
|
| 132 |
response_headers['Accept-Ranges'] = 'bytes'
|