-
Notifications
You must be signed in to change notification settings - Fork 3
/
sitemap.py
89 lines (73 loc) · 2.56 KB
/
sitemap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Sitemap generation for the website."""
import time
from urllib.parse import quote
from db import ShipImageDatabase
db_manager = ShipImageDatabase()
def generate_sitemap():
"""
Generate the sitemap XML for the website including main page, authors, and tags.
"""
authors = db_manager.get_authors()["authors"]
tags = db_manager.get_tags()
current_time = time.strftime("%Y-%m-%dT%H:%M:%S+01:00", time.gmtime())
sitemap = ['<?xml version="1.0" encoding="UTF-8"?>']
sitemap.append('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">')
# Main page
sitemap.append("<url>")
sitemap.append("<loc>https://cosmo-lilac.vercel.app/</loc>")
sitemap.append(f"<lastmod>{current_time}</lastmod>")
sitemap.append("<priority>1.0</priority>")
sitemap.append("</url>")
# Authors
for author in authors:
author = quote(author[0])
sitemap.append("<url>")
sitemap.append(f"<loc>https://cosmo-lilac.vercel.app/search?author={author}</loc>")
sitemap.append(f"<lastmod>{current_time}</lastmod>")
sitemap.append("<priority>1.0</priority>")
sitemap.append("</url>")
# Tags
for tag in tags:
tag = quote(tag[0])
sitemap.append("<url>")
sitemap.append(f"<loc>https://cosmo-lilac.vercel.app/search?{tag}=1</loc>")
sitemap.append(f"<lastmod>{current_time}</lastmod>")
sitemap.append("<priority>1.0</priority>")
sitemap.append("</url>")
sitemap.append("</urlset>")
return "\n".join(sitemap)
def generate_url_tags():
"""Generate a tag for html content to push to seo_tags page."""
urllist = []
tags = db_manager.get_tags()
for tag in tags:
taguri = quote(tag[0])
linetag = (
"<h3><a href="
+ f"https://cosmo-lilac.vercel.app/search?{taguri}=1"
+ ">"
+ tag[0]
+ "</a></h3>"
)
urllist.append(linetag)
return "\n".join(urllist)
def generate_url_authors():
"""
Generates a list of HTML links for each author in the database.
Returns:
str: A string containing HTML links for each author, separated by newlines.
"""
urllist = []
authors = db_manager.get_authors()["authors"]
for author in authors:
authoruri = quote(author[0])
line = (
"<h3><a href="
+ f"https://cosmo-lilac.vercel.app/search?author={authoruri}"
+ ">"
+ author[0]
+ "</a></h3>"
)
# print(line)
urllist.append(line)
return "\n".join(urllist)