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,25 +1,16 @@
import type { RequestHandler } from '@sveltejs/kit'
import { Jellyfin } from '$lib/services'
import { YouTubeMusic } from '$lib/service-managers/youtube-music'
import { Connections } from '$lib/server/users'
import { Connections } from '$lib/server/connections'
export const GET: RequestHandler = async ({ url }) => {
const ids = url.searchParams.get('ids')?.replace(/\s/g, '').split(',')
if (!ids) return new Response('Missing ids query parameter', { status: 400 })
const connections: Connection<serviceType>[] = []
for (const connectionId of ids) {
const connection = Connections.getConnection(connectionId)
switch (connection.type) {
case 'jellyfin':
connection.service = await Jellyfin.fetchSerivceInfo(connection.service.userId, connection.service.urlOrigin, connection.tokens.accessToken)
break
case 'youtube-music':
const ytmusic = new YouTubeMusic(connection)
connection.service = await ytmusic.fetchServiceInfo()
break
}
connections.push(connection)
const connections: ConnectionInfo[] = []
for (const connection of Connections.getConnections(ids)) {
await connection
.getConnectionInfo()
.then((info) => connections.push(info))
.catch((reason) => console.log(`Failed to fetch connection info: ${reason}`))
}
return Response.json({ connections })

View File

@@ -1,22 +1,15 @@
import { Connections } from '$lib/server/users'
import { Jellyfin } from '$lib/services'
import { YouTubeMusic } from '$lib/service-managers/youtube-music'
import type { RequestHandler } from '@sveltejs/kit'
import { Connections } from '$lib/server/connections'
export const GET: RequestHandler = async ({ params }) => {
const userId = params.userId!
const connections = Connections.getUserConnections(userId)
for (const connection of connections) {
switch (connection.type) {
case 'jellyfin':
connection.service = await Jellyfin.fetchSerivceInfo(connection.service.userId, connection.service.urlOrigin, connection.tokens.accessToken)
break
case 'youtube-music':
const youTubeMusic = new YouTubeMusic(connection)
connection.service = await youTubeMusic.fetchServiceInfo()
break
}
const connections: ConnectionInfo[] = []
for (const connection of Connections.getUserConnections(userId)) {
await connection
.getConnectionInfo()
.then((info) => connections.push(info))
.catch((reason) => console.log(`Failed to fetch connection info: ${reason}`))
}
return Response.json({ connections })

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 })