Skip to content

Commit

Permalink
84 dynamic sitemapxml (#85)
Browse files Browse the repository at this point in the history
* add dynamic sitemap generator

* delete existing sitemap.xml
  • Loading branch information
thearyadev authored Dec 21, 2023
1 parent 703af39 commit 5546db3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
20 changes: 19 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,6 +27,7 @@
import csv
import io
import zipfile
import xml.etree.ElementTree as ET

load_dotenv()
templates = Jinja2Templates(directory="templates")
Expand All @@ -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]:
"""
Expand Down Expand Up @@ -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")

Expand Down
19 changes: 0 additions & 19 deletions static/sitemap.xml

This file was deleted.

0 comments on commit 5546db3

Please sign in to comment.