Update app.py
Browse files
app.py
CHANGED
@@ -60,11 +60,9 @@ class GifChatBot:
|
|
60 |
URLs returning HTML or very small content (e.g. < 10KB) are rejected.
|
61 |
"""
|
62 |
try:
|
63 |
-
# Always attempt verification
|
64 |
-
# so we can filter out error pages (which often return HTML).
|
65 |
response = self.session.head(gif_url, timeout=3, allow_redirects=True)
|
66 |
if response.status_code != 200:
|
67 |
-
# Fallback to GET if HEAD doesn't succeed.
|
68 |
response = self.session.get(gif_url, timeout=5, stream=True, allow_redirects=True)
|
69 |
if response.status_code == 200:
|
70 |
content_type = response.headers.get('content-type', '').lower()
|
@@ -121,7 +119,6 @@ class GifChatBot:
|
|
121 |
return None
|
122 |
|
123 |
except Exception as error:
|
124 |
-
# Suppress error logging here to avoid flooding your console.
|
125 |
return None
|
126 |
|
127 |
def _get_trending_gif(self) -> Optional[str]:
|
@@ -154,15 +151,23 @@ class GifChatBot:
|
|
154 |
return None
|
155 |
|
156 |
def _is_good_quality_gif(self, gif: Dict) -> bool:
|
157 |
-
"""Check if a GIF meets quality criteria"""
|
158 |
try:
|
159 |
-
|
|
|
|
|
160 |
return False
|
161 |
|
162 |
-
if
|
|
|
|
|
163 |
return False
|
164 |
|
165 |
-
|
|
|
|
|
|
|
|
|
166 |
return False
|
167 |
|
168 |
return True
|
|
|
60 |
URLs returning HTML or very small content (e.g. < 10KB) are rejected.
|
61 |
"""
|
62 |
try:
|
63 |
+
# Always attempt verification to filter out error pages.
|
|
|
64 |
response = self.session.head(gif_url, timeout=3, allow_redirects=True)
|
65 |
if response.status_code != 200:
|
|
|
66 |
response = self.session.get(gif_url, timeout=5, stream=True, allow_redirects=True)
|
67 |
if response.status_code == 200:
|
68 |
content_type = response.headers.get('content-type', '').lower()
|
|
|
119 |
return None
|
120 |
|
121 |
except Exception as error:
|
|
|
122 |
return None
|
123 |
|
124 |
def _get_trending_gif(self) -> Optional[str]:
|
|
|
151 |
return None
|
152 |
|
153 |
def _is_good_quality_gif(self, gif: Dict) -> bool:
|
154 |
+
"""Check if a GIF meets quality criteria: number of frames, maximum size, and minimum size (500 KB)"""
|
155 |
try:
|
156 |
+
original = gif.get("images", {}).get("original", {})
|
157 |
+
# Reject if too many frames (more than 50)
|
158 |
+
if int(original.get("frames", 50)) > 50:
|
159 |
return False
|
160 |
|
161 |
+
# Reject if file size is above 2MB
|
162 |
+
size = int(original.get("size", 0))
|
163 |
+
if size > 2000000:
|
164 |
return False
|
165 |
|
166 |
+
# New check: Accept only GIFs above 500 KB
|
167 |
+
if size < 500000:
|
168 |
+
return False
|
169 |
+
|
170 |
+
if not original.get("url"):
|
171 |
return False
|
172 |
|
173 |
return True
|