Files

149 lines
3.7 KiB
Python
Raw Permalink Normal View History

import re
2025-12-25 10:32:22 +02:00
import datetime
2025-03-22 18:27:43 +03:00
from sass import compile as compile_sass
2025-12-25 10:32:22 +02:00
from flask import Flask, redirect, request, url_for, g, Response
2025-09-07 12:27:23 +03:00
2025-12-25 10:32:22 +02:00
from modules import autobuild, locales, helpers
2025-10-04 17:13:08 +03:00
2025-09-07 12:27:23 +03:00
2025-08-03 10:43:19 +03:00
# ---------- APP CONFIG ------------------------------------------------------
2025-03-22 18:27:43 +03:00
app = Flask(__name__)
2025-12-25 10:32:22 +02:00
locales.ensure_loaded()
# CSS Compilation and minification
2025-08-22 21:44:31 +03:00
if app.debug:
css = compile_sass(filename="static/style.scss", output_style="compressed")
with open("static/style.css", "w", encoding="utf-8") as f:
f.write(css)
2025-08-22 21:44:31 +03:00
# JS minification
with open("static/script.js", encoding="utf-8") as f:
js = f.read()
js = re.sub(r"/\*.*?\*/", "", js, flags=re.S)
js = re.sub(r"//.*", "", js)
js = re.sub(r"\s+", " ", js).strip()
with open("static/script.min.js", "w", encoding="utf-8") as f:
f.write(js)
2025-12-25 10:32:22 +02:00
@app.before_request
def _ensure_updater_started():
autobuild.ensure_started()
2025-09-07 12:27:23 +03:00
2025-10-04 17:13:08 +03:00
2025-09-07 12:27:23 +03:00
@app.before_request
2025-12-25 10:32:22 +02:00
def before_request():
if args := request.view_args:
g.locale = args.get("lang", "en")
g.translations = locales.translations.get(g.locale, helpers.get_best_lang())
g.locales_name = locales.locales_name
2025-09-07 12:27:23 +03:00
2025-10-04 17:13:08 +03:00
@app.context_processor
def _inject_autobuild_vers():
2026-04-04 17:01:36 +00:00
return {
"autobuild_vers": autobuild.autobuild_vers,
"autobuild_sizes": autobuild.autobuild_sizes,
}
2025-10-04 17:13:08 +03:00
2025-09-07 12:27:23 +03:00
@app.context_processor
def _inject_autobuild_date():
return {
2026-04-04 17:01:36 +00:00
"autobuild_date": autobuild.autobuild_date.strftime(
g.translations.get("downloads", {})
.get("date-format", "{DD}.{MM}.{YYYY}")
.replace("{YYYY}", "%Y")
.replace("{MM}", "%m")
.replace("{DD}", "%d")
)
}
2025-08-22 15:40:56 +03:00
@app.context_processor
def inject_translations():
def translate(text, **kwargs):
section, key = text.split(":", 1)
template = (
g.translations.get(section, {})
2025-08-22 15:40:56 +03:00
.get(key, f"${section}: {key}$")
)
2025-08-22 15:40:56 +03:00
try:
return template.format(**kwargs)
2025-09-07 12:27:23 +03:00
except Exception:
2025-08-22 15:40:56 +03:00
return template
return {"_": translate}
2025-08-22 15:40:56 +03:00
2025-12-25 10:32:22 +02:00
# ---------- ROUTES -------------------------------------------------------
2025-08-03 10:43:19 +03:00
2025-03-24 09:02:58 +02:00
@app.route("/")
def home():
2025-12-25 10:32:22 +02:00
return redirect(url_for("index", lang=helpers.get_best_lang()))
2025-03-24 09:02:58 +02:00
2025-05-24 11:41:35 +03:00
2025-12-31 23:05:25 +03:00
@app.route("/<lang>", strict_slashes=False)
2025-03-22 18:27:43 +03:00
def index(lang):
2025-12-25 10:32:22 +02:00
return helpers.render_localized_template(lang, "index.html")
2025-05-24 11:41:35 +03:00
2025-03-22 18:27:43 +03:00
2025-12-31 23:05:25 +03:00
@app.route("/<lang>/download", strict_slashes=False)
def download(lang):
2025-12-25 10:32:22 +02:00
return helpers.render_localized_template(lang, "download.html")
2025-08-03 10:43:19 +03:00
@app.route("/robots.txt")
def robots_txt():
base_url = request.url_root.rstrip("/")
content = [
"User-agent: *",
"Disallow:",
f"Sitemap: {base_url}/sitemap.xml",
]
return Response("\n".join(content), mimetype="text/plain")
@app.route("/sitemap.xml")
def sitemap_xml():
base_url = request.url_root.rstrip("/")
2025-12-25 10:32:22 +02:00
today = datetime.date.today().isoformat()
2025-08-03 10:43:19 +03:00
urls = []
2025-12-25 10:32:22 +02:00
for lang in locales.locales_code:
2025-08-03 10:43:19 +03:00
urls.append(f"{base_url}/{lang}")
urls.append(f"{base_url}/{lang}/download")
xml_lines = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
]
for loc in urls:
xml_lines.extend(
[
2025-12-25 10:32:22 +02:00
f" <url>",
f" <loc>{loc}</loc>",
f" <lastmod>{today}</lastmod>",
f" <changefreq>monthly</changefreq>",
f" <priority>0.8</priority>",
f" </url>",
2025-08-03 10:43:19 +03:00
]
)
xml_lines.append("</urlset>")
return Response("\n".join(xml_lines), mimetype="application/xml")
# ---------- APP ENTRY -------------------------------------------------------
2025-03-24 09:02:58 +02:00
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True)