feat/flask: add sitemap.xml and robots.txt
This commit is contained in:
60
app.py
60
app.py
@@ -9,13 +9,20 @@ from flask import (
|
||||
request,
|
||||
send_from_directory,
|
||||
url_for,
|
||||
Response
|
||||
)
|
||||
|
||||
|
||||
# ---------- APP CONFIG ------------------------------------------------------
|
||||
|
||||
|
||||
cp = ConfigParser()
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
# ---------- LOCALES FUNCTIONS -----------------------------------------------
|
||||
|
||||
|
||||
def load_all_locales():
|
||||
locales_list = []
|
||||
locales_dict = {}
|
||||
@@ -49,6 +56,9 @@ def load_all_locales():
|
||||
locales_list, locales_dict, locales_code = load_all_locales()
|
||||
|
||||
|
||||
# ---------- HELPER FUNCTIONS ------------------------------------------------
|
||||
|
||||
|
||||
def get_best_lang():
|
||||
return request.accept_languages.best_match(locales_code) or "en"
|
||||
|
||||
@@ -67,6 +77,9 @@ def render_localized_template(lang, template_name):
|
||||
)
|
||||
|
||||
|
||||
# ---------- MAIN PAGES ------------------------------------------------------
|
||||
|
||||
|
||||
@app.route("/favicon.ico")
|
||||
def favicon():
|
||||
return send_from_directory(
|
||||
@@ -89,5 +102,52 @@ def download(lang):
|
||||
return render_localized_template(lang, "download.html")
|
||||
|
||||
|
||||
# ---------- ROBOTS.TXT + SITEMAP.XML ----------------------------------------
|
||||
|
||||
|
||||
@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("/")
|
||||
today = date.today().isoformat()
|
||||
|
||||
urls = []
|
||||
for lang in locales_code:
|
||||
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(
|
||||
[
|
||||
" <url>",
|
||||
f" <loc>{loc}</loc>",
|
||||
f" <lastmod>{today}</lastmod>",
|
||||
" <changefreq>monthly</changefreq>",
|
||||
" <priority>0.8</priority>",
|
||||
" </url>",
|
||||
]
|
||||
)
|
||||
xml_lines.append("</urlset>")
|
||||
|
||||
return Response("\n".join(xml_lines), mimetype="application/xml")
|
||||
|
||||
|
||||
# ---------- APP ENTRY -------------------------------------------------------
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", debug=True)
|
||||
|
Reference in New Issue
Block a user