MyanmarSwe commited on
Commit
dc2aa5a
·
verified ·
1 Parent(s): e93f6bd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +34 -24
main.py CHANGED
@@ -1,4 +1,4 @@
1
- from fastapi import FastAPI, HTTPException, Query, Header
2
  from fastapi.responses import StreamingResponse
3
  import requests
4
  from bs4 import BeautifulSoup
@@ -6,14 +6,12 @@ import re
6
  import base64
7
 
8
  app = FastAPI()
 
9
 
10
  @app.get("/")
11
  def index():
12
  return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
13
 
14
- # ကိုယ်တိုင်သုံးမည့် Password သတ်မှတ်ရန်
15
- ACCESS_KEY = "969786"
16
-
17
  def get_direct_url(url):
18
  if "drive.google.com" in url:
19
  fid = re.search(r'/(?:d|file/d|open\?id=)/([a-zA-Z0-9_-]+)', url)
@@ -33,30 +31,42 @@ def get_direct_url(url):
33
  return None
34
 
35
  @app.get("/download")
36
- def download_proxy(url: str, key: str = None):
37
- # Password စစ်ဆေးခြင်း
38
  if key != ACCESS_KEY:
39
- raise HTTPException(status_code=403, detail="Access Denied: Invalid Key")
40
 
41
  target_link = get_direct_url(url)
42
  if not target_link:
43
- raise HTTPException(status_code=400, detail="Unsupported or Invalid Link")
44
-
45
- try:
46
- r = requests.get(target_link, stream=True, allow_redirects=True)
47
- file_name = "downloaded_file"
48
- if "content-disposition" in r.headers:
49
- fname_match = re.findall('filename="(.+)"', r.headers['content-disposition'])
50
- if fname_match: file_name = fname_match[0].strip('"')
51
-
52
- def iterfile():
53
- for chunk in r.iter_content(chunk_size=1024*1024):
54
- if chunk: yield chunk
55
-
56
- return StreamingResponse(iterfile(), media_type="application/octet-stream",
57
- headers={"Content-Disposition": f"attachment; filename={file_name}"})
58
- except Exception as e:
59
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  if __name__ == "__main__":
62
  import uvicorn
 
1
+ from fastapi import FastAPI, HTTPException, Query, Request
2
  from fastapi.responses import StreamingResponse
3
  import requests
4
  from bs4 import BeautifulSoup
 
6
  import base64
7
 
8
  app = FastAPI()
9
+ ACCESS_KEY = "969786"
10
 
11
  @app.get("/")
12
  def index():
13
  return {"message": "Proxy is Online!", "usage": "/download?url=[LINK]&key=[YOUR_KEY]"}
14
 
 
 
 
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)
 
31
  return None
32
 
33
  @app.get("/download")
34
+ async def download_proxy(request: Request, url: str, key: str = None):
 
35
  if key != ACCESS_KEY:
36
+ raise HTTPException(status_code=403, detail="Access Denied")
37
 
38
  target_link = get_direct_url(url)
39
  if not target_link:
40
+ raise HTTPException(status_code=400, detail="Invalid Link")
41
+
42
+ # Client ဆီကလာတဲ့ Range Header ကို ရယူခြင်း (Video ကျော်ကြည့်ရန်အတွက်)
43
+ range_header = request.headers.get('range')
44
+ headers = {}
45
+ if range_header:
46
+ headers['Range'] = range_header
47
+
48
+ # Target Server (MediaFire/GDrive) ဆီကို Range Header နဲ့ လှမ်းတောင်းခြင်း
49
+ r = requests.get(target_link, headers=headers, stream=True, allow_redirects=True)
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 ကျော်ကြည့်နိုင်ရန် 'Accept-Ranges': 'bytes' ထည့်ပေးရပါမည်
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):
62
+ if chunk: yield chunk
63
+
64
+ return StreamingResponse(
65
+ iterfile(),
66
+ status_code=r.status_code,
67
+ headers=response_headers,
68
+ media_type=r.headers.get('Content-Type', 'application/octet-stream')
69
+ )
70
 
71
  if __name__ == "__main__":
72
  import uvicorn