Files
digital-home-dashboard/weather-proxy.py

32 lines
1.3 KiB
Python

#!/usr/bin/env python3
from http.server import HTTPServer, BaseHTTPRequestHandler
import subprocess, json
class Handler(BaseHTTPRequestHandler):
def log_message(self, *a): pass
def do_GET(self):
try:
cmd = ["curl", "-s", "--max-time", "5", "https://wttr.in/Saint+Petersburg?format=j1"]
result = subprocess.run(cmd, timeout=8, capture_output=True, text=True)
if result.stdout and len(result.stdout) > 10:
body = result.stdout.encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", len(body))
self.end_headers()
self.wfile.write(body)
else:
raise Exception("No data rc=" + str(result.returncode))
except Exception as e:
err = json.dumps({"error": str(e)}).encode()
self.send_response(502)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", len(err))
self.end_headers()
self.wfile.write(err)
if __name__ == "__main__":
server = HTTPServer(("0.0.0.0", 8765), Handler)
print("Weather proxy on :8765", flush=True)
server.serve_forever()