-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerate_sitemap.py
51 lines (45 loc) · 1.94 KB
/
generate_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
import datetime
import os
url = 'https://studeyang.tech/technotes/#'
exclude_files = [
'coverpage', 'navbar', 'README', 'sidebar',
'A类/README', 'A类/Python/README', 'A类/Python/sidebar',
'B类/README', 'B类/sidebar',
'C类/README', 'C类/sidebar',
'D类/README', 'D类/sidebar', 'D类/D04-职业规划/20180808知识地图认定--整理', 'D类/D04-职业规划/202009月知识梳理计划', 'D类/D04-职业规划/202010月面试计划',
'D类/D07-anki/知识转Anki进度', 'D类/D08-杂谈/一些idea', 'D类/D08-杂谈/面试题',
'资源/学习资源'
]
def create_sitemap():
xml = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
txt = ''
for path, dirs, files in os.walk("./"):
for file in files:
if not file.endswith('.md'):
continue
try:
if not path.endswith('/'):
path += '/'
new_path = (path.replace('\\', '/') + file)[2:-3]
if new_path in exclude_files:
continue
print(new_path)
txt += f'{url}/{new_path}\n'
xml += ' <url>\n'
xml += f' <loc>{url}/{new_path}</loc>\n'
lastmod = datetime.datetime.utcfromtimestamp(os.path.getmtime(path + file)).strftime('%Y-%m-%d')
xml += f' <lastmod>{lastmod}</lastmod>\n'
xml += ' <changefreq>monthly</changefreq>\n'
xml += ' <priority>0.5</priority>\n'
xml += ' </url>\n'
except Exception as e:
print(path, file, e)
break
xml += f'</urlset>\n'
with open('./sitemap.xml', 'w', encoding='utf-8') as sitemap:
sitemap.write(xml)
with open('./sitemap.txt', 'w', encoding='utf-8') as sitemap:
sitemap.write(txt)
if __name__ == '__main__':
create_sitemap()