Updated Jellyfin media item parsers, added startIndex && limit params to YTMusic getPlaylistItems() method

This commit is contained in:
Eclypsed
2024-05-29 01:25:17 -04:00
parent 11497f8b91
commit a98b258a03
10 changed files with 303 additions and 230 deletions

View File

@@ -16,7 +16,7 @@ export const GET: RequestHandler = async ({ url, request }) => {
// * Withing the .getAudioStream() method of connections, a TypeError should be thrown if the request was invalid (e.g. non-existent id)
// * A standard Error should be thrown if the fetch to the service's server failed or the request returned invalid data
.catch((error: TypeError | Error) => {
if (error instanceof TypeError) return new Response('Malformed Request', { status: 400 })
if (error instanceof TypeError) return new Response('Bad Request', { status: 400 })
return new Response('Failed to fetch valid audio stream', { status: 502 })
})

View File

@@ -9,8 +9,13 @@ export const GET: RequestHandler = async ({ params, url }) => {
const playlistId = url.searchParams.get('id')
if (!playlistId) return new Response(`Missing id search parameter`, { status: 400 })
const playlist = await connection.getPlaylist(playlistId).catch(() => undefined)
if (!playlist) return new Response(`Failed to fetch playlist with id: ${playlistId}`, { status: 400 })
const response = await connection
.getPlaylistItems(playlistId)
.then((playlist) => Response.json({ playlist }))
.catch((error: TypeError | Error) => {
if (error instanceof TypeError) return new Response('Bad Request', { status: 400 })
return new Response('Failed to fetch playlist items', { status: 502 })
})
return Response.json({ playlist })
return response
}

View File

@@ -1,13 +1,27 @@
import type { RequestHandler } from '@sveltejs/kit'
import { Connections } from '$lib/server/connections'
export const GET: RequestHandler = async ({ params }) => {
export const GET: RequestHandler = async ({ params, url }) => {
const { connectionId, playlistId } = params
const connection = Connections.getConnection(connectionId!)
if (!connection) return new Response('Invalid connection id', { status: 400 })
const items = await connection.getPlaylistItems(playlistId!).catch((reason) => console.error(reason))
if (!items) return new Response(`Failed to fetch playlist with id: ${playlistId!}`, { status: 400 })
const startIndexString = url.searchParams.get('startIndex')
const limitString = url.searchParams.get('limit')
return Response.json({ items })
const numberStartIndex = Number(startIndexString)
const numberLimit = Number(limitString)
const startIndex = Number.isInteger(numberStartIndex) && numberStartIndex > 0 ? numberStartIndex : undefined
const limit = Number.isInteger(numberLimit) && numberLimit > 0 ? numberLimit : undefined
const response = await connection
.getPlaylistItems(playlistId!, startIndex, limit)
.then((items) => Response.json({ items }))
.catch((error: TypeError | Error) => {
if (error instanceof TypeError) return new Response('Bad Request', { status: 400 })
return new Response('Failed to fetch playlist items', { status: 502 })
})
return response
}