newsnow/scripts/favicon.ts
2024-10-05 23:56:10 +08:00

48 lines
1.4 KiB
TypeScript

import fs from "node:fs"
import { fileURLToPath } from "node:url"
import { join } from "node:path"
import { Buffer } from "node:buffer"
import { getLogos } from "favicons-scraper"
import { consola } from "consola"
import { sources } from "../shared/data"
const projectDir = fileURLToPath(new URL("..", import.meta.url))
const iconsDir = join(projectDir, "public", "icons")
async function downloadImage(url: string, outputPath: string, id: string) {
try {
const response = await fetch(url)
if (!response.ok) {
throw new Error(`${id}: could not fetch ${url}, status: ${response.status}`)
}
const image = await (await fetch(url)).arrayBuffer()
fs.writeFileSync(outputPath, Buffer.from(image))
consola.success(`${id}: downloaded successfully.`)
} catch (error) {
consola.error(`${id}: error downloading the image. `, error)
}
}
async function main() {
await Promise.all(
Object.entries(sources).map(async ([id, source]) => {
try {
const icon = join(iconsDir, `${id.split("-")[0]}.png`)
if (fs.existsSync(icon)) {
consola.info(`${id}: icon exists. skip.`)
return
}
if (!source.home) return
const res = await getLogos(source.home)
if (res.length) {
await downloadImage(res[0].src, icon, id)
}
} catch (e) {
consola.error(id, "\n", e)
}
}),
)
}
main()