Replace json timezone with server timezone #8
@@ -8,7 +8,8 @@ from tools import getClientIP, getGitCommit, json_response, parse_date, get_tool
|
||||
from blueprints import sol
|
||||
from dateutil import parser as date_parser
|
||||
from blueprints.spotify import get_playing_spotify_track
|
||||
from cache_helper import get_nc_config, get_git_latest_activity
|
||||
from cache_helper import get_git_latest_activity
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
# Constants
|
||||
HTTP_OK = 200
|
||||
@@ -22,6 +23,8 @@ app = Blueprint("api", __name__, url_prefix="/api/v1")
|
||||
# Register solana blueprint
|
||||
app.register_blueprint(sol.app)
|
||||
|
||||
tz = ZoneInfo(os.getenv("TIMEZONE", "Australia/Sydney"))
|
||||
|
||||
|
||||
@app.route("/", strict_slashes=False)
|
||||
@app.route("/help")
|
||||
@@ -33,7 +36,6 @@ def help():
|
||||
"endpoints": {
|
||||
"/time": "Get the current time",
|
||||
"/timezone": "Get the current timezone",
|
||||
"/message": "Get the message from the config",
|
||||
"/project": "Get the current project from git",
|
||||
"/version": "Get the current version of the website",
|
||||
"/page_date?url=URL&verbose=BOOL": "Get the last modified date of a webpage (verbose is optional, default false)",
|
||||
@@ -68,15 +70,13 @@ def version():
|
||||
@app.route("/time")
|
||||
def time():
|
||||
"""Get the current time in the configured timezone."""
|
||||
nc_config = get_nc_config()
|
||||
timezone_offset = datetime.timedelta(hours=nc_config["time-zone"])
|
||||
timezone = datetime.timezone(offset=timezone_offset)
|
||||
current_time = datetime.datetime.now(tz=timezone)
|
||||
|
||||
current_time = datetime.datetime.now(tz)
|
||||
return jsonify(
|
||||
{
|
||||
"timestring": current_time.strftime("%A, %B %d, %Y %I:%M %p"),
|
||||
"timestamp": current_time.timestamp(),
|
||||
"timezone": nc_config["time-zone"],
|
||||
"timezone": tz.tzname(current_time),
|
||||
"timeISO": current_time.isoformat(),
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
@@ -87,10 +87,11 @@ def time():
|
||||
@app.route("/timezone")
|
||||
def timezone():
|
||||
"""Get the current timezone setting."""
|
||||
nc_config = get_nc_config()
|
||||
current_time = datetime.datetime.now(tz)
|
||||
return jsonify(
|
||||
{
|
||||
"timezone": nc_config["time-zone"],
|
||||
"timezone": tz.utcoffset(current_time).total_seconds() / 3600,
|
||||
"timezone_name": tz.tzname(current_time),
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
}
|
||||
@@ -99,10 +100,15 @@ def timezone():
|
||||
|
||||
@app.route("/message")
|
||||
def message():
|
||||
"""Get the message from the configuration."""
|
||||
nc_config = get_nc_config()
|
||||
"""Deprecated: Get the message from the config."""
|
||||
return jsonify(
|
||||
{"message": nc_config["message"], "ip": getClientIP(request), "status": HTTP_OK}
|
||||
{
|
||||
"message": "",
|
||||
"ip": getClientIP(request),
|
||||
"status": HTTP_OK,
|
||||
"deprecated": True,
|
||||
"note": "The /message endpoint is deprecated and will be removed in a future version. It currently returns an empty message.",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -10,45 +10,6 @@ import requests
|
||||
from functools import lru_cache
|
||||
|
||||
|
||||
# Cache storage for NC_CONFIG with timestamp
|
||||
_nc_config_cache = {"data": None, "timestamp": 0}
|
||||
_nc_config_ttl = 3600 # 1 hour cache
|
||||
|
||||
|
||||
def get_nc_config():
|
||||
"""
|
||||
Get NC_CONFIG with caching (1 hour TTL).
|
||||
Falls back to default config on error.
|
||||
|
||||
Returns:
|
||||
dict: Configuration dictionary
|
||||
"""
|
||||
global _nc_config_cache
|
||||
current_time = datetime.datetime.now().timestamp()
|
||||
|
||||
# Check if cache is valid
|
||||
if (
|
||||
_nc_config_cache["data"]
|
||||
and (current_time - _nc_config_cache["timestamp"]) < _nc_config_ttl
|
||||
):
|
||||
return _nc_config_cache["data"]
|
||||
|
||||
# Fetch new config
|
||||
try:
|
||||
config = requests.get(
|
||||
"https://cloud.woodburn.au/s/4ToXgFe3TnnFcN7/download/website-conf.json",
|
||||
timeout=5,
|
||||
).json()
|
||||
_nc_config_cache = {"data": config, "timestamp": current_time}
|
||||
return config
|
||||
except Exception as e:
|
||||
print(f"Error fetching NC_CONFIG: {e}")
|
||||
# Return cached data if available, otherwise default
|
||||
if _nc_config_cache["data"]:
|
||||
return _nc_config_cache["data"]
|
||||
return {"time-zone": 10, "message": ""}
|
||||
|
||||
|
||||
# Cache storage for git data
|
||||
_git_data_cache = {"data": None, "timestamp": 0}
|
||||
_git_data_ttl = 300 # 5 minutes cache
|
||||
|
||||
17
server.py
17
server.py
@@ -18,6 +18,7 @@ import qrcode
|
||||
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_H
|
||||
from ansi2html import Ansi2HTMLConverter
|
||||
from PIL import Image
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
# Import blueprints
|
||||
from blueprints import now, blog, wellknown, api, podcast, acme, spotify
|
||||
@@ -34,7 +35,6 @@ from tools import (
|
||||
)
|
||||
from curl import curl_response
|
||||
from cache_helper import (
|
||||
get_nc_config,
|
||||
get_git_latest_activity,
|
||||
get_projects,
|
||||
get_uptime_status,
|
||||
@@ -78,6 +78,8 @@ if os.path.isfile("data/sites.json"):
|
||||
# Remove any sites that are not enabled
|
||||
SITES = [site for site in SITES if "enabled" not in site or site["enabled"]]
|
||||
|
||||
TZ = ZoneInfo(os.getenv("TIMEZONE", "Australia/Sydney"))
|
||||
|
||||
# endregion
|
||||
|
||||
# region Assets routes
|
||||
@@ -280,13 +282,8 @@ def index():
|
||||
html_url = git["repo"]["html_url"]
|
||||
repo = '<a href="' + html_url + '" target="_blank">' + repo_name + "</a>"
|
||||
|
||||
# Get time using cached config
|
||||
nc_config = get_nc_config()
|
||||
timezone_offset = datetime.timedelta(hours=nc_config["time-zone"])
|
||||
timezone = datetime.timezone(offset=timezone_offset)
|
||||
time = datetime.datetime.now(tz=timezone)
|
||||
|
||||
time = time.strftime("%B %d")
|
||||
timezone_offset = TZ.utcoffset(datetime.datetime.now()).total_seconds() / 3600
|
||||
time = datetime.datetime.now().strftime("%B %d")
|
||||
time += """
|
||||
<span id=\"time\"></span>
|
||||
<script>
|
||||
@@ -305,7 +302,7 @@ def index():
|
||||
setInterval(updateClock, 1000);
|
||||
}
|
||||
"""
|
||||
time += f"startClock({nc_config['time-zone']});"
|
||||
time += f"startClock({timezone_offset});"
|
||||
time += "</script>"
|
||||
|
||||
HNSaddress = getAddress("HNS")
|
||||
@@ -327,7 +324,7 @@ def index():
|
||||
sites=SITES,
|
||||
projects=projects,
|
||||
time=time,
|
||||
message=nc_config.get("message", ""),
|
||||
message="",
|
||||
),
|
||||
200,
|
||||
{"Content-Type": "text/html"},
|
||||
|
||||
Reference in New Issue
Block a user