diff --git a/setup.py b/setup.py index 16a889d..ad75204 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='staticmap', packages=['staticmap'], - version='0.5.4', + version='0.5.5', description='A small, python-based library for creating map images with lines and markers.', author='Christoph Lingg', author_email='christoph@komoot.de', diff --git a/staticmap/staticmap.py b/staticmap/staticmap.py index 15ed5a2..9a44ccc 100644 --- a/staticmap/staticmap.py +++ b/staticmap/staticmap.py @@ -7,6 +7,11 @@ import requests from PIL import Image, ImageDraw +try: + from cachecontrol import CacheControl +except: + CacheControl = None + class Line: def __init__(self, coords, color, width, simplify=True): @@ -215,6 +220,13 @@ def __init__(self, width, height, padding_x=0, padding_y=0, url_template="http:/ self.reverse_y = reverse_y self.background_color = background_color + # Optional cached session + if CacheControl is not None: + self.requests_sess = requests.session() + self.cached_sess = CacheControl(self.requests_sess) + else: + self.cached_sess = None + # features self.markers = [] self.lines = [] @@ -412,7 +424,8 @@ def _draw_base_layer(self, image): raise RuntimeError("could not download {} tiles: {}".format(len(tiles), tiles)) failed_tiles = [] - futures = [thread_pool.submit(requests.get, tile[2], timeout=self.request_timeout, headers=self.headers) for tile in tiles] + request_fn = requests.get if self.cached_sess is None else self.cached_sess.get + futures = [thread_pool.submit(request_fn, tile[2], timeout=self.request_timeout, headers=self.headers) for tile in tiles] for tile, future in zip(tiles, futures): x, y, url = tile