mirror of
https://git.stupid.fish/teidesu/scripts.git
synced 2025-07-28 02:32:11 +10:00
chore: update public repo
This commit is contained in:
parent
e0109980c0
commit
e7c9507247
25 changed files with 5364 additions and 0 deletions
37
utils/fetch.ts
Normal file
37
utils/fetch.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
import { createWriteStream } from 'node:fs'
|
||||
|
||||
import { type FfetchAddon, ffetchAddons, ffetchBase, type FfetchResultInternals } from '@fuman/fetch'
|
||||
import { toughCookieAddon } from '@fuman/fetch/tough'
|
||||
import { ffetchZodAdapter } from '@fuman/fetch/zod'
|
||||
import { webReadableToFuman, write } from '@fuman/io'
|
||||
import { nodeWritableToFuman } from '@fuman/node'
|
||||
import { type CheerioAPI, load } from 'cheerio'
|
||||
|
||||
const cheerioAddon: FfetchAddon<object, { cheerio: () => Promise<CheerioAPI> }> = {
|
||||
response: {
|
||||
async cheerio(this: FfetchResultInternals<object>) {
|
||||
this._headers ??= {}
|
||||
this._headers.Accept ??= 'text/html; charset=utf-8'
|
||||
return load(await this.text())
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export const ffetch = ffetchBase.extend({
|
||||
addons: [
|
||||
ffetchAddons.parser(ffetchZodAdapter()),
|
||||
cheerioAddon,
|
||||
toughCookieAddon(),
|
||||
],
|
||||
})
|
||||
|
||||
export async function downloadStream(stream: ReadableStream, path: string) {
|
||||
const file = nodeWritableToFuman(createWriteStream(path))
|
||||
await write.pipe(file, webReadableToFuman(stream))
|
||||
file.close()
|
||||
}
|
||||
|
||||
export async function downloadFile(url: string, path: string, extra?: Parameters<typeof ffetch>[1]) {
|
||||
const stream = await ffetch(url, extra).stream()
|
||||
await downloadStream(stream, path)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue