rate limiter 0.2 sec

This commit is contained in:
vrubelroman 2025-11-18 15:10:19 +03:00
parent e0e8fa8f6b
commit 24d53e731a
4 changed files with 127 additions and 0 deletions

View file

@ -2,6 +2,7 @@ import aiohttp
import logging
from typing import Optional, Dict, Any
from config import LICHESS_API_BASE_URL, LICHESS_STATS_API_BASE_URL
from rate_limiter import get_rate_limiter
logger = logging.getLogger(__name__)
@ -9,9 +10,11 @@ class LichessAPI:
def __init__(self):
self.lichess_base_url = LICHESS_API_BASE_URL
self.stats_base_url = LICHESS_STATS_API_BASE_URL
self.rate_limiter = get_rate_limiter()
async def get_user_profile(self, token: str) -> Optional[Dict[str, Any]]:
"""Get user profile from Lichess API using token"""
await self.rate_limiter.wait_if_needed()
headers = {"Authorization": f"Bearer {token}"}
try:
@ -31,6 +34,7 @@ class LichessAPI:
async def get_today_stats(self, username: str) -> Optional[Dict[str, Any]]:
"""Get today's statistics from our stats API"""
await self.rate_limiter.wait_if_needed()
try:
async with aiohttp.ClientSession() as session:
async with session.get(
@ -47,6 +51,7 @@ class LichessAPI:
async def get_yesterday_stats(self, username: str) -> Optional[Dict[str, Any]]:
"""Get yesterday's statistics from our stats API"""
await self.rate_limiter.wait_if_needed()
try:
async with aiohttp.ClientSession() as session:
async with session.get(
@ -63,6 +68,7 @@ class LichessAPI:
async def get_week_stats(self, username: str) -> Optional[Dict[str, Any]]:
"""Get week's statistics from our stats API"""
await self.rate_limiter.wait_if_needed()
try:
async with aiohttp.ClientSession() as session:
async with session.get(
@ -79,6 +85,7 @@ class LichessAPI:
async def get_games_period(self, username: str, since: int, until: int, rated_only: Optional[bool] = None) -> Optional[Dict[str, Any]]:
"""Get games for a specific period"""
await self.rate_limiter.wait_if_needed()
try:
url = f"{self.stats_base_url}/games/{username}/period"
params = {"since": since, "until": until}
@ -102,6 +109,7 @@ class LichessAPI:
async def get_puzzles_period(self, token: str, since: int, until: int, max_puzzles: int = 150) -> Optional[Dict[str, Any]]:
"""Get puzzles for a specific period"""
await self.rate_limiter.wait_if_needed()
headers = {"Authorization": f"Bearer {token}"}
try:
@ -122,6 +130,7 @@ class LichessAPI:
async def check_user_exists(self, username: str) -> bool:
"""Check if user exists on Lichess"""
await self.rate_limiter.wait_if_needed()
try:
async with aiohttp.ClientSession() as session:
async with session.get(
@ -141,6 +150,7 @@ class LichessAPI:
async def get_user_ratings(self, username: str) -> Optional[Dict[str, Any]]:
"""Get user ratings from Lichess API"""
await self.rate_limiter.wait_if_needed()
try:
async with aiohttp.ClientSession() as session:
async with session.get(