From 8670e99b5e90321be47987eafe24b4c06aed2096 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Fri, 27 Sep 2024 16:05:40 +1000 Subject: [PATCH] mp_tile.py: use If-Modified-Since to try to reuse tile data --- MAVProxy/modules/mavproxy_map/mp_tile.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/MAVProxy/modules/mavproxy_map/mp_tile.py b/MAVProxy/modules/mavproxy_map/mp_tile.py index 953de323fb..afe0bddf00 100755 --- a/MAVProxy/modules/mavproxy_map/mp_tile.py +++ b/MAVProxy/modules/mavproxy_map/mp_tile.py @@ -24,6 +24,7 @@ import math import threading import os +import pathlib import string import time import cv2 @@ -284,11 +285,28 @@ def downloader(self): print("Downloading %s [%u left]" % (url, len(keys))) req = url_request(url) req.add_header('User-Agent', 'MAVProxy') + + # try to re-use our cached data: + try: + mtime = os.path.getmtime(path) + req.add_header('If-Modified-Since', time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(mtime))) + except Exception: + pass + if url.find('google') != -1: req.add_header('Referer', 'https://maps.google.com/') resp = url_open(req) headers = resp.info() except url_error as e: + try: + if e.getcode() == 304: + # cache hit; touch the file to reset its refresh time + pathlib.Path(path).touch() + self._download_pending.pop(key) + continue + except Exception as ex: + pass + #print('Error loading %s' % url) if not key in self._tile_cache: self._tile_cache[key] = self._unavailable