mirror of https://github.com/Meekdai/Gmeek.git
feat: 添加站点地图生成功能
This commit is contained in:
parent
77a8aaa2b1
commit
fdd4f8be7d
40
Gmeek.py
40
Gmeek.py
|
|
@ -308,6 +308,44 @@ class GMEEK():
|
|||
feedFile.close()
|
||||
return
|
||||
|
||||
def createSiteMapXml(self):
|
||||
baseUrl=self.blogBase["homeUrl"].rstrip("/")
|
||||
nowDate=datetime.datetime.now(self.TZ).strftime("%Y-%m-%d")
|
||||
seen=set()
|
||||
entries=[]
|
||||
|
||||
def addUrl(path,lastmod):
|
||||
fullUrl=baseUrl+"/"+path.lstrip("/")
|
||||
if fullUrl not in seen:
|
||||
seen.add(fullUrl)
|
||||
entries.append((fullUrl,lastmod))
|
||||
|
||||
addUrl("index.html",nowDate)
|
||||
addUrl("tag.html",nowDate)
|
||||
|
||||
postTotal=len(self.blogBase["postListJson"])
|
||||
pageTotal=max(1,(postTotal+self.blogBase["onePageListNum"]-1)//self.blogBase["onePageListNum"])
|
||||
for pageNum in range(2,pageTotal+1):
|
||||
addUrl("page%d.html" % pageNum,nowDate)
|
||||
|
||||
for post in self.blogBase["singeListJson"].values():
|
||||
postDate=datetime.datetime.fromtimestamp(post["createdAt"],tz=self.TZ).strftime("%Y-%m-%d")
|
||||
addUrl(post["postUrl"],postDate)
|
||||
|
||||
for post in self.blogBase["postListJson"].values():
|
||||
postDate=datetime.datetime.fromtimestamp(post["createdAt"],tz=self.TZ).strftime("%Y-%m-%d")
|
||||
addUrl(post["postUrl"],postDate)
|
||||
|
||||
siteMapLine=['<?xml version="1.0" encoding="UTF-8"?>','<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">']
|
||||
for loc,lastmod in entries:
|
||||
siteMapLine.append("<url><loc>%s</loc><lastmod>%s</lastmod></url>" % (html.escape(loc),lastmod))
|
||||
siteMapLine.append("</urlset>")
|
||||
|
||||
print("====== create sitemap xml ======")
|
||||
siteMapFile=open(self.root_dir+"sitemap.xml","w",encoding="utf-8")
|
||||
siteMapFile.write("\n".join(siteMapLine))
|
||||
siteMapFile.close()
|
||||
|
||||
print("====== create rss xml ======")
|
||||
feed.rss_file(self.root_dir+'rss.xml')
|
||||
|
||||
|
|
@ -417,6 +455,7 @@ class GMEEK():
|
|||
|
||||
self.createPlistHtml()
|
||||
self.createFeedXml()
|
||||
self.createSiteMapXml()
|
||||
print("====== create static html end ======")
|
||||
|
||||
def runOne(self,number_str):
|
||||
|
|
@ -427,6 +466,7 @@ class GMEEK():
|
|||
self.createPostHtml(self.blogBase[listJsonName]["P"+number_str])
|
||||
self.createPlistHtml()
|
||||
self.createFeedXml()
|
||||
self.createSiteMapXml()
|
||||
print("====== create static html end ======")
|
||||
else:
|
||||
print("====== issue is closed ======")
|
||||
|
|
|
|||
Loading…
Reference in New Issue