Update app.py
Browse files
app.py
CHANGED
@@ -25,6 +25,7 @@ from collections import defaultdict
|
|
25 |
from starlette.responses import JSONResponse
|
26 |
import logging
|
27 |
import gc
|
|
|
28 |
|
29 |
tmp_dir = tempfile.gettempdir()
|
30 |
BASE_URL = "https://chrunos-zam.hf.space"
|
@@ -122,29 +123,60 @@ async def get_video_url(youtube_url: str):
|
|
122 |
# Define a global temporary download directory
|
123 |
global_download_dir = tempfile.mkdtemp()
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
def get_user_ip(request: Request) -> str:
|
130 |
"""Helper function to get user's IP address."""
|
|
|
|
|
|
|
131 |
return request.client.host
|
132 |
|
133 |
@app.post("/maxs")
|
134 |
async def download_high_quality_video(request: Request):
|
135 |
user_ip = get_user_ip(request)
|
136 |
-
user_info = request_counts[user_ip]
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
data = await request.json()
|
149 |
video_url = data.get('url')
|
150 |
quality = data.get('quality', '1080') # Default to 1080p if not specified
|
@@ -192,12 +224,10 @@ async def download_high_quality_video(request: Request):
|
|
192 |
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
193 |
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
194 |
|
195 |
-
# Increment the user's request count
|
196 |
-
user_info["count"] += 1
|
197 |
|
198 |
gc.collect()
|
199 |
|
200 |
-
return {"url": download_url}
|
201 |
|
202 |
|
203 |
|
|
|
25 |
from starlette.responses import JSONResponse
|
26 |
import logging
|
27 |
import gc
|
28 |
+
from typing import Dict, Any
|
29 |
|
30 |
tmp_dir = tempfile.gettempdir()
|
31 |
BASE_URL = "https://chrunos-zam.hf.space"
|
|
|
123 |
# Define a global temporary download directory
|
124 |
global_download_dir = tempfile.mkdtemp()
|
125 |
|
126 |
+
|
127 |
+
class RateLimiter:
|
128 |
+
def __init__(self, max_requests: int, time_window: timedelta):
|
129 |
+
self.max_requests = max_requests
|
130 |
+
self.time_window = time_window
|
131 |
+
self.requests: Dict[str, list] = defaultdict(list)
|
132 |
+
|
133 |
+
def _cleanup_old_requests(self, user_ip: str) -> None:
|
134 |
+
"""Remove requests that are outside the time window."""
|
135 |
+
current_time = time.time()
|
136 |
+
self.requests[user_ip] = [
|
137 |
+
timestamp for timestamp in self.requests[user_ip]
|
138 |
+
if current_time - timestamp < self.time_window.total_seconds()
|
139 |
+
]
|
140 |
+
|
141 |
+
def is_rate_limited(self, user_ip: str) -> bool:
|
142 |
+
"""Check if the user has exceeded their rate limit."""
|
143 |
+
self._cleanup_old_requests(user_ip)
|
144 |
+
|
145 |
+
# Add current request
|
146 |
+
current_time = time.time()
|
147 |
+
self.requests[user_ip].append(current_time)
|
148 |
+
|
149 |
+
# Check if user has exceeded the maximum requests
|
150 |
+
return len(self.requests[user_ip]) > self.max_requests
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
# Initialize rate limiter with 100 requests per day
|
155 |
+
rate_limiter = RateLimiter(
|
156 |
+
max_requests=5,
|
157 |
+
time_window=timedelta(days=1)
|
158 |
+
)
|
159 |
|
160 |
def get_user_ip(request: Request) -> str:
|
161 |
"""Helper function to get user's IP address."""
|
162 |
+
forwarded = request.headers.get("X-Forwarded-For")
|
163 |
+
if forwarded:
|
164 |
+
return forwarded.split(",")[0]
|
165 |
return request.client.host
|
166 |
|
167 |
@app.post("/maxs")
|
168 |
async def download_high_quality_video(request: Request):
|
169 |
user_ip = get_user_ip(request)
|
|
|
170 |
|
171 |
+
if rate_limiter.is_rate_limited(user_ip):
|
172 |
+
raise HTTPException(
|
173 |
+
status_code=429,
|
174 |
+
detail={
|
175 |
+
"error": "You have exceeded the maximum number of requests per day. Please try again tomorrow.",
|
176 |
+
"url": "https://t.me/chrunoss"
|
177 |
+
}
|
178 |
+
)
|
179 |
+
|
|
|
180 |
data = await request.json()
|
181 |
video_url = data.get('url')
|
182 |
quality = data.get('quality', '1080') # Default to 1080p if not specified
|
|
|
224 |
encoded_filename = urllib.parse.quote(downloaded_file.name)
|
225 |
download_url = f"{BASE_URL}/file/{encoded_filename}"
|
226 |
|
|
|
|
|
227 |
|
228 |
gc.collect()
|
229 |
|
230 |
+
return {"url": download_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
|
231 |
|
232 |
|
233 |
|