mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-18 18:59:15 +08:00
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import fs from "node:fs"
|
|
|
|
import { fileURLToPath } from "node:url"
|
|
import { join } from "node:path"
|
|
import { Buffer } from "node:buffer"
|
|
import { consola } from "consola"
|
|
import { originSources } from "../shared/sources"
|
|
|
|
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(originSources).map(async ([id, source]) => {
|
|
try {
|
|
const icon = join(iconsDir, `${id}.png`)
|
|
if (fs.existsSync(icon)) {
|
|
consola.info(`${id}: icon exists. skip.`)
|
|
return
|
|
}
|
|
if (!source.home) return
|
|
await downloadImage(`https://icons.duckduckgo.com/ip3/${source.home.replace(/^https?:\/\//, "").replace(/\/$/, "")}.ico`, icon, id)
|
|
} catch (e) {
|
|
consola.error(id, "\n", e)
|
|
}
|
|
}),
|
|
)
|
|
}
|
|
|
|
main()
|