Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,6 +7,7 @@ import random
|
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
from fastapi import FastAPI, HTTPException, Request
|
| 9 |
from fastapi.responses import StreamingResponse
|
|
|
|
| 10 |
import uvicorn
|
| 11 |
|
| 12 |
# Google Auth Libraries
|
|
@@ -19,6 +20,9 @@ app = FastAPI()
|
|
| 19 |
ACCESS_KEY = os.getenv("ACCESS_KEY", "0000")
|
| 20 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
@app.get("/")
|
| 23 |
def index():
|
| 24 |
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
|
@@ -31,7 +35,7 @@ def get_all_service_accounts():
|
|
| 31 |
data = json.loads(SERVICE_ACCOUNT_JSON_STR)
|
| 32 |
if isinstance(data, list):
|
| 33 |
return data
|
| 34 |
-
return [data]
|
| 35 |
except Exception as e:
|
| 36 |
print(f"JSON Parsing Error: {e}")
|
| 37 |
return []
|
|
@@ -61,15 +65,27 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 61 |
# --- Google Drive မဟုတ်သော Link များအတွက် (MediaFire, Dropbox, etc.) ---
|
| 62 |
if "drive.google.com" not in url:
|
| 63 |
target_link = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
if "mediafire.com" in url:
|
| 65 |
try:
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 68 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 69 |
target_link = btn.get('href') if btn else None
|
| 70 |
-
except
|
|
|
|
|
|
|
| 71 |
elif "dropbox.com" in url:
|
| 72 |
target_link = url.replace("?dl=0", "").split("?")[0] + "?dl=1"
|
|
|
|
| 73 |
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 74 |
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 75 |
target_link = f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content"
|
|
@@ -85,9 +101,8 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 85 |
raise HTTPException(status_code=400, detail="Invalid Google Drive Link")
|
| 86 |
|
| 87 |
accounts = get_all_service_accounts()
|
| 88 |
-
random.shuffle(accounts)
|
| 89 |
|
| 90 |
-
# အကောင့်တစ်ခုချင်းစီဖြင့် ဒေါင်းရန် ကြိုးစားကြည့်မည်
|
| 91 |
for account in accounts:
|
| 92 |
token = get_token_for_account(account)
|
| 93 |
if not token: continue
|
|
@@ -99,22 +114,19 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 99 |
|
| 100 |
r = requests.get(api_link, headers=headers, stream=True, allow_redirects=True)
|
| 101 |
|
| 102 |
-
# အကယ်၍ အောင်မြင်ပါက (200 သို့မဟုတ် 206 Partial Content) ပြန်ပို့မည်
|
| 103 |
if r.status_code in [200, 206]:
|
| 104 |
return process_response(r)
|
| 105 |
|
| 106 |
-
# 403 သို့မဟုတ် 404 ဖြစ်ပါက နောက်အကောင့်တစ်ခုဖြင့် ထပ်စမ်းမည်
|
| 107 |
print(f"Account {account.get('client_email')} failed with {r.status_code}. Trying next...")
|
| 108 |
|
| 109 |
-
# ---
|
| 110 |
public_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 111 |
-
headers = {}
|
| 112 |
if request.headers.get('range'):
|
| 113 |
headers['Range'] = request.headers.get('range')
|
| 114 |
|
| 115 |
r = requests.get(public_url, headers=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 r.cookies.items():
|
|
@@ -129,7 +141,14 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 129 |
raise HTTPException(status_code=r.status_code, detail="All service accounts and public download failed.")
|
| 130 |
|
| 131 |
async def stream_file(target_url, range_header):
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
r = requests.get(target_url, headers=headers, stream=True, allow_redirects=True)
|
| 134 |
if r.status_code >= 400:
|
| 135 |
raise HTTPException(status_code=r.status_code, detail="External link fetch failed")
|
|
|
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
from fastapi import FastAPI, HTTPException, Request
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
+
from fake_useragent import UserAgent
|
| 11 |
import uvicorn
|
| 12 |
|
| 13 |
# Google Auth Libraries
|
|
|
|
| 20 |
ACCESS_KEY = os.getenv("ACCESS_KEY", "0000")
|
| 21 |
SERVICE_ACCOUNT_JSON_STR = os.getenv("GOOGLE_SERVICE_ACCOUNT_JSON")
|
| 22 |
|
| 23 |
+
# User-Agent Generator ကို တစ်ခါတည်း တည်ဆောက်ထားပါ
|
| 24 |
+
ua = UserAgent()
|
| 25 |
+
|
| 26 |
@app.get("/")
|
| 27 |
def index():
|
| 28 |
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
|
|
|
| 35 |
data = json.loads(SERVICE_ACCOUNT_JSON_STR)
|
| 36 |
if isinstance(data, list):
|
| 37 |
return data
|
| 38 |
+
return [data]
|
| 39 |
except Exception as e:
|
| 40 |
print(f"JSON Parsing Error: {e}")
|
| 41 |
return []
|
|
|
|
| 65 |
# --- Google Drive မဟုတ်သော Link များအတွက် (MediaFire, Dropbox, etc.) ---
|
| 66 |
if "drive.google.com" not in url:
|
| 67 |
target_link = None
|
| 68 |
+
|
| 69 |
+
# Headers အတွက် Random User-Agent ထုတ်ပေးခြင်း
|
| 70 |
+
current_headers = {
|
| 71 |
+
'User-Agent': ua.random,
|
| 72 |
+
'Accept-Language': 'en-US,en;q=0.9',
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
if "mediafire.com" in url:
|
| 76 |
try:
|
| 77 |
+
# MediaFire အတွက် Referer ထည့်ပေးခြင်းက ပိုစိတ်ချရသည်
|
| 78 |
+
current_headers['Referer'] = 'https://www.mediafire.com/'
|
| 79 |
+
r = requests.get(url, headers=current_headers, timeout=10)
|
| 80 |
soup = BeautifulSoup(r.text, 'html.parser')
|
| 81 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 82 |
target_link = btn.get('href') if btn else None
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"MediaFire Error: {e}")
|
| 85 |
+
|
| 86 |
elif "dropbox.com" in url:
|
| 87 |
target_link = url.replace("?dl=0", "").split("?")[0] + "?dl=1"
|
| 88 |
+
|
| 89 |
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 90 |
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 91 |
target_link = f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content"
|
|
|
|
| 101 |
raise HTTPException(status_code=400, detail="Invalid Google Drive Link")
|
| 102 |
|
| 103 |
accounts = get_all_service_accounts()
|
| 104 |
+
random.shuffle(accounts)
|
| 105 |
|
|
|
|
| 106 |
for account in accounts:
|
| 107 |
token = get_token_for_account(account)
|
| 108 |
if not token: continue
|
|
|
|
| 114 |
|
| 115 |
r = requests.get(api_link, headers=headers, stream=True, allow_redirects=True)
|
| 116 |
|
|
|
|
| 117 |
if r.status_code in [200, 206]:
|
| 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}"
|
| 124 |
+
headers = {'User-Agent': ua.random} # Public link မှာလည်း UA သုံးခြင်း
|
| 125 |
if request.headers.get('range'):
|
| 126 |
headers['Range'] = request.headers.get('range')
|
| 127 |
|
| 128 |
r = requests.get(public_url, headers=headers, stream=True, allow_redirects=True)
|
| 129 |
|
|
|
|
| 130 |
if "text/html" in r.headers.get('Content-Type', '').lower():
|
| 131 |
confirm_token = None
|
| 132 |
for k, v in r.cookies.items():
|
|
|
|
| 141 |
raise HTTPException(status_code=r.status_code, detail="All service accounts and public download failed.")
|
| 142 |
|
| 143 |
async def stream_file(target_url, range_header):
|
| 144 |
+
# ဖိုင်ကို တိုက်ရိုက်ဆွဲထုတ်ရာတွင်လည်း Identity မျိုးစုံ သုံးနိုင်ရန်
|
| 145 |
+
headers = {
|
| 146 |
+
'User-Agent': ua.random,
|
| 147 |
+
'Referer': 'https://www.mediafire.com/' if "mediafire" in target_url else ''
|
| 148 |
+
}
|
| 149 |
+
if range_header:
|
| 150 |
+
headers['Range'] = 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")
|