Going to try out some OOP/DI patterns and see where that takes me

This commit is contained in:
Eclypsed
2024-03-24 16:03:31 -04:00
parent d50497e7d5
commit 15db7f1aed
22 changed files with 894 additions and 900 deletions

View File

@@ -1,49 +1,17 @@
import type { RequestHandler } from '@sveltejs/kit'
import { SECRET_INTERNAL_API_KEY } from '$env/static/private'
import { Jellyfin } from '$lib/services'
import { YouTubeMusic } from '$lib/service-managers/youtube-music'
import { Connections } from '$lib/server/connections'
// This is temporary functionally for the sake of developing the app.
// In the future will implement more robust algorithm for offering recommendations
export const GET: RequestHandler = async ({ params, fetch }) => {
export const GET: RequestHandler = async ({ params }) => {
const userId = params.userId!
const connectionsResponse = await fetch(`/api/users/${userId}/connections`, { headers: { apikey: SECRET_INTERNAL_API_KEY } })
const userConnections = await connectionsResponse.json()
const recommendations: MediaItem[] = []
for (const connection of userConnections.connections) {
const { type, service, tokens } = connection as Connection<serviceType>
switch (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
const requestHeaders = new Headers({ Authorization: `MediaBrowser Token="${tokens.accessToken}"` })
const mostPlayedResponse = await fetch(mostPlayedSongsURL, { headers: requestHeaders })
const mostPlayedData = await mostPlayedResponse.json()
for (const song of mostPlayedData.Items) recommendations.push(Jellyfin.songFactory(song, connection))
break
case 'youtube-music':
const youtubeMusic = new YouTubeMusic(connection)
await youtubeMusic
.getHome()
.then(({ listenAgain, quickPicks, newReleases }) => {
for (const mediaItem of listenAgain) recommendations.push(mediaItem)
})
.catch()
break
}
for (const connection of Connections.getUserConnections(userId)) {
await connection
.getRecommendations()
.then((connectionRecommendations) => recommendations.push(...connectionRecommendations))
.catch((reason) => console.log(`Failed to fetch recommendations: ${reason}`))
}
return Response.json({ recommendations })