Files
Lazuli/src/routes/api/users/[userId]/recommendations/+server.ts

41 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-02-03 02:47:23 -05:00
import type { RequestHandler } from '@sveltejs/kit'
import { SECRET_INTERNAL_API_KEY } from '$env/static/private'
2024-02-04 01:01:37 -05:00
import { Jellyfin } from '$lib/service-managers/jellyfin'
2024-02-03 02:47:23 -05:00
// This is temporary functionally for the sake of developing the app.
2024-02-04 01:01:37 -05:00
// In the future will implement more robust algorithm for offering recommendations
2024-02-03 02:47:23 -05:00
export const GET: RequestHandler = async ({ params, fetch }) => {
2024-02-18 00:01:54 -05:00
const userId = params.userId!
2024-02-03 02:47:23 -05:00
const connectionsResponse = await fetch(`/api/users/${userId}/connections`, { headers: { apikey: SECRET_INTERNAL_API_KEY } })
2024-02-18 00:01:54 -05:00
const userConnections = await connectionsResponse.json()
2024-02-03 02:47:23 -05:00
2024-02-18 00:01:54 -05:00
const recommendations: MediaItem[] = []
2024-02-03 02:47:23 -05:00
2024-02-18 00:01:54 -05:00
for (const connection of userConnections.connections) {
const { service, accessToken } = connection as Connection
2024-02-03 02:47:23 -05:00
switch (service.type) {
case 'jellyfin':
const mostPlayedSongsSearchParams = new URLSearchParams({
SortBy: 'PlayCount',
SortOrder: 'Descending',
IncludeItemTypes: 'Audio',
Recursive: 'true',
limit: '10',
})
const mostPlayedSongsURL = new URL(`/Users/${service.userId}/Items?${mostPlayedSongsSearchParams.toString()}`, service.urlOrigin).href
2024-02-18 00:01:54 -05:00
const requestHeaders = new Headers({ Authorization: `MediaBrowser Token="${accessToken}"` })
2024-02-03 02:47:23 -05:00
const mostPlayedResponse = await fetch(mostPlayedSongsURL, { headers: requestHeaders })
const mostPlayedData = await mostPlayedResponse.json()
2024-02-04 01:01:37 -05:00
2024-02-18 00:01:54 -05:00
for (const song of mostPlayedData.Items) recommendations.push(Jellyfin.songFactory(song, connection))
2024-02-04 01:01:37 -05:00
break
2024-02-03 02:47:23 -05:00
}
}
2024-02-04 01:01:37 -05:00
return Response.json({ recommendations })
2024-02-03 02:47:23 -05:00
}