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-25 00:33:34 -05:00
|
|
|
import { Jellyfin } from '$lib/services'
|
|
|
|
|
import { YouTubeMusic } from '$lib/service-managers/youtube-music'
|
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) {
|
2024-02-23 00:53:54 -05:00
|
|
|
const { type, service, tokens } = connection as Connection<serviceType>
|
2024-02-03 02:47:23 -05:00
|
|
|
|
2024-02-23 00:53:54 -05:00
|
|
|
switch (type) {
|
2024-02-03 02:47:23 -05:00
|
|
|
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-23 00:53:54 -05:00
|
|
|
const requestHeaders = new Headers({ Authorization: `MediaBrowser Token="${tokens.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-24 02:06:02 -05:00
|
|
|
case 'youtube-music':
|
2024-02-28 03:03:40 -05:00
|
|
|
const youtubeMusic = new YouTubeMusic(connection)
|
2024-03-09 01:44:22 -05:00
|
|
|
await youtubeMusic
|
|
|
|
|
.getHome()
|
|
|
|
|
.then(({ listenAgain, quickPicks, newReleases }) => {
|
|
|
|
|
for (const mediaItem of listenAgain) recommendations.push(mediaItem)
|
|
|
|
|
})
|
|
|
|
|
.catch()
|
2024-02-24 02:06:02 -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
|
|
|
}
|