mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-31 19:08:05 +08:00
refactor: fetch favicon
This commit is contained in:
parent
6a8e3ba1c5
commit
0aaa1a3355
@ -1,30 +1,24 @@
|
|||||||
import fs from "node:fs"
|
import fs from "node:fs"
|
||||||
import { fileURLToPath } from "node:url"
|
import { fileURLToPath } from "node:url"
|
||||||
import { join } from "node:path"
|
import { join } from "node:path"
|
||||||
|
import { Buffer } from "node:buffer"
|
||||||
import { getLogos } from "favicons-scraper"
|
import { getLogos } from "favicons-scraper"
|
||||||
import fetch from "node-fetch"
|
|
||||||
import { sources } from "../shared/data"
|
import { sources } from "../shared/data"
|
||||||
|
|
||||||
const projectDir = fileURLToPath(new URL("..", import.meta.url))
|
const projectDir = fileURLToPath(new URL("..", import.meta.url))
|
||||||
const iconsDir = join(projectDir, "public", "icons")
|
const iconsDir = join(projectDir, "public", "icons")
|
||||||
async function downloadImage(url: string, outputPath: string) {
|
async function downloadImage(url: string, outputPath: string, id: string) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(url)
|
const response = await fetch(url)
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`Could not fetch ${url}, status: ${response.status}`)
|
throw new Error(`${id}: could not fetch ${url}, status: ${response.status}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileStream = fs.createWriteStream(outputPath)
|
const image = await (await fetch(url)).arrayBuffer()
|
||||||
|
fs.writeFileSync(outputPath, Buffer.from(image))
|
||||||
await new Promise((resolve, reject) => {
|
console.log(`${id}: downloaded successfully.`)
|
||||||
response.body?.pipe(fileStream)
|
|
||||||
fileStream.on("finish", resolve)
|
|
||||||
fileStream.on("error", reject)
|
|
||||||
})
|
|
||||||
|
|
||||||
console.log("Image downloaded successfully.")
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error downloading the image:", error)
|
console.error(`${id}: error downloading the image. `, error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,15 +26,18 @@ async function main() {
|
|||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.entries(sources).map(async ([id, source]) => {
|
Object.entries(sources).map(async ([id, source]) => {
|
||||||
try {
|
try {
|
||||||
const icon = join(iconsDir, `${id}.png`)
|
const icon = join(iconsDir, `${id.split("-")[0]}.png`)
|
||||||
if (fs.existsSync(icon)) return
|
if (fs.existsSync(icon)) {
|
||||||
|
console.log(`${id}: icon exists. skip.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
if (!source.home) return
|
if (!source.home) return
|
||||||
const res = await getLogos(source.home)
|
const res = await getLogos(source.home)
|
||||||
if (res.length) {
|
if (res.length) {
|
||||||
await downloadImage(res[0].src, icon)
|
await downloadImage(res[0].src, icon, id)
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(id, "\n", e )
|
console.error(id, "\n", e)
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user