mirror of
https://github.com/lifekiller/rarbg.git
synced 2025-05-16 23:40:14 -07:00
use async context manager for rate limiting
This commit is contained in:
parent
2fa07bee4e
commit
2d0ae85196
36
rarbg.py
36
rarbg.py
@ -42,15 +42,26 @@ 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):
|
class RateLimit:
|
||||||
now = datetime.now()
|
|
||||||
app.next_call = max(app.next_call, now) + API_RATE_LIMIT
|
def __init__(self, rate_limit):
|
||||||
sleep = app.next_call - now - API_RATE_LIMIT
|
self.rate_limit = rate_limit
|
||||||
await asyncio.sleep(sleep.total_seconds())
|
self.next_call = datetime.now()
|
||||||
return (await get(*args, **kwds))
|
|
||||||
|
async def __aenter__(self):
|
||||||
|
now = datetime.now()
|
||||||
|
self.next_call = max(now, self.next_call) + self.rate_limit
|
||||||
|
sleep = self.next_call - now - self.rate_limit
|
||||||
|
print('sleep', str(sleep))
|
||||||
|
await asyncio.sleep(sleep.total_seconds())
|
||||||
|
|
||||||
|
async def __aexit__(self, exc_type, exc, tb):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
api_rate_limit = RateLimit(API_RATE_LIMIT)
|
||||||
|
|
||||||
|
|
||||||
async def update_token():
|
async def update_token():
|
||||||
@ -63,15 +74,16 @@ async def update_token():
|
|||||||
|
|
||||||
|
|
||||||
async def api(params):
|
async def api(params):
|
||||||
print(params)
|
print('request', 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 rate_limited_get(API_ENDPOINT, params=params)
|
async with api_rate_limit:
|
||||||
data = await resp.json()
|
resp = await get(API_ENDPOINT, params=params)
|
||||||
|
data = await resp.json()
|
||||||
|
|
||||||
if 'error' in data:
|
if 'error' in data:
|
||||||
print('! too many requests')
|
print('too many requests')
|
||||||
return web.HTTPServiceUnavailable(text=data['error'])
|
return web.HTTPServiceUnavailable(text=data['error'])
|
||||||
|
|
||||||
for i in data['torrent_results']:
|
for i in data['torrent_results']:
|
||||||
@ -81,6 +93,8 @@ async def api(params):
|
|||||||
hash=parse_qs(i['download'])['magnet:?xt'][0].split(':')[-1],
|
hash=parse_qs(i['download'])['magnet:?xt'][0].split(':')[-1],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
print('response', params)
|
||||||
|
|
||||||
result = TEMPLATE.render(title='rarbg', entries=data['torrent_results'])
|
result = TEMPLATE.render(title='rarbg', entries=data['torrent_results'])
|
||||||
return web.Response(text=result)
|
return web.Response(text=result)
|
||||||
|
|
||||||
|
2
setup.py
2
setup.py
@ -2,7 +2,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='rarbg',
|
name='rarbg',
|
||||||
version='0.1-dev',
|
version='0.2-dev',
|
||||||
description='RSS interface to TorrentAPI',
|
description='RSS interface to TorrentAPI',
|
||||||
url='https://github.com/banteg/rarbg',
|
url='https://github.com/banteg/rarbg',
|
||||||
py_modules=['rarbg'],
|
py_modules=['rarbg'],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user