MyanmarSwe commited on
Commit
c60549e
·
verified ·
1 Parent(s): 04fa3a5

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +40 -48
main.py CHANGED
@@ -31,7 +31,7 @@ CACHE_TTL = 1800
31
  client = httpx.AsyncClient(
32
  timeout=httpx.Timeout(60.0, read=None),
33
  follow_redirects=True,
34
- limits=httpx.Limits(max_connections=200, max_keepalive_connections=50)
35
  )
36
 
37
  @app.get("/")
@@ -64,6 +64,28 @@ def get_clean_filename(url):
64
  name = decoded_url.split('/')[-1].split('?')[0]
65
  return name if (name and '.' in name) else "video.mp4"
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  @app.get("/download")
68
  async def download_proxy(request: Request, url: str, key: str = None):
69
  if key != ACCESS_KEY:
@@ -76,56 +98,28 @@ async def download_proxy(request: Request, url: str, key: str = None):
76
 
77
  # --- MediaFire Section ---
78
  if "mediafire.com" in clean_url:
79
- target_link = None
80
  cached = MEDIAFIRE_CACHE.get(clean_url)
81
-
82
- if cached and (current_time - cached['time']) < CACHE_TTL:
83
- target_link = cached['link']
84
 
85
  if not target_link:
86
- try:
87
- headers = {
88
- 'User-Agent': ua.random,
89
- 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
90
- 'Accept-Language': 'en-US,en;q=0.5',
91
- 'Referer': 'https://www.mediafire.com/'
92
- }
93
- async with httpx.AsyncClient(headers=headers, follow_redirects=True, timeout=20.0) as temp_client:
94
- r = await temp_client.get(clean_url)
95
- if r.status_code == 200:
96
- # 1. Regex ရှာဖွေခြင်း (ပိုမိုကျယ်ပြန့်သော Pattern)
97
- match = re.search(r'https?://download[^\s"\']+mediafire\.com/[^\s"\']+', r.text)
98
- if match:
99
- target_link = match.group(0).strip()
100
-
101
- # 2. BeautifulSoup နဲ့ ထပ်မံရှာဖွေခြင်း
102
- if not target_link:
103
- soup = BeautifulSoup(r.text, 'html.parser')
104
- # Download link ရှာရန် ဖြစ်နိုင်သမျှ နည်းလမ်းအားလုံးသုံးခြင်း
105
- link_tags = soup.find_all('a', href=True)
106
- for tag in link_tags:
107
- href = tag['href']
108
- if 'download' in href and 'mediafire.com' in href:
109
- target_link = href
110
- break
111
-
112
- # aria-label ထဲတွင် ဝှက်ထားသော link ကိုရှာခြင်း
113
- if not target_link:
114
- download_btn = soup.find('a', {'aria-label': re.compile(r'Download', re.I)})
115
- if download_btn: target_link = download_btn.get('href')
116
-
117
- if target_link:
118
- # // နဲ့စရင် https: ထည့်ပေးခြင်း
119
- if target_link.startswith("//"): target_link = f"https:{target_link}"
120
- MEDIAFIRE_CACHE[clean_url] = {'link': target_link, 'time': current_time}
121
- except Exception as e:
122
- print(f"Scraper Error: {e}")
123
 
124
  if target_link:
125
- return await stream_file(target_link, range_header, filename, referer=clean_url)
 
 
 
 
 
 
 
 
 
126
  else:
127
- # 404 ပြန်ရခြင်းအကြောင်းရင်းမှာ Scraper က link ရှာမတွေ့လို့ပါ
128
- raise HTTPException(status_code=404, detail="Direct link not found in MediaFire page.")
129
 
130
  # --- Google Drive Section ---
131
  elif "drive.google.com" in clean_url:
@@ -164,11 +158,9 @@ async def stream_file(target_url, range_header, filename, referer=None):
164
  req = client.build_request("GET", target_url, headers=headers)
165
  r = await client.send(req, stream=True)
166
 
167
- # HTML ပြန်ကျလာရင် (Block ခံရရင်) Cache ရှင်းပြီး Error ပြန်ပေးရန်
168
  if "text/html" in r.headers.get("Content-Type", "").lower() and r.status_code == 200:
169
  await r.aclose()
170
- for key, val in list(MEDIAFIRE_CACHE.items()):
171
- if val['link'] == target_url: del MEDIAFIRE_CACHE[key]
172
  raise HTTPException(status_code=415, detail="MediaFire detection triggered.")
173
 
174
  return await process_response(r, filename)
 
31
  client = httpx.AsyncClient(
32
  timeout=httpx.Timeout(60.0, read=None),
33
  follow_redirects=True,
34
+ limits=httpx.Limits(max_connections=300, max_keepalive_connections=100)
35
  )
36
 
37
  @app.get("/")
 
64
  name = decoded_url.split('/')[-1].split('?')[0]
65
  return name if (name and '.' in name) else "video.mp4"
66
 
67
+ async def scrape_mediafire(url):
68
+ """MediaFire Direct Link ကို ပိုမိုတိကျစွာ ရှာဖွေပေးခြင်း"""
69
+ try:
70
+ headers = {
71
+ 'User-Agent': ua.random,
72
+ 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
73
+ 'Referer': 'https://www.mediafire.com/'
74
+ }
75
+ async with httpx.AsyncClient(headers=headers, follow_redirects=True, timeout=20.0) as temp_client:
76
+ r = await temp_client.get(url)
77
+ if r.status_code == 200:
78
+ # Regex ရှာဖွေခြင်း
79
+ match = re.search(r'https?://download[^\s"\']+mediafire\.com/[^\s"\']+', r.text)
80
+ if match: return match.group(0).strip()
81
+
82
+ # BS4 နဲ့ ထပ်ရှာခြင်း
83
+ soup = BeautifulSoup(r.text, 'html.parser')
84
+ btn = soup.find('a', {'id': 'downloadButton'}) or soup.find('a', {'aria-label': re.compile(r'Download', re.I)})
85
+ if btn: return btn.get('href')
86
+ except: pass
87
+ return None
88
+
89
  @app.get("/download")
90
  async def download_proxy(request: Request, url: str, key: str = None):
91
  if key != ACCESS_KEY:
 
98
 
99
  # --- MediaFire Section ---
100
  if "mediafire.com" in clean_url:
 
101
  cached = MEDIAFIRE_CACHE.get(clean_url)
102
+ target_link = cached['link'] if (cached and (current_time - cached['time']) < CACHE_TTL) else None
 
 
103
 
104
  if not target_link:
105
+ target_link = await scrape_mediafire(clean_url)
106
+ if target_link:
107
+ if target_link.startswith("//"): target_link = f"https:{target_link}"
108
+ MEDIAFIRE_CACHE[clean_url] = {'link': target_link, 'time': current_time}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  if target_link:
111
+ # Retry Logic: Block ခံရရင် တစ်ကြိမ်ထပ်ကြိုးစားမည်
112
+ try:
113
+ return await stream_file(target_link, range_header, filename, referer=clean_url)
114
+ except HTTPException as e:
115
+ if e.status_code == 415: # HTML Blocked ဖြစ်ခဲ့ရင် Cache ဖျက်ပြီး အသစ်တခါပြန်ရှာ
116
+ del MEDIAFIRE_CACHE[clean_url]
117
+ new_link = await scrape_mediafire(clean_url)
118
+ if new_link:
119
+ return await stream_file(new_link, range_header, filename, referer=clean_url)
120
+ raise e
121
  else:
122
+ raise HTTPException(status_code=404, detail="Direct link failure")
 
123
 
124
  # --- Google Drive Section ---
125
  elif "drive.google.com" in clean_url:
 
158
  req = client.build_request("GET", target_url, headers=headers)
159
  r = await client.send(req, stream=True)
160
 
161
+ # HTML Blocked Detection
162
  if "text/html" in r.headers.get("Content-Type", "").lower() and r.status_code == 200:
163
  await r.aclose()
 
 
164
  raise HTTPException(status_code=415, detail="MediaFire detection triggered.")
165
 
166
  return await process_response(r, filename)