-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_server.py
More file actions
66 lines (57 loc) · 2.86 KB
/
Copy pathproxy_server.py
File metadata and controls
66 lines (57 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""
Atlas Nexus — Markets API Proxy
Serves Yahoo Finance data to browser dashboards via CORS
Usage: python3 proxy_server.py [--port 8765]
"""
import json, urllib.request, http.server, os, sys
from urllib.parse import urlparse, parse_qs
PORT = int(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[1] == "--port" else 8765
class Proxy(http.server.HTTPServer):
allow_reuse_address = True
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
path = urlparse(self.path).path
qs = parse_qs(urlparse(self.path).query)
# CORS
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cache-Control", "max-age=60")
self.end_headers()
try:
if path.startswith("/chart/"):
symbol = path.split("/chart/")[1]
url = f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range=5d&interval=1d"
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(req, timeout=10) as resp:
data = json.loads(resp.read())
result = data.get("chart", {}).get("result", [{}])[0]
meta = result.get("meta", {})
quotes = result.get("indicators", {}).get("quote", [{}])[0]
clean = [p for p in quotes.get("close", []) if p is not None]
payload = {
"price": meta.get("regularMarketPrice"),
"change": meta.get("regularMarketPrice", 0) - meta.get("previousClose", meta.get("regularMarketPrice", 0)),
"changePct": (meta.get("regularMarketPrice", 0) - meta.get("previousClose", meta.get("regularMarketPrice", 0))) / meta.get("previousClose", 1) * 100,
"high": meta.get("regularMarketDayHigh"),
"low": meta.get("regularMarketDayLow"),
"prevClose": meta.get("previousClose"),
"history": clean[-5:] if clean else []
}
self.wfile.write(json.dumps(payload).encode())
elif path == "/health":
self.wfile.write(json.dumps({"status":"ok"}).encode())
else:
self.wfile.write(json.dumps({"error":"unknown endpoint"}).encode())
except Exception as e:
self.wfile.write(json.dumps({"error": str(e)}).encode())
def log_message(self, format, *args):
pass # Silence logs
print(f"🔮 Atlas Nexus API Proxy → http://localhost:{PORT}")
print(f" Endpoints: /chart/SYMBOL")
httpd = Proxy(("", PORT), Handler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n👋 Done")