MyanmarSwe commited on
Commit
f722c0d
·
verified ·
1 Parent(s): fd9bdbb

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -38
main.py CHANGED
@@ -60,10 +60,11 @@ def get_google_file_id(url):
60
  return fid.group(1) if fid else None
61
 
62
  def get_clean_filename(url):
 
63
  decoded_url = urllib.parse.unquote(url)
64
  name = decoded_url.split('/')[-1].split('?')[0]
65
  if not name or '.' not in name:
66
- name = "video.mp4"
67
  return name
68
 
69
  @app.get("/download")
@@ -76,48 +77,46 @@ async def download_proxy(request: Request, url: str, key: str = None):
76
  range_header = request.headers.get('range')
77
  current_time = time.time()
78
 
79
- # --- MediaFire Section (Modified to use Redirect) ---
80
  if "mediafire.com" in clean_url:
81
  cached_data = MEDIAFIRE_CACHE.get(clean_url)
82
 
83
- # Cache ရှိရင် တိုက်ရိုက် Redirect လုပ်မည်
84
  if cached_data and (current_time - cached_data['time']) < CACHE_TTL:
85
- return RedirectResponse(url=cached_data['link'])
86
-
87
- try:
88
- req_ua = ua.random
89
- async with httpx.AsyncClient(headers={'User-Agent': req_ua}, follow_redirects=True) as temp_client:
90
- page_res = await temp_client.get(clean_url)
91
- if page_res.status_code != 200:
92
- raise HTTPException(status_code=page_res.status_code, detail="MediaFire access failed")
93
-
94
- html_content = page_res.text
95
- target_link = None
96
-
97
- match = re.search(r"https?://download[0-9]+\.mediafire\.com/[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+/[^\s'\"]+", html_content)
98
- if match:
99
- target_link = match.group(0).replace('"', '').replace("'", "")
100
-
101
- if not target_link:
102
- soup = BeautifulSoup(html_content, 'html.parser')
103
- download_btn = soup.find('a', {'id': 'downloadButton'}) or soup.find('a', {'class': 'input_btn_p'})
104
- if download_btn: target_link = download_btn.get('href')
105
-
106
- if target_link:
107
- if target_link.startswith("//"): target_link = f"https:{target_link}"
108
- elif target_link.startswith("/"): target_link = f"https://www.mediafire.com{target_link}"
109
 
110
- # သိမ်းဆည်းခြင်း
111
- MEDIAFIRE_CACHE[clean_url] = {'link': target_link, 'time': current_time}
 
 
 
112
 
113
- # Proxy အစား RedirectResponse ကိုသုံးပြီး Player ဆီ လွှဲပေးလိုက်ခြင်း
114
- return RedirectResponse(url=target_link)
115
- else:
116
- raise HTTPException(status_code=404, detail="Direct link extraction failed")
117
-
118
- except Exception as e:
119
- print(f"MediaFire Error: {e}")
120
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  # --- Google Drive Section (Stay as Proxy) ---
123
  elif "drive.google.com" in clean_url:
@@ -158,9 +157,15 @@ async def stream_file(target_url, range_header, filename):
158
  raise HTTPException(status_code=500, detail=str(e))
159
 
160
  async def process_response(r, filename):
 
161
  mime_type, _ = mimetypes.guess_type(filename)
162
  if not mime_type or 'application' in mime_type:
163
- mime_type = 'video/x-matroska' if filename.endswith('.mkv') else 'video/mp4'
 
 
 
 
 
164
 
165
  safe_headers = ['content-length', 'content-range', 'accept-ranges', 'cache-control']
166
  res_headers = {n: v for n, v in r.headers.items() if n.lower() in safe_headers}
 
60
  return fid.group(1) if fid else None
61
 
62
  def get_clean_filename(url):
63
+ """URL ထဲမှ ဖိုင်နာမည်နှင့် Extension (.mp4/.mkv) ကို တိကျစွာထုတ်ယူခြင်း"""
64
  decoded_url = urllib.parse.unquote(url)
65
  name = decoded_url.split('/')[-1].split('?')[0]
66
  if not name or '.' not in name:
67
+ return "video.mp4"
68
  return name
69
 
70
  @app.get("/download")
 
77
  range_header = request.headers.get('range')
78
  current_time = time.time()
79
 
80
+ # --- MediaFire Section ---
81
  if "mediafire.com" in clean_url:
82
  cached_data = MEDIAFIRE_CACHE.get(clean_url)
83
 
84
+ target_link = None
85
  if cached_data and (current_time - cached_data['time']) < CACHE_TTL:
86
+ target_link = cached_data['link']
87
+ else:
88
+ try:
89
+ req_ua = ua.random
90
+ async with httpx.AsyncClient(headers={'User-Agent': req_ua}, follow_redirects=True) as temp_client:
91
+ page_res = await temp_client.get(clean_url)
92
+ if page_res.status_code != 200:
93
+ raise HTTPException(status_code=page_res.status_code, detail="MediaFire access failed")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
+ html_content = page_res.text
96
+ # Direct link Regex
97
+ match = re.search(r"https?://download[0-9]+\.mediafire\.com/[a-zA-Z0-9_-]+/[a-zA-Z0-9_-]+/[^\s'\"]+", html_content)
98
+ if match:
99
+ target_link = match.group(0).replace('"', '').replace("'", "")
100
 
101
+ if not target_link:
102
+ soup = BeautifulSoup(html_content, 'html.parser')
103
+ download_btn = soup.find('a', {'id': 'downloadButton'}) or soup.find('a', {'class': 'input_btn_p'})
104
+ if download_btn: target_link = download_btn.get('href')
105
+
106
+ if target_link:
107
+ if target_link.startswith("//"): target_link = f"https:{target_link}"
108
+ elif target_link.startswith("/"): target_link = f"https://www.mediafire.com{target_link}"
109
+ MEDIAFIRE_CACHE[clean_url] = {'link': target_link, 'time': current_time}
110
+ except Exception as e:
111
+ raise HTTPException(status_code=500, detail=str(e))
112
+
113
+ if target_link:
114
+ # .mp4 သို့မဟုတ် .mkv ဖြစ်ကြောင်း Player သိစေရန် Extension hint ထည့်ခြင်း
115
+ separator = "&" if "?" in target_link else "?"
116
+ hinted_link = f"{target_link}{separator}type=video&file={urllib.parse.quote(filename)}"
117
+ return RedirectResponse(url=hinted_link)
118
+ else:
119
+ raise HTTPException(status_code=404, detail="Direct link extraction failed")
120
 
121
  # --- Google Drive Section (Stay as Proxy) ---
122
  elif "drive.google.com" in clean_url:
 
157
  raise HTTPException(status_code=500, detail=str(e))
158
 
159
  async def process_response(r, filename):
160
+ # MIME Type ကို .mp4 သို့မဟုတ် .mkv အလိုက် အလိုအလျောက် သတ်မှတ်ခြင်း
161
  mime_type, _ = mimetypes.guess_type(filename)
162
  if not mime_type or 'application' in mime_type:
163
+ if filename.lower().endswith('.mp4'):
164
+ mime_type = 'video/mp4'
165
+ elif filename.lower().endswith('.mkv'):
166
+ mime_type = 'video/x-matroska'
167
+ else:
168
+ mime_type = 'video/mp4'
169
 
170
  safe_headers = ['content-length', 'content-range', 'accept-ranges', 'cache-control']
171
  res_headers = {n: v for n, v in r.headers.items() if n.lower() in safe_headers}