Skip to content

Commit

Permalink
Implement etag for hourly update
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Sep 18, 2024
1 parent 1e7a96f commit 5cacaf4
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,21 +951,31 @@ def download_small_kml(self, stationid) -> bytes | None:
except Exception as error:
print(f"Error in download_latest_kml: {type(error)} args: {error.args}")

def get_chunks(self):
def zipped_chunks():
def get_chunks(self, url):
def zipped_chunks(url):
# Iterable that yields the bytes of a zip file
with httpx.stream(
"GET",
"https://opendata.dwd.de/weather/local_forecasts/mos/MOSMIX_S/all_stations/kml/MOSMIX_S_LATEST_240.kmz",
url,
) as r:
self.etags[url] = r.headers["etag"] # type: ignore
yield from r.iter_bytes(chunk_size=171072)

return stream_unzip(zipped_chunks())
return stream_unzip(zipped_chunks(url))

def download_large_kml(self, stationid):
placemark = b""
url = "https://opendata.dwd.de/weather/local_forecasts/mos/MOSMIX_S/all_stations/kml/MOSMIX_S_LATEST_240.kmz"
# Check if content has updated
headers = {"If-None-Match": self.etags[url] if url in self.etags else ""} # type: ignore
r = httpx.head(
"https://opendata.dwd.de/weather/local_forecasts/mos/MOSMIX_S/all_stations/kml/MOSMIX_S_LATEST_240.kmz",
headers=headers,
)
if r.status_code == 304:
return

for file_name, file_size, unzipped_chunks in self.get_chunks():
for file_name, file_size, unzipped_chunks in self.get_chunks(url):
chunk1 = b""
chunk2 = b""
first_chunk = None
Expand Down

0 comments on commit 5cacaf4

Please sign in to comment.