Update utils.ts

This commit is contained in:
freearhey
2026-03-23 17:13:15 +03:00
parent c4f2d20ca4
commit 273816b9a3

View File

@@ -1,4 +1,9 @@
import { MasterPlaylist, MediaPlaylist, Variant } from 'hls-parser/types'
import { parse as parsePlaylist } from 'hls-parser'
import { TESTING } from './constants.js'
import normalizeUrl from 'normalize-url'
import { orderBy } from 'es-toolkit'
import axios from 'axios'
export function isURI(string: string): boolean {
try {
@@ -21,3 +26,33 @@ export function truncate(string: string, limit: number = 100) {
return string.slice(0, limit - 3) + '...'
}
export async function getStreamInfo(
url: string,
options: { httpUserAgent: string | null; httpReferrer: string | null }
): Promise<Variant | undefined> {
let playlist: MasterPlaylist | MediaPlaylist | undefined
if (TESTING) {
playlist = (await import('../tests/__data__/input/playlist_update/playlist.mjs'))
.default as unknown as MasterPlaylist
} else {
try {
const response = await axios(url, {
signal: AbortSignal.timeout(30000),
headers: {
'User-Agent': options.httpUserAgent || 'Mozilla/5.0',
Referer: options.httpReferrer
}
})
playlist = parsePlaylist(response.data)
} catch {}
}
if (playlist && playlist.isMasterPlaylist && playlist.variants.length) {
return orderBy(playlist.variants, ['bandwidth'], ['desc'])[0]
}
return undefined
}