import axios from 'axios'
import fs from 'fs'
import path from 'path'
const formatDate = (date = new Date) => {
const year = date.getFullYear ()
const month = (date.getMonth () + 1).toString ().padStart (2, '0')
const day = date.getDate ().toString ().padStart (2, '0')
return `${ year }-${ month }-${ day }`
}
const SITE_TITLE = 'ぼざクリ タグ広場'
const DOMAIN = 'https://hub.nizika.monster'
const API_BASE_URL = 'https://hub.nizika.monster/api'
const fetchPosts = async () => (await axios.get (`${ API_BASE_URL }/posts`)).data.posts
const fetchPostIds = async () => (await fetchPosts ()).map (post => post.id)
const fetchTags = async () => (await axios.get (`${ API_BASE_URL }/tags`)).data
const fetchTagNames = async () => (await fetchTags ()).map (tag => tag.name)
const fetchWikiPages = async () => (await axios.get (`${ API_BASE_URL }/wiki`)).data
const fetchWikiTitles = async () => (await fetchWikiPages ()).map (page => page.title)
const posts = (await fetchPosts ()).map (post => [
`/posts/${ post.id }`,
`${ post.title || post.url } | ${ SITE_TITLE }` ])
const tags = (await fetchTags ()).map (tag => [
`/posts?${ new URLSearchParams ({ tags: tag.name }) }`,
`${ tag.name } | ${ SITE_TITLE }`])
const wikiPages = (await fetchWikiPages ()).map (page => [
`/wiki/${ encodeURIComponent (page.title) }`,
`${ page.title } Wiki | ${ SITE_TITLE }`])
const routes = [
['/', `${ SITE_TITLE } 〜 ぼざろクリーチャーシリーズ綜合リンク集サイト`],
...tags,
...posts,
['/wiki', `Wiki | ${ SITE_TITLE }`],
...wikiPages]
const xml = `
${ routes.map (([route]) => `
${ DOMAIN }${ route }
${ formatDate () }
daily
`).join ('\n') }
`
const rss = `
The Series of Bocchi the Rock! Creatures Hub Feed
${ DOMAIN }/
最新の投稿、タグ、Wiki ページの一覧
ja
${ (new Date).toUTCString () }
${ routes.map (([route, title]) => ` -
${ title }
${ DOMAIN }${ route }
${ DOMAIN }${ route }
${ (new Date).toUTCString () }
`).join ('\n') }
`
fs.writeFileSync (path.resolve ('dist/sitemap.xml'), xml)
fs.writeFileSync (path.resolve ('dist/rss.xml'), rss)