Update utils.ts
This commit is contained in:
@@ -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 normalizeUrl from 'normalize-url'
|
||||||
|
import { orderBy } from 'es-toolkit'
|
||||||
|
import axios from 'axios'
|
||||||
|
|
||||||
export function isURI(string: string): boolean {
|
export function isURI(string: string): boolean {
|
||||||
try {
|
try {
|
||||||
@@ -21,3 +26,33 @@ export function truncate(string: string, limit: number = 100) {
|
|||||||
|
|
||||||
return string.slice(0, limit - 3) + '...'
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user