diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2017-02-25 13:44:19 +0100 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2017-02-25 13:44:19 +0100 |
| commit | e1b0c99706fc7c0d1b95f4e047b9ac37539a59cb (patch) | |
| tree | 914d4d81d2f4725081fc6aabd35600d700cd15ca /crawler.py | |
| parent | 1c550fdc51d16ec15c39edb346c28d5f07a2a425 (diff) | |
| download | apigrabber-e1b0c99706fc7c0d1b95f4e047b9ac37539a59cb.tar.gz apigrabber-e1b0c99706fc7c0d1b95f4e047b9ac37539a59cb.zip | |
respect rate limits
Diffstat (limited to 'crawler.py')
| -rw-r--r-- | crawler.py | 21 |
1 files changed, 16 insertions, 5 deletions
@@ -33,11 +33,18 @@ class Crawler(object): "Content-Encoding": "gzip" } try: - async with session.get(self._apiurl + path, headers=headers, - params=params) as response: - assert response.status == 200 - return await response.json() - except (aiohttp.errors.ClientResponseError, RuntimeError): + while True: + async with session.get(self._apiurl + path, headers=headers, + params=params) as response: + if response.status == 429: + logging.warning("hit by rate limit, retrying") + await asyncio.sleep(10) + continue + assert response.status == 200 + return await response.json() + except (aiohttp.errors.ClientResponseError, + RuntimeError, + aiohttp.errors.ContentEncodingError): logging.warning("error connecting to API, retrying") return await self._req(session, path, params) @@ -81,6 +88,10 @@ class Crawler(object): data += res["data"] + res["included"] + if len(res["data"]) < 50: + # asked for 50, got less -> exhausted + break + if not forever: break # stop after one iteration |
