Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,7 @@ from utils.instaloader_utils import fetch_user_posts, fetch_competitors_data
|
|
16 |
import torch
|
17 |
from torchvision import transforms
|
18 |
from transformers import ResNetForImageClassification
|
|
|
19 |
|
20 |
# Set up logging
|
21 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -47,9 +48,17 @@ logger = logging.getLogger(__name__)
|
|
47 |
RATE_LIMIT_DELAY = 5 # Delay in seconds between API calls
|
48 |
LAST_REQUEST_TIME = 0
|
49 |
|
50 |
-
# Pydantic model for request validation
|
51 |
class UserRequest(BaseModel):
|
52 |
-
username:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
@app.post("/fetch-posts")
|
55 |
async def fetch_posts(user: UserRequest):
|
|
|
16 |
import torch
|
17 |
from torchvision import transforms
|
18 |
from transformers import ResNetForImageClassification
|
19 |
+
import re
|
20 |
|
21 |
# Set up logging
|
22 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
48 |
RATE_LIMIT_DELAY = 5 # Delay in seconds between API calls
|
49 |
LAST_REQUEST_TIME = 0
|
50 |
|
|
|
51 |
class UserRequest(BaseModel):
|
52 |
+
username: str = Field(..., min_length=1, max_length=30)
|
53 |
+
|
54 |
+
@validator("username")
|
55 |
+
def validate_username(cls, value):
|
56 |
+
"""
|
57 |
+
Validate that the username contains only alphanumeric characters, dots, and underscores.
|
58 |
+
"""
|
59 |
+
if not re.match(r"^[a-zA-Z0-9._]+$", value):
|
60 |
+
raise ValueError("Username can only contain alphanumeric characters, dots, and underscores.")
|
61 |
+
return value
|
62 |
|
63 |
@app.post("/fetch-posts")
|
64 |
async def fetch_posts(user: UserRequest):
|