demo_network_visjs/client/fetch_db_from_site.ts
2024-01-23 14:13:26 +01:00

43 lines
1.1 KiB
TypeScript

import { DbEntry } from '../datas.ts'
const domParser = new DOMParser()
export async function fetchDbFromSite(siteUrl: string) {
const index = await fetch(siteUrl, { mode: 'no-cors' })
.then((response) => response.text())
.then((raw) => domParser.parseFromString(raw, 'text/html'))
const nodes = Array.from(
index.querySelectorAll<HTMLAnchorElement>('#episodes>a'),
)
.map((a) => ({ href: a.href, title: a.innerText }))
const db: DbEntry[] = []
for (const node of nodes) {
const id = getNodeId(node)
const title = node.title
const content = node.href
const links = await getNodeLinks(node)
db.push({ id, title, content, links })
}
return db
}
function getNodeId({ href }: { href: string }) {
const { pathname } = new URL(href)
return Number.parseInt(pathname.slice(1, -5))
}
async function getNodeLinks({ href }: { href: string }) {
const page = await fetch(href, { mode: 'no-cors' })
.then((response) => response.text())
.then((raw) => domParser.parseFromString(raw, 'text/html'))
return Array.from(page.querySelectorAll<HTMLAnchorElement>('.cell-p>a')).map(
getNodeId,
)
}