Open nvkelso opened 7 years ago
Ian's done a bit of that for vector tiles in his Tilepacks work here.
def fetch_tile(format_args): sleep_time = 0.5 * random.uniform(1.0, 1.7) response_info = [] while True: url = 'https://tile.mapzen.com/mapzen/vector/v1/{layer}/{zoom}/{x}/{y}.{fmt}?api_key={api_key}'.format(**format_args) try: start = time.time() resp = sess.get(url, timeout=(6.1, 30)) data = resp.content finish = time.time() response_info.append({ "url": url, "response status": resp.status_code, "server": resp.headers.get('Server'), "time to headers millis": int(resp.elapsed.total_seconds() * 1000), "time for content millis": int((finish - start) * 1000), "response length bytes": len(data), }) resp.raise_for_status() return (format_args, response_info, data) except requests.exceptions.RequestException as e: if isinstance(e, requests.exceptions.HTTPError): if e.response.status_code == 404: print("HTTP 404 -- {} while retrieving {}. Not trying again.".format( e.response.status_code, e.response.text, url) ) return (format_args, response_info, None) else: print("HTTP error {} -- {} while retrieving {}, retrying after {:0.2f} sec".format( e.response.status_code, e.response.text, url, sleep_time) ) else: print("{} while retrieving {}, retrying after {:0.2f} sec".format( type(e), url, sleep_time) ) time.sleep(sleep_time) sleep_time = min(sleep_time * 2.0, 30.0) * random.uniform(1.0, 1.7) except Exception as e: print("Ran into an unexpected exception: {}".format(e)) traceback.print_tb() raise
Ian's done a bit of that for vector tiles in his Tilepacks work here.