-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenSitemap.js
129 lines (114 loc) · 3.08 KB
/
genSitemap.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import fs from 'fs';
import { stationNames } from './src/data/stations.js';
import { trainNames } from './src/data/trains.js';
const sitemap = fs.createWriteStream('./public/sitemap.xml');
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
(async () => {
const trainsRes = await fetch('https://api.amtraker.com/v3/trains');
const stationsRes = await fetch('https://api.amtraker.com/v3/stations');
const trainsData = await trainsRes.json();
const stationsData = await stationsRes.json();
const newTrainsData = {
...trainNames,
...trainsData
};
const newStationsData = {
...stationNames,
...stationsData,
}
sitemap.write('<?xml version="1.0" encoding="UTF-8"?>');
sitemap.write('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
sitemap.write(`
<url>
<loc>https://amtraker.com/</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>never</changefreq>
<priority>1.0</priority>
</url>
`);
// train names
/*
[...new Set(Object.values(trainNames))].forEach((trainName) => {
sitemap.write(`
<url>
<loc>https://amtraker.com/trains/names/${trainName.replaceAll(' ', '%20')}</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
`);
});
*/
// train numbers
[...new Set(Object.keys(newTrainsData))].forEach((trainNum) => {
sitemap.write(`
<url>
<loc>https://amtraker.com/trains/${trainNum}</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
`);
});
// station codes
[...new Set(Object.keys(newStationsData))].forEach((station) => {
sitemap.write(`
<url>
<loc>https://amtraker.com/stations/${station}</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>hourly</changefreq>
<priority>0.7</priority>
</url>
`);
});
// train list
sitemap.write(`
<url>
<loc>https://amtraker.com/trains</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>hourly</changefreq>
<priority>0.6</priority>
</url>
`);
// station list
sitemap.write(`
<url>
<loc>https://amtraker.com/stations</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>never</changefreq>
<priority>0.5</priority>
</url>
`);
// map
sitemap.write(`
<url>
<loc>https://amtraker.com/map</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>never</changefreq>
<priority>0.4</priority>
</url>
`);
// about page
sitemap.write(`
<url>
<loc>https://amtraker.com/about</loc>
<lastmod>${year}-${month}-${day}</lastmod>
<changefreq>never</changefreq>
<priority>0.3</priority>
</url>
`);
sitemap.write('</urlset>');
sitemap.end();
})();
/*
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://www.example.com/foo.html</loc>
<lastmod>2022-06-04</lastmod>
</url>
</urlset>
*/