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 blueprints import sol
|
||||||
from dateutil import parser as date_parser
|
from dateutil import parser as date_parser
|
||||||
from blueprints.spotify import get_playing_spotify_track
|
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
|
# Constants
|
||||||
HTTP_OK = 200
|
HTTP_OK = 200
|
||||||
@@ -22,6 +23,8 @@ app = Blueprint("api", __name__, url_prefix="/api/v1")
|
|||||||
# Register solana blueprint
|
# Register solana blueprint
|
||||||
app.register_blueprint(sol.app)
|
app.register_blueprint(sol.app)
|
||||||
|
|
||||||
|
tz = ZoneInfo(os.getenv("TIMEZONE", "Australia/Sydney"))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", strict_slashes=False)
|
@app.route("/", strict_slashes=False)
|
||||||
@app.route("/help")
|
@app.route("/help")
|
||||||
@@ -33,7 +36,6 @@ def help():
|
|||||||
"endpoints": {
|
"endpoints": {
|
||||||
"/time": "Get the current time",
|
"/time": "Get the current time",
|
||||||
"/timezone": "Get the current timezone",
|
"/timezone": "Get the current timezone",
|
||||||
"/message": "Get the message from the config",
|
|
||||||
"/project": "Get the current project from git",
|
"/project": "Get the current project from git",
|
||||||
"/version": "Get the current version of the website",
|
"/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)",
|
"/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")
|
@app.route("/time")
|
||||||
def time():
|
def time():
|
||||||
"""Get the current time in the configured timezone."""
|
"""Get the current time in the configured timezone."""
|
||||||
nc_config = get_nc_config()
|
|
||||||
timezone_offset = datetime.timedelta(hours=nc_config["time-zone"])
|
current_time = datetime.datetime.now(tz)
|
||||||
timezone = datetime.timezone(offset=timezone_offset)
|
|
||||||
current_time = datetime.datetime.now(tz=timezone)
|
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"timestring": current_time.strftime("%A, %B %d, %Y %I:%M %p"),
|
"timestring": current_time.strftime("%A, %B %d, %Y %I:%M %p"),
|
||||||
"timestamp": current_time.timestamp(),
|
"timestamp": current_time.timestamp(),
|
||||||
"timezone": nc_config["time-zone"],
|
"timezone": tz.tzname(current_time),
|
||||||
"timeISO": current_time.isoformat(),
|
"timeISO": current_time.isoformat(),
|
||||||
"ip": getClientIP(request),
|
"ip": getClientIP(request),
|
||||||
"status": HTTP_OK,
|
"status": HTTP_OK,
|
||||||
@@ -87,12 +87,11 @@ def time():
|
|||||||
@app.route("/timezone")
|
@app.route("/timezone")
|
||||||
def timezone():
|
def timezone():
|
||||||
"""Get the current timezone setting."""
|
"""Get the current timezone setting."""
|
||||||
nc_config = get_nc_config()
|
current_time = datetime.datetime.now(tz)
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"timezone": nc_config["time-zone"],
|
"timezone": tz.utcoffset(current_time).total_seconds() / 3600,
|
||||||
"server_timezone_name": datetime.datetime.now(datetime.timezone.utc).astimezone().tzname(),
|
"timezone_name": tz.tzname(current_time),
|
||||||
"server_timezone_offset": datetime.datetime.now(datetime.timezone.utc).astimezone().utcoffset().total_seconds() / 3600,
|
|
||||||
"ip": getClientIP(request),
|
"ip": getClientIP(request),
|
||||||
"status": HTTP_OK,
|
"status": HTTP_OK,
|
||||||
}
|
}
|
||||||
@@ -101,10 +100,15 @@ def timezone():
|
|||||||
|
|
||||||
@app.route("/message")
|
@app.route("/message")
|
||||||
def message():
|
def message():
|
||||||
"""Get the message from the configuration."""
|
"""Deprecated: Get the message from the config."""
|
||||||
nc_config = get_nc_config()
|
|
||||||
return jsonify(
|
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
|
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
|
# Cache storage for git data
|
||||||
_git_data_cache = {"data": None, "timestamp": 0}
|
_git_data_cache = {"data": None, "timestamp": 0}
|
||||||
_git_data_ttl = 300 # 5 minutes cache
|
_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 qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_H
|
||||||
from ansi2html import Ansi2HTMLConverter
|
from ansi2html import Ansi2HTMLConverter
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
# Import blueprints
|
# Import blueprints
|
||||||
from blueprints import now, blog, wellknown, api, podcast, acme, spotify
|
from blueprints import now, blog, wellknown, api, podcast, acme, spotify
|
||||||
@@ -34,7 +35,6 @@ from tools import (
|
|||||||
)
|
)
|
||||||
from curl import curl_response
|
from curl import curl_response
|
||||||
from cache_helper import (
|
from cache_helper import (
|
||||||
get_nc_config,
|
|
||||||
get_git_latest_activity,
|
get_git_latest_activity,
|
||||||
get_projects,
|
get_projects,
|
||||||
get_uptime_status,
|
get_uptime_status,
|
||||||
@@ -78,6 +78,8 @@ if os.path.isfile("data/sites.json"):
|
|||||||
# Remove any sites that are not enabled
|
# Remove any sites that are not enabled
|
||||||
SITES = [site for site in SITES if "enabled" not in site or site["enabled"]]
|
SITES = [site for site in SITES if "enabled" not in site or site["enabled"]]
|
||||||
|
|
||||||
|
TZ = ZoneInfo(os.getenv("TIMEZONE", "Australia/Sydney"))
|
||||||
|
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
# region Assets routes
|
# region Assets routes
|
||||||
@@ -280,13 +282,8 @@ def index():
|
|||||||
html_url = git["repo"]["html_url"]
|
html_url = git["repo"]["html_url"]
|
||||||
repo = '<a href="' + html_url + '" target="_blank">' + repo_name + "</a>"
|
repo = '<a href="' + html_url + '" target="_blank">' + repo_name + "</a>"
|
||||||
|
|
||||||
# Get time using cached config
|
timezone_offset = TZ.utcoffset(datetime.datetime.now()).total_seconds() / 3600
|
||||||
nc_config = get_nc_config()
|
time = datetime.datetime.now().strftime("%B %d")
|
||||||
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")
|
|
||||||
time += """
|
time += """
|
||||||
<span id=\"time\"></span>
|
<span id=\"time\"></span>
|
||||||
<script>
|
<script>
|
||||||
@@ -305,7 +302,7 @@ def index():
|
|||||||
setInterval(updateClock, 1000);
|
setInterval(updateClock, 1000);
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
time += f"startClock({nc_config['time-zone']});"
|
time += f"startClock({timezone_offset});"
|
||||||
time += "</script>"
|
time += "</script>"
|
||||||
|
|
||||||
HNSaddress = getAddress("HNS")
|
HNSaddress = getAddress("HNS")
|
||||||
@@ -327,7 +324,7 @@ def index():
|
|||||||
sites=SITES,
|
sites=SITES,
|
||||||
projects=projects,
|
projects=projects,
|
||||||
time=time,
|
time=time,
|
||||||
message=nc_config.get("message", ""),
|
message="",
|
||||||
),
|
),
|
||||||
200,
|
200,
|
||||||
{"Content-Type": "text/html"},
|
{"Content-Type": "text/html"},
|
||||||
|
|||||||
Reference in New Issue
Block a user