implement: cache
This commit is contained in:
parent
2be0fe6ec5
commit
cce24c469a
Binary file not shown.
|
@ -1,3 +1,5 @@
|
|||
from datetime import datetime
|
||||
from functools import cache
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
import json
|
||||
import urllib.request
|
||||
|
@ -11,19 +13,11 @@ def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
|
|||
|
||||
class WeatherHandler(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
return self.weather_handler()
|
||||
return self.data_output()
|
||||
|
||||
def weather_handler(self):
|
||||
coordinates = [48.712, 44.514]
|
||||
url = (
|
||||
"https://api.open-meteo.com/v1/forecast?latitude="
|
||||
+ str(coordinates[0])
|
||||
+ "&longitude="
|
||||
+ str(coordinates[1])
|
||||
+ "¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m"
|
||||
)
|
||||
raw_data = urllib.request.urlopen(url).read()
|
||||
print(raw_data)
|
||||
def data_output(self):
|
||||
date = datetime.today().strftime("%Y-%m-%d %H")
|
||||
raw_data = weather_handler(date)
|
||||
return self.render(200, raw_data)
|
||||
|
||||
def render(self, response_code: int, response_body: dict | None = None):
|
||||
|
@ -33,3 +27,19 @@ class WeatherHandler(BaseHTTPRequestHandler):
|
|||
self.end_headers()
|
||||
if response_body is not None:
|
||||
self.wfile.write(response_body)
|
||||
|
||||
|
||||
@cache
|
||||
def weather_handler(date: str):
|
||||
print(date)
|
||||
coordinates = [48.712, 44.514]
|
||||
url = (
|
||||
"https://api.open-meteo.com/v1/forecast?latitude="
|
||||
+ str(coordinates[0])
|
||||
+ "&longitude="
|
||||
+ str(coordinates[1])
|
||||
+ "¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m"
|
||||
)
|
||||
raw_data = urllib.request.urlopen(url).read()
|
||||
# print(raw_data)
|
||||
return raw_data
|
||||
|
|
Loading…
Reference in New Issue
Block a user