-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsitemap.js
More file actions
194 lines (146 loc) · 5.84 KB
/
Copy pathsitemap.js
File metadata and controls
194 lines (146 loc) · 5.84 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const fs = require('node:fs');
const pLimit = require('p-limit');
require( 'dotenv' ).config();
const upload = require( './r2' );
const limit = pLimit(5);
const POSTS_PER_REQUEST = 1000;
if ( !process.env.API_TOKEN ) {
throw new Error( 'Unable to load api key' );
}
if ( !process.env.AWS_ACCESS_KEY || !process.env.AWS_SECRET_KEY ) {
throw new Error( 'AWS auth not configured' );
}
const API_HOST = 'api.developertracker.com';
const sitemapTemplate = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">`;
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
}
return array;
};
const addPath = (sitemap, url, changefreq = 'hourly') => {
return `${sitemap}
<url>
<loc>https://www.developertracker.com${url}</loc>
<changefreq>${changefreq}</changefreq>
</url>`;
};
const uploadXML = async (filename) => {
try {
await upload( filename, fs.readFileSync(`./sitemap/${filename}`, 'utf-8'), 'application/xml' );
console.log( `Successfully uploaded sitemap ${filename}` );
} catch ( uploadError ) {
console.error( uploadError );
}
}
const getGames = async function getGames() {
let allGamesConfig;
try {
const gamesConfigResponse = await fetch( `https://${ API_HOST }/games`, {
Authorization: `Bearer ${ process.env.API_TOKEN }`,
} );
allGamesConfig = await gamesConfigResponse.json();
} catch ( getGamesError ) {
console.log( `Unable to load games. Got "${ getGamesError.message }"` );
throw getGamesError;
}
return allGamesConfig.data;
};
const buildSitemap = async function buildSitemap(sitemap, gameIdentifier, offset = 0){
console.log(`Loading ${POSTS_PER_REQUEST} posts for ${gameIdentifier} with offset ${offset}`);
let posts;
try {
const postsDataResponse = await fetch( `https://${API_HOST}/${ gameIdentifier }/posts?limit=${POSTS_PER_REQUEST}&offset=${offset}` );
posts = await postsDataResponse.json();
} catch ( parseFail ) {
console.error( `Failed to load posts for ${ gameIdentifier } at offset ${ offset }` );
// Keep whatever we've collected rather than failing the whole run.
return sitemap;
}
// The API caps offset (MAX_POST_OFFSET) and answers an over-cap request with
// an error object, not a list. fetch() doesn't throw on a 4xx, so guard the
// shape here: a non-array means "no more pages" — stop recursing instead of
// crashing on posts.data being undefined (which used to fail the entire run
// for any game larger than the offset cap).
if ( !posts || !Array.isArray( posts.data ) ) {
return sitemap;
}
for ( const post of posts.data ) {
sitemap = addPath(sitemap, `/${ gameIdentifier }/?post=${post.id}`, 'never');
}
if(posts.data.length >= POSTS_PER_REQUEST){
sitemap = await buildSitemap(sitemap, gameIdentifier, offset + POSTS_PER_REQUEST);
}
return sitemap;
};
const buildAllSitemaps = async function buildAllSitemaps() {
// State is local to each run so repeated (scheduled) invocations don't
// accumulate the index or the counters across runs.
let finished = 0;
let failed = 0;
const indexedGames = [];
if ( !fs.existsSync( 'sitemap' ) ) {
fs.mkdirSync( 'sitemap' );
}
fs.readdirSync('sitemap').forEach(f => {
if(f === '.gitignore'){
return true;
}
fs.rmSync(`sitemap/${f}`);
});
console.time(`build-sitemaps`);
let games = await getGames();
let allGameSitemaps = [];
games = shuffle(games);
for ( const game of games ) {
indexedGames.push(game.identifier);
const sitemapPromise = limit(() => {
console.time(`build-sitemap-${game.identifier}`);
let gameSitemap = addPath(sitemapTemplate, `/${ game.identifier }/`);
return buildSitemap(gameSitemap, game.identifier)
.then((sitemap) => {
console.timeEnd(`build-sitemap-${game.identifier}`);
if(!sitemap){
console.log(`Failed to build complete sitemap for ${game.identifier}`);
failed = failed + 1;
return false;
}
sitemap = `${sitemap}
</urlset>`;
fs.writeFileSync(`sitemap/sitemap.${game.identifier}.xml`, sitemap);
finished = finished + 1;
return uploadXML(`sitemap.${game.identifier}.xml`);
});
});
allGameSitemaps.push(sitemapPromise);
}
console.log('Waiting for all sitemaps to finish');
await Promise.all(allGameSitemaps);
const sitemapIndex = `<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${indexedGames.map((identifier) => {
return `
<sitemap>
<loc>https://www.developertracker.com/sitemap.${identifier}.xml</loc>
</sitemap>`;
}).join('')}
</sitemapindex>`;
fs.writeFileSync(`sitemap/sitemap.xml`, sitemapIndex);
await uploadXML('sitemap.xml');
console.timeEnd(`build-sitemaps`);
console.log(`Done with ${finished}/${finished + failed} successfull sitemaps`);
};
module.exports = buildAllSitemaps;
// Allow running standalone: `node sitemap.js`
if ( require.main === module ) {
buildAllSitemaps().catch( ( sitemapError ) => {
console.error( sitemapError );
process.exit( 1 );
} );
}