Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -4,9 +4,18 @@ import requests
|
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import re
|
| 6 |
import base64
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
def index():
|
|
@@ -15,7 +24,16 @@ def index():
|
|
| 15 |
def get_direct_url(url):
|
| 16 |
if "drive.google.com" in url:
|
| 17 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
elif "mediafire.com" in url:
|
| 20 |
try:
|
| 21 |
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
|
@@ -23,11 +41,14 @@ def get_direct_url(url):
|
|
| 23 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 24 |
return btn.get('href') if btn else None
|
| 25 |
except: return None
|
|
|
|
| 26 |
elif "dropbox.com" in url:
|
| 27 |
return url.replace("?dl=0", "").split("?")[0] + "?dl=1"
|
|
|
|
| 28 |
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 29 |
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 30 |
return f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content"
|
|
|
|
| 31 |
return None
|
| 32 |
|
| 33 |
@app.get("/download")
|
|
@@ -45,17 +66,42 @@ async def download_proxy(request: Request, url: str, key: str = None):
|
|
| 45 |
if range_header:
|
| 46 |
headers['Range'] = range_header
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# Header များကို ပြန်လည်ပေးပို့ရန် ပြင်ဆင်ခြင်း
|
| 52 |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 53 |
response_headers = {name: value for name, value in r.headers.items() if name.lower() not in excluded_headers}
|
| 54 |
|
| 55 |
-
# Video ကျော်ကြည့်နိုင်ရန်
|
| 56 |
response_headers['Accept-Ranges'] = 'bytes'
|
|
|
|
| 57 |
if 'Content-Length' in r.headers:
|
| 58 |
response_headers['Content-Length'] = r.headers['Content-Length']
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def iterfile():
|
| 61 |
for chunk in r.iter_content(chunk_size=1024*1024):
|
|
|
|
| 4 |
from bs4 import BeautifulSoup
|
| 5 |
import re
|
| 6 |
import base64
|
| 7 |
+
import os
|
| 8 |
+
from fastapi import FastAPI, HTTPException, Query, Request
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
@app.get("/")
|
| 13 |
+
def index():
|
| 14 |
+
return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
|
| 15 |
+
|
| 16 |
+
# --- Configurations ---
|
| 17 |
+
ACCESS_KEY = os.getenv("ACCESS_KEY", "969786") # တကယ်လို့ Secret မရှိရင် default သုံးမယ်
|
| 18 |
+
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY", "")
|
| 19 |
|
| 20 |
@app.get("/")
|
| 21 |
def index():
|
|
|
|
| 24 |
def get_direct_url(url):
|
| 25 |
if "drive.google.com" in url:
|
| 26 |
fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
|
| 27 |
+
if fid:
|
| 28 |
+
file_id = fid.group(1)
|
| 29 |
+
# API Key ထည့်သွင်းထားပါက API ကို အသုံးပြုမည် (Virus warning ကို အလိုလိုကျော်ဖြတ်ပြီးသားဖြစ်မည်)
|
| 30 |
+
if GOOGLE_API_KEY and GOOGLE_API_KEY != "YOUR_GOOGLE_API_KEY_HERE":
|
| 31 |
+
return f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media&key={GOOGLE_API_KEY}"
|
| 32 |
+
else:
|
| 33 |
+
# API Key မရှိပါက ရိုးရိုး Direct Link ကို အသုံးပြုမည်
|
| 34 |
+
return f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
elif "mediafire.com" in url:
|
| 38 |
try:
|
| 39 |
r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}, timeout=10)
|
|
|
|
| 41 |
btn = soup.find('a', {'id': 'downloadButton'})
|
| 42 |
return btn.get('href') if btn else None
|
| 43 |
except: return None
|
| 44 |
+
|
| 45 |
elif "dropbox.com" in url:
|
| 46 |
return url.replace("?dl=0", "").split("?")[0] + "?dl=1"
|
| 47 |
+
|
| 48 |
elif "1drv.ms" in url or "onedrive.live.com" in url:
|
| 49 |
encoded_url = base64.b64encode(bytes(url, 'utf-8')).decode('utf-8').replace('=', '').replace('/', '_').replace('+', '-')
|
| 50 |
return f"https://api.onedrive.com/v1.0/shares/u!{encoded_url}/root/content"
|
| 51 |
+
|
| 52 |
return None
|
| 53 |
|
| 54 |
@app.get("/download")
|
|
|
|
| 66 |
if range_header:
|
| 67 |
headers['Range'] = range_header
|
| 68 |
|
| 69 |
+
# Session အသုံးပြုခြင်းဖြင့် GDrive ၏ Cookie များကို သိမ်းဆည်းနိုင်သည်
|
| 70 |
+
session = requests.Session()
|
| 71 |
+
|
| 72 |
+
# Target Server ဆီကို Range Header နဲ့ လှမ်းတောင်းခြင်း
|
| 73 |
+
r = session.get(target_link, headers=headers, stream=True, allow_redirects=True)
|
| 74 |
|
| 75 |
+
# ---------------------------------------------------------
|
| 76 |
+
# GDrive 100MB အထက် ဖိုင်များအတွက် Virus Scan Warning ကို ကျော်ဖြတ်ခြင်း
|
| 77 |
+
# (API Key မသုံးထားသော ရိုးရိုး Link များအတွက် အထူးလိုအပ်ပါသည်)
|
| 78 |
+
# ---------------------------------------------------------
|
| 79 |
+
if "drive.google.com" in url and (not GOOGLE_API_KEY or GOOGLE_API_KEY == "YOUR_GOOGLE_API_KEY_HERE"):
|
| 80 |
+
if "Google Drive - Virus scan warning" in r.text or "confirm=" not in target_link:
|
| 81 |
+
confirm_token = None
|
| 82 |
+
# Cookie မှတစ်ဆင့် Virus Scan Token ကို ရှာဖွေခြင်း
|
| 83 |
+
for cookie_key, cookie_value in session.cookies.items():
|
| 84 |
+
if cookie_key.startswith("download_warning"):
|
| 85 |
+
confirm_token = cookie_value
|
| 86 |
+
break
|
| 87 |
+
|
| 88 |
+
if confirm_token:
|
| 89 |
+
# Token ရပါက Link အသစ်ဖြင့် ဒေါင်းလုဒ် ပြန်စပါမည်
|
| 90 |
+
new_target_link = f"{target_link}&confirm={confirm_token}"
|
| 91 |
+
r = session.get(new_target_link, headers=headers, stream=True, allow_redirects=True)
|
| 92 |
+
|
| 93 |
# Header များကို ပြန်လည်ပေးပို့ရန် ပြင်ဆင်ခြင်း
|
| 94 |
excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
|
| 95 |
response_headers = {name: value for name, value in r.headers.items() if name.lower() not in excluded_headers}
|
| 96 |
|
| 97 |
+
# Video ကျော်ကြည့်နိုင်ရန် Accept-Ranges နှင့် Content-Range ထည့်ပေးရပါမည်
|
| 98 |
response_headers['Accept-Ranges'] = 'bytes'
|
| 99 |
+
|
| 100 |
if 'Content-Length' in r.headers:
|
| 101 |
response_headers['Content-Length'] = r.headers['Content-Length']
|
| 102 |
+
|
| 103 |
+
if 'Content-Range' in r.headers:
|
| 104 |
+
response_headers['Content-Range'] = r.headers['Content-Range']
|
| 105 |
|
| 106 |
def iterfile():
|
| 107 |
for chunk in r.iter_content(chunk_size=1024*1024):
|