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
874e1952e3
commit
ce9f435ef2
3 changed files with 70 additions and 26 deletions
|
@ -2,12 +2,26 @@ import { z } from 'zod'
|
|||
import { ffetch as ffetchBase } from './fetch.ts'
|
||||
import { getEnv } from './misc.ts'
|
||||
|
||||
export const navidromeFfetch = ffetchBase.extend({
|
||||
baseUrl: getEnv('NAVIDROME_ENDPOINT'),
|
||||
headers: {
|
||||
'x-nd-authorization': `Bearer ${getEnv('NAVIDROME_TOKEN')}`,
|
||||
},
|
||||
})
|
||||
let _cachedFfetch: typeof ffetchBase | undefined
|
||||
export async function getNavidromeFfetch() {
|
||||
if (_cachedFfetch) return _cachedFfetch
|
||||
const baseUrl = getEnv('NAVIDROME_ENDPOINT')
|
||||
const authRes = await ffetchBase.post('/auth/login', {
|
||||
baseUrl,
|
||||
json: {
|
||||
username: getEnv('NAVIDROME_USERNAME'),
|
||||
password: getEnv('NAVIDROME_PASSWORD'),
|
||||
},
|
||||
}).parsedJson(z.object({ token: z.string() }))
|
||||
|
||||
_cachedFfetch = ffetchBase.extend({
|
||||
baseUrl,
|
||||
headers: {
|
||||
'x-nd-authorization': `Bearer ${authRes.token}`,
|
||||
},
|
||||
})
|
||||
return _cachedFfetch
|
||||
}
|
||||
|
||||
export const NavidromeSong = z.object({
|
||||
id: z.string(),
|
||||
|
@ -17,11 +31,13 @@ export const NavidromeSong = z.object({
|
|||
artist: z.string(),
|
||||
path: z.string(),
|
||||
duration: z.number(),
|
||||
size: z.number(),
|
||||
})
|
||||
export type NavidromeSong = z.infer<typeof NavidromeSong>
|
||||
|
||||
export function fetchSongs(offset: number, pageSize: number) {
|
||||
return navidromeFfetch('/api/song', {
|
||||
export async function fetchSongs(offset: number, pageSize: number) {
|
||||
const api = await getNavidromeFfetch()
|
||||
return api('/api/song', {
|
||||
query: {
|
||||
_start: offset,
|
||||
_end: offset + pageSize,
|
||||
|
@ -30,3 +46,18 @@ export function fetchSongs(offset: number, pageSize: number) {
|
|||
},
|
||||
}).parsedJson(z.array(NavidromeSong))
|
||||
}
|
||||
|
||||
export async function* fetchSongsIter(params?: {
|
||||
chunkSize?: number
|
||||
onChunkProcessed?: (page: number, items: number) => void
|
||||
}) {
|
||||
const { chunkSize = 1000, onChunkProcessed } = params ?? {}
|
||||
for (let offset = 0; ; offset += chunkSize) {
|
||||
const songs = await fetchSongs(offset, chunkSize)
|
||||
if (songs.length === 0) return
|
||||
|
||||
yield * songs
|
||||
|
||||
onChunkProcessed?.(Math.floor(offset / chunkSize), songs.length)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue