2023-10-05 22:19:58 +03:00
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const { parseStringPromise, Builder } = require('xml2js')
|
|
|
|
|
2023-10-07 03:08:46 +03:00
|
|
|
const weekMs = 7 * 24 * 60 * 60 * 1000
|
|
|
|
const nowISO = new Date().toISOString()
|
|
|
|
|
|
|
|
const getQuery = (chain) => `
|
|
|
|
query {
|
|
|
|
topTokens(pageSize: 100, page: 1, chain: ${chain}, orderBy: VOLUME) {
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
const chains = ['ETHEREUM', 'ARBITRUM', 'OPTIMISM', 'POLYGON', 'BASE', 'BNB', 'CELO']
|
|
|
|
|
2023-10-05 22:19:58 +03:00
|
|
|
fs.readFile('./public/sitemap.xml', 'utf8', async (err, data) => {
|
2023-10-07 03:08:46 +03:00
|
|
|
const sitemapURLs = {}
|
2023-10-05 22:19:58 +03:00
|
|
|
try {
|
|
|
|
const sitemap = await parseStringPromise(data)
|
|
|
|
if (sitemap.urlset.url) {
|
|
|
|
sitemap.urlset.url.forEach((url) => {
|
2023-10-12 01:23:15 +03:00
|
|
|
const lastMod = new Date(url.lastmod).getTime()
|
2023-10-07 03:08:46 +03:00
|
|
|
if (lastMod < Date.now() - weekMs) {
|
2023-10-12 01:23:15 +03:00
|
|
|
url.lastmod = nowISO
|
2023-10-07 03:08:46 +03:00
|
|
|
}
|
2023-10-12 01:23:15 +03:00
|
|
|
sitemapURLs[url.loc] = true
|
2023-10-05 22:19:58 +03:00
|
|
|
})
|
|
|
|
}
|
2023-10-07 03:08:46 +03:00
|
|
|
|
|
|
|
for (const chainName of chains) {
|
|
|
|
const response = await fetch('https://api.uniswap.org/v1/graphql', {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
Origin: 'https://app.uniswap.org',
|
|
|
|
},
|
|
|
|
body: JSON.stringify({ query: getQuery(chainName) }),
|
|
|
|
})
|
|
|
|
const tokensJSON = await response.json()
|
|
|
|
const tokenAddresses = tokensJSON.data.topTokens.map((token) => token.address.toLowerCase())
|
|
|
|
|
|
|
|
tokenAddresses.forEach((address) => {
|
|
|
|
const tokenURL = `https://app.uniswap.org/tokens/${chainName.toLowerCase()}/${address}`
|
|
|
|
if (!(tokenURL in sitemapURLs)) {
|
|
|
|
sitemap.urlset.url.push({
|
2023-10-12 01:23:15 +03:00
|
|
|
loc: [tokenURL],
|
|
|
|
lastmod: [nowISO],
|
|
|
|
priority: [0.8],
|
2023-10-07 03:08:46 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-10-05 22:19:58 +03:00
|
|
|
const builder = new Builder()
|
|
|
|
const xml = builder.buildObject(sitemap)
|
|
|
|
fs.writeFile('./public/sitemap.xml', xml, (error) => {
|
|
|
|
if (error) throw error
|
|
|
|
console.log('Sitemap updated')
|
|
|
|
})
|
2023-10-07 03:08:46 +03:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
2023-10-05 22:19:58 +03:00
|
|
|
}
|
|
|
|
})
|