-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
290 lines (236 loc) · 8.93 KB
/
Copy pathmain.py
File metadata and controls
290 lines (236 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from fastapi import FastAPI, HTTPException, Depends, Security
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from pydantic import BaseModel
from typing import List
import uvicorn
import os
import logging
from functools import lru_cache
import time
from collections import defaultdict
from rating_fetcher import RatingFetcher
from dotenv import load_dotenv
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Load environment variables from .env file
load_dotenv()
# Security
security = HTTPBearer()
API_KEY = os.getenv("API_KEY")
if not API_KEY:
logger.error("API_KEY environment variable is required")
raise ValueError("API_KEY environment variable is required")
logger.info("Starting CPStats API...")
# Rate limiting
request_counts = defaultdict(list)
RATE_LIMIT = int(os.getenv("RATE_LIMIT_REQUESTS", "100"))
RATE_WINDOW = int(os.getenv("RATE_LIMIT_WINDOW", "3600"))
# Rate limiting
request_counts = defaultdict(list)
RATE_LIMIT = int(os.getenv("RATE_LIMIT_REQUESTS", "100"))
RATE_WINDOW = int(os.getenv("RATE_LIMIT_WINDOW", "3600"))
def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security)):
"""Verify API key for authentication"""
if credentials.credentials != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
return credentials.credentials
def rate_limit_check(client_ip: str):
"""Check if client has exceeded rate limit"""
now = time.time()
# Clean old requests
request_counts[client_ip] = [req_time for req_time in request_counts[client_ip]
if now - req_time < RATE_WINDOW]
if len(request_counts[client_ip]) >= RATE_LIMIT:
raise HTTPException(status_code=429, detail="Rate limit exceeded")
request_counts[client_ip].append(now)
app = FastAPI(
title="CPStats-API",
description="REST API for fetching competitive programming ratings from multiple platforms",
version="1.0.0",
docs_url="/docs" if os.getenv("DEBUG",
"False").lower() == "true" else None,
redoc_url="/redoc" if os.getenv("DEBUG",
"False").lower() == "true" else None
)
# CORS middleware with strict origins for production
allowed_origins = []
origins_env = os.getenv("ALLOWED_ORIGINS", "")
if origins_env:
allowed_origins = [origin.strip() for origin in origins_env.split(",")]
else:
# Default to restrictive CORS in production
allowed_origins = ["https://discord.com"]
app.add_middleware(
CORSMiddleware,
allow_origins=allowed_origins,
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["*"],
)
# Cache for ratings (simple in-memory cache)
@lru_cache(maxsize=1000)
def cached_single_rating(platform: str, username: str, cache_key: int):
"""Cached version of single rating fetch"""
return rating_fetcher.get_rating_by_platform(platform, username)
def get_cache_key():
"""Generate cache key based on CACHE_TTL"""
cache_ttl = int(os.getenv("CACHE_TTL", "14400")) # Default to 4 hours
return int(time.time() // cache_ttl)
# Initialize rating fetcher
rating_fetcher = RatingFetcher()
# Pydantic models for request validation
class RatingRequest(BaseModel):
platform: str
username: str
class MultipleRatingRequest(BaseModel):
requests: List[RatingRequest]
class SingleRatingRequest(BaseModel):
platform: str
username: str
@app.get("/")
async def root():
"""Root endpoint with API information"""
return {
"message": "CPStats API",
"version": "1.0.0",
"status": "online",
"supported_platforms": ["leetcode", "codeforces", "codechef", "atcoder"],
"endpoints": {
"single_rating": "/rating/{platform}/{username}",
"multiple_ratings": "/ratings (POST)",
"health": "/health",
"platforms": "/platforms"
},
"rate_limit": f"{RATE_LIMIT} requests per {RATE_WINDOW//60} minutes"
}
@app.get("/health")
async def health_check():
"""Health check endpoint with system information"""
try:
# Basic health check
health_data = {
"status": "healthy",
"message": "API is running",
"timestamp": int(time.time()),
"version": "1.0.0"
}
# Add environment info (non-sensitive)
health_data["environment"] = {
"debug": os.getenv("DEBUG", "False").lower() == "true",
"cache_enabled": os.getenv("ENABLE_CACHE", "True").lower() == "true",
"rate_limit": f"{RATE_LIMIT} requests per {RATE_WINDOW//60} minutes"
}
return health_data
except Exception as e:
logger.error(f"Health check failed: {str(e)}")
raise HTTPException(status_code=503, detail="Service unavailable")
@app.get("/rating/{platform}/{username}")
async def get_single_rating(
platform: str,
username: str,
api_key: str = Depends(verify_api_key)
):
"""
Get rating for a single platform and username (Requires API key)
- **platform**: Platform name (leetcode, codeforces, codechef, atcoder)
- **username**: Username on the platform
"""
try:
# Use cache if enabled
if os.getenv("ENABLE_CACHE", "True").lower() == "true":
cache_key = get_cache_key()
result = cached_single_rating(platform, username, cache_key)
else:
result = rating_fetcher.get_rating_by_platform(platform, username)
return result
except Exception as e:
logger.error(f"Error fetching single rating: {str(e)}")
raise HTTPException(
status_code=500, detail="Internal server error")
@app.post("/ratings")
async def get_multiple_ratings(
request: MultipleRatingRequest,
api_key: str = Depends(verify_api_key)
):
"""
Get ratings for multiple platform/username pairs (Requires API key)
Request body should contain a list of platform/username pairs with API key
"""
try:
if not request.requests:
raise HTTPException(status_code=400, detail="No requests provided")
if len(request.requests) > 20: # Limit batch size
raise HTTPException(
status_code=400, detail="Maximum 20 requests per batch")
# Convert Pydantic models to dicts
requests_data = [req.dict() for req in request.requests]
result = rating_fetcher.get_multiple_ratings(requests_data)
return result
except Exception as e:
logger.error(f"Error fetching multiple ratings: {str(e)}")
raise HTTPException(
status_code=500, detail="Internal server error")
@app.post("/rating")
async def get_single_rating_post(
request: SingleRatingRequest,
api_key: str = Depends(verify_api_key)
):
"""
Get rating for a single platform and username (POST method, Requires API key)
"""
try:
if os.getenv("ENABLE_CACHE", "True").lower() == "true":
cache_key = get_cache_key()
result = cached_single_rating(
request.platform, request.username, cache_key)
else:
result = rating_fetcher.get_rating_by_platform(
request.platform, request.username)
return result
except Exception as e:
logger.error(f"Error fetching single rating (POST): {str(e)}")
raise HTTPException(
status_code=500, detail="Internal server error")
@app.get("/platforms")
async def get_supported_platforms():
"""Get list of supported platforms"""
return {
"platforms": [
{
"name": "leetcode",
"description": "LeetCode competitive programming platform",
"rating_type": "Contest rating"
},
{
"name": "codeforces",
"description": "Codeforces competitive programming platform",
"rating_type": "Contest rating"
},
{
"name": "codechef",
"description": "CodeChef competitive programming platform",
"rating_type": "Contest rating"
},
{
"name": "atcoder",
"description": "AtCoder competitive programming platform",
"rating_type": "Contest rating"
}
]
}
if __name__ == "__main__":
host = os.getenv("API_HOST", "0.0.0.0")
# Use port 7860 for Hugging Face Spaces, 8000 as fallback
port = int(os.getenv("PORT", os.getenv("API_PORT", "7860")))
debug = os.getenv("DEBUG", "False").lower() == "true"
logger.info(f"Starting server on {host}:{port}")
# For production/container environments, don't use reload
if debug and os.getenv("CONTAINER_ENV") != "true":
uvicorn.run("main:app", host=host, port=port, reload=True)
else:
uvicorn.run(app, host=host, port=port)