mirror of
https://github.com/lifekiller/rarbg.git
synced 2025-06-01 15:20:21 -07:00
smarter rate limiting
This commit is contained in:
parent
339b41c188
commit
2fa07bee4e
12
rarbg.py
12
rarbg.py
@ -13,6 +13,7 @@ from humanize import naturalsize
|
|||||||
from jinja2 import Template
|
from jinja2 import Template
|
||||||
|
|
||||||
API_ENDPOINT = 'https://torrentapi.org/pubapi_v2.php'
|
API_ENDPOINT = 'https://torrentapi.org/pubapi_v2.php'
|
||||||
|
API_RATE_LIMIT = timedelta(seconds=2)
|
||||||
TOKEN_LIFESPAN = timedelta(minutes=15)
|
TOKEN_LIFESPAN = timedelta(minutes=15)
|
||||||
|
|
||||||
TEMPLATE = Template('''
|
TEMPLATE = Template('''
|
||||||
@ -41,6 +42,15 @@ TEMPLATE = Template('''
|
|||||||
app = web.Application()
|
app = web.Application()
|
||||||
app.token = None
|
app.token = None
|
||||||
app.token_got = datetime.now()
|
app.token_got = datetime.now()
|
||||||
|
app.next_call = datetime.now()
|
||||||
|
|
||||||
|
|
||||||
|
async def rate_limited_get(*args, **kwds):
|
||||||
|
now = datetime.now()
|
||||||
|
app.next_call = max(app.next_call, now) + API_RATE_LIMIT
|
||||||
|
sleep = app.next_call - now - API_RATE_LIMIT
|
||||||
|
await asyncio.sleep(sleep.total_seconds())
|
||||||
|
return (await get(*args, **kwds))
|
||||||
|
|
||||||
|
|
||||||
async def update_token():
|
async def update_token():
|
||||||
@ -57,7 +67,7 @@ async def api(params):
|
|||||||
await update_token()
|
await update_token()
|
||||||
params.update(token=app.token, format='json_extended')
|
params.update(token=app.token, format='json_extended')
|
||||||
|
|
||||||
resp = await get(API_ENDPOINT, params=params)
|
resp = await rate_limited_get(API_ENDPOINT, params=params)
|
||||||
data = await resp.json()
|
data = await resp.json()
|
||||||
|
|
||||||
if 'error' in data:
|
if 'error' in data:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user