summaryrefslogtreecommitdiff
path: root/crawler.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2017-03-28 18:29:39 +0200
committerschneefux <schneefux+commit@schneefux.xyz>2017-03-28 18:29:39 +0200
commit151d1ff466c1d2904eafe6f0d383f89978114cb0 (patch)
treefdb2a0420f6be6f8cd16373fa591ed9f9a7e48c3 /crawler.py
parent3c7b19d77476d774762b4a6c97bff18bcd0b249a (diff)
downloadapigrabber-151d1ff466c1d2904eafe6f0d383f89978114cb0.tar.gz
apigrabber-151d1ff466c1d2904eafe6f0d383f89978114cb0.zip
rewrite in NodeJS
Diffstat (limited to 'crawler.py')
-rw-r--r--crawler.py90
1 files changed, 0 insertions, 90 deletions
diff --git a/crawler.py b/crawler.py
deleted file mode 100644
index 84e4692..0000000
--- a/crawler.py
+++ /dev/null
@@ -1,90 +0,0 @@
-#!/usr/bin/python
-
-import json
-import time
-import logging
-import requests
-
-APIURL = "https://api.dc01.gamelockerapp.com/"
-
-
-class ApiError(Exception):
- pass
-
-class Crawler(object):
- def __init__(self, token):
- """Sets constants."""
- self._apiurl = APIURL
- self._token = token
- self._pagelimit = 50
-
- def _req(self, path, params):
- """Sends an API request and returns the response dict.
-
- :param path: URL path.
- :type path: str
- :param params: Request parameters.
- :type params: dict
- :return: API response.
- :rtype: dict
- """
- headers = {
- "Authorization": "Bearer " + self._token,
- "X-TITLE-ID": "semc-vainglory",
- "Accept": "application/vnd.api+json",
- "Accept-Encoding": "gzip"
- }
- retries = 5
- while True:
- try:
- response = requests.get(self._apiurl + path,
- headers=headers,
- params=params)
- if response.status_code == 429:
- logging.warning("rate limited, retrying")
- else:
- if response.status_code > 500:
- logging.error("API server error %s",
- response.status_code)
- raise ApiError(response.status_code)
- else:
- return response.json()
- except (json.decoder.JSONDecodeError) as err:
- # API bug?
- logging.error("API error '%s', retrying", err)
- retries -= 1
- if retries == 0:
- logging.error("Giving up")
- raise ApiError(str(err))
-
- time.sleep(5)
-
- def matches(self, params, region="na"):
- """Queries the API for matches and their related data.
-
- :param region: (optional) Region where the matches were played.
- Defaults to "na" (North America).
- :type region: str
- :param params: Additional filters.
- :type params: dict
- """
- params["page[offset]"] = 0
- params["page[limit]"] = self._pagelimit
- while True:
- res = self._req("shards/" + region + "/matches",
- params)
-
- if "errors" in res:
- if res["errors"][0].get("title") == "Not Found" \
- and params["page[offset]"] > 0:
- # a query returned exactly 50 matches
- # which is expected, so don't fail.
- return
- raise ApiError(res["errors"])
-
- yield res
-
- if len(res["data"]) < self._pagelimit:
- # asked for 50, got less -> exhausted
- break
- params["page[offset]"] += params["page[limit]"]