108 lines
3.0 KiB
Python
108 lines
3.0 KiB
Python
import os
|
|
import hashlib
|
|
import json
|
|
import requests
|
|
import subprocess
|
|
from datetime import datetime, timedelta
|
|
from flask import Flask, jsonify, Response, request
|
|
|
|
BASE_API = "https://ws1.smn.gob.ar"
|
|
CACHE_DIR = "json.api.cache"
|
|
CACHE_TTL = timedelta(minutes=30)
|
|
SMN_TOKEN_FILE = "token.txt"
|
|
ACCESS_TOKEN = "i.hate.smn"
|
|
|
|
app = Flask(__name__)
|
|
|
|
def get_cache_filename(url: str) -> str:
|
|
h = hashlib.sha256(url.encode()).hexdigest()
|
|
return os.path.join(CACHE_DIR, f"{h}.json")
|
|
|
|
def load_cache(url: str):
|
|
path = get_cache_filename(url)
|
|
if not os.path.exists(path):
|
|
return None
|
|
mtime = datetime.fromtimestamp(os.path.getmtime(path))
|
|
if datetime.now() - mtime > CACHE_TTL:
|
|
return None
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
|
|
def save_cache(url: str, data: dict):
|
|
os.makedirs(CACHE_DIR, exist_ok=True)
|
|
path = get_cache_filename(url)
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
|
|
def load_smn_token():
|
|
with open(SMN_TOKEN_FILE, "r") as f:
|
|
return f.read().strip()
|
|
|
|
def refresh_smn_token():
|
|
print("[TOKEN] Refreshing SMN token...")
|
|
try:
|
|
result = subprocess.run(
|
|
["python3", "tokenext.py"], # cheap as fuck, fixing later
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=60
|
|
)
|
|
if result.returncode == 0:
|
|
print("[TOKEN] Token refreshed successfully.")
|
|
else:
|
|
print("[TOKEN] Failed to refresh token:", result.stderr)
|
|
except Exception as e:
|
|
print("[TOKEN] Error running tokenext.py:", e)
|
|
|
|
def check_access_token():
|
|
header_token = request.headers.get("Authorization", "").strip()
|
|
return header_token == ACCESS_TOKEN
|
|
|
|
def fetch_from_smn(url: str, retry: bool = True):
|
|
token = load_smn_token()
|
|
headers = {
|
|
"Authorization": f"JWT {token}",
|
|
"Accept": "application/json",
|
|
"User-Agent": "Mozilla/5.0"
|
|
}
|
|
|
|
resp = requests.get(url, headers=headers)
|
|
|
|
if resp.status_code == 401 and retry:
|
|
print("[AUTH] SMN token expired, trying to refresh...")
|
|
refresh_smn_token()
|
|
return fetch_from_smn(url, retry=False)
|
|
|
|
return resp
|
|
|
|
@app.route("/smn/<path:subpath>")
|
|
def smn_proxy(subpath):
|
|
if not check_access_token():
|
|
return jsonify({"error": "Unauthorized"}), 401
|
|
|
|
url = f"{BASE_API}/{subpath}"
|
|
|
|
# Check cache
|
|
cached = load_cache(url)
|
|
if cached:
|
|
print(f"[CACHE] Loaded {subpath}")
|
|
return jsonify(cached)
|
|
|
|
print(f"[FETCH] {url}")
|
|
resp = fetch_from_smn(url)
|
|
|
|
if resp.status_code != 200:
|
|
return Response(
|
|
resp.text,
|
|
status=resp.status_code,
|
|
content_type=resp.headers.get("Content-Type", "text/plain")
|
|
)
|
|
|
|
data = resp.json()
|
|
save_cache(url, data)
|
|
return jsonify(data)
|
|
|
|
if __name__ == "__main__":
|
|
os.makedirs(CACHE_DIR, exist_ok=True)
|
|
app.run(host="0.0.0.0", port=6942, debug=False)
|