forked from ensdomains/ens-app-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-site-map.mjs
113 lines (96 loc) · 3 KB
/
generate-site-map.mjs
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
import { createReadStream, createWriteStream, existsSync } from 'fs'
import { gql, request } from 'graphql-request'
import fetch from 'node-fetch'
import {
parseSitemap,
parseSitemapIndex,
SitemapAndIndexStream,
SitemapStream,
XMLToSitemapItemStream,
} from 'sitemap'
const baseURL = 'https://alpha.ens.domains'
const graphAPI = 'https://api.thegraph.com/subgraphs/name/ensdomains/ens'
const queryAll = gql`
query allNames($lastCreatedAt: String) {
domains(
first: 1000
where: { createdAt_gt: $lastCreatedAt, name_not_ends_with: ".addr.reverse", name_not: null }
orderBy: createdAt
orderDirection: asc
) {
createdAt
name
}
}
`
const querySpecific = gql`
query specificName($previousName: String) {
domains(first: 1, where: { name: $previousName }) {
createdAt
}
}
`
const getPreviousLast = async () => {
if (!existsSync('./out/sitemap.xml')) {
return '0'
}
const oldSiteMaps = await parseSitemapIndex(createReadStream('./out/sitemap.xml'))
const lastUrl = oldSiteMaps[oldSiteMaps.length - 1]
const lastSiteMap = await parseSitemap(
createReadStream(lastUrl.url.replace(`${baseURL}/`, './out/')),
)
const lastItem = lastSiteMap[lastSiteMap.length - 1]
const previousName = lastItem.url.replace(`${baseURL}/profile/`, '')
const { domains } = await request(graphAPI, querySpecific, { previousName })
const previousCreatedAt = domains[0].createdAt
return previousCreatedAt
}
const writeStream = createWriteStream('./out/sitemap.xml')
const main = async () => {
const sitemap = new SitemapAndIndexStream({
limit: 50000,
getSitemapStream: (i) => {
const sitemapStream = new SitemapStream({
hostname: baseURL,
})
const path = `/sitemap-${i}.xml`
return [
new URL(path, baseURL).toString(),
sitemapStream,
sitemapStream.pipe(createWriteStream(`./out/sitemaps/${path}`)),
]
},
})
sitemap.pipe(writeStream, {
end: false,
})
const oldSitemapIndexRes = await fetch(new URL('/sitemap.xml', baseURL).toString())
if (oldSitemapIndexRes.ok) {
const oldSitemapIndex = await parseSitemapIndex(oldSitemapIndexRes.body)
for (const sitemapIndex of oldSitemapIndex) {
const res = await fetch(sitemapIndex.url)
res.body.pipe(new XMLToSitemapItemStream()).pipe(sitemap, {
end: false,
})
await new Promise((resolve) => {
res.body.on('end', resolve)
})
}
}
let lastCreatedAt = oldSitemapIndexRes.ok ? await getPreviousLast() : '0'
do {
// eslint-disable-next-line no-await-in-loop
const { domains } = await request(graphAPI, queryAll, { lastCreatedAt })
console.log('FETCHED 1000 ITEMS')
if (domains.length === 0) {
break
}
lastCreatedAt = domains[domains.length - 1].createdAt
for (const domain of domains) {
sitemap.write({ url: `${baseURL}/profile/${domain.name}` })
}
// eslint-disable-next-line no-constant-condition
} while (true)
sitemap.end()
}
main()