From 5546db3f857c0583616cc8cb28f8a3ef25a0a482 Mon Sep 17 00:00:00 2001 From: Aryan Kothari <87589047+thearyadev@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:35:47 -0500 Subject: [PATCH] 84 dynamic sitemapxml (#85) * add dynamic sitemap generator * delete existing sitemap.xml --- server.py | 20 +++++++++++++++++++- static/sitemap.xml | 19 ------------------- 2 files changed, 19 insertions(+), 20 deletions(-) delete mode 100644 static/sitemap.xml diff --git a/server.py b/server.py index 1ab28033..5aae7bee 100644 --- a/server.py +++ b/server.py @@ -5,7 +5,7 @@ from dotenv import load_dotenv from fastapi import Depends, FastAPI, Request -from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse +from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse, Response from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates @@ -27,6 +27,7 @@ import csv import io import zipfile +import xml.etree.ElementTree as ET load_dotenv() templates = Jinja2Templates(directory="templates") @@ -43,6 +44,20 @@ ) +@lru_cache +def get_sitemap() -> str: + urls: list[str] = [ + f"https://t500-aggregator.aryankothari.dev/season/{season}" for season in + db.get_seasons()] + urls.append("https://t500-aggregator.aryankothari.dev/trends/seasonal") + urlset = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9") + for url in urls: + url_element = ET.SubElement(urlset, "url") + loc = ET.SubElement(url_element, "loc") + loc.text = url + return ET.tostring(urlset, encoding="utf8", method="xml").decode() + + @lru_cache def seasons_list() -> list[str]: """ @@ -445,6 +460,9 @@ async def index_redirect( if "favicon-32x32.png" in str(request.url): return FileResponse("static/favicon-32x32.png") + if "sitemap.xml" in str(request.url): + return Response(content=get_sitemap(), media_type="application/xml") + if "favicon-16x16.png" in str(request.url): return FileResponse("static/favicon-16x16.png") diff --git a/static/sitemap.xml b/static/sitemap.xml deleted file mode 100644 index cc88de44..00000000 --- a/static/sitemap.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - https://t500-aggregator.aryankothari.dev/season/4_2 - 2023-04-28T20:23:48+00:00 - 1.00 - - - https://t500-aggregator.aryankothari.dev/trends/seasonal - 2023-04-28T20:23:48+00:00 - 0.80 - - - https://t500-aggregator.aryankothari.dev/season/4_1 - 2023-04-28T20:23:48+00:00 - 0.80 - - \ No newline at end of file