diff --git a/src/app.d.ts b/src/app.d.ts index ba78252..609bb72 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -36,8 +36,8 @@ declare global { } | { type: 'youtube-music' youtubeUserId: string - username: string - profilePicture: string + username?: string + profilePicture?: string }) interface Connection { diff --git a/src/lib/components/media/mediaPlayer.svelte b/src/lib/components/media/mediaPlayer.svelte index cf16c15..9db2e91 100644 --- a/src/lib/components/media/mediaPlayer.svelte +++ b/src/lib/components/media/mediaPlayer.svelte @@ -5,7 +5,7 @@ // import { FastAverageColor } from 'fast-average-color' import Slider from '$lib/components/util/slider.svelte' - $: console.log(`Queue is now: ${$queue}`) + $: currentlyPlaying = $queue.current() let paused = true, shuffle = false, @@ -48,8 +48,7 @@ $: if (!seeking && durationTimestamp) durationTimestamp.innerText = formatTime(duration) -{#if $queue.queue.length > 0} - {@const currentlyPlaying = $queue.getCurrent()} +{#if currentlyPlaying}
@@ -112,7 +111,7 @@
-
{/if} diff --git a/src/lib/server/youtube-music.ts b/src/lib/server/youtube-music.ts index 0ffafbb..76856ea 100644 --- a/src/lib/server/youtube-music.ts +++ b/src/lib/server/youtube-music.ts @@ -72,16 +72,25 @@ export class YouTubeMusic implements Connection { public getConnectionInfo = async (): Promise> => { const youtube = google.youtube('v3') - const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token: await this.getAccessToken() }) - const userChannel = userChannelResponse.data.items![0] + const access_token = await this.getAccessToken().catch(() => { + return null + }) + + let username, profilePicture + if (access_token) { + const userChannelResponse = await youtube.channels.list({ mine: true, part: ['snippet'], access_token }) + const userChannel = userChannelResponse?.data.items?.[0] + username = userChannel?.snippet?.title ?? undefined // ?? undefined will simply ensure that if it is null it get's converted to undefined + profilePicture = userChannel?.snippet?.thumbnails?.default?.url ?? undefined + } return { id: this.id, userId: this.userId, type: 'youtube-music', youtubeUserId: this.ytUserId, - username: userChannel.snippet?.title!, - profilePicture: userChannel.snippet?.thumbnails?.default?.url!, + username, + profilePicture, } } @@ -182,10 +191,12 @@ export class YouTubeMusic implements Connection { const parseTwoRowItemRenderer = (connection: string, rowContent: InnerTube.musicTwoRowItemRenderer): Song | Album | Artist | Playlist => { const name = rowContent.title.runs[0].text + const thumbnail = refineThumbnailUrl(rowContent.thumbnailRenderer.musicThumbnailRenderer.thumbnail.thumbnails[0].url) let artists: (Song | Album)['artists'], createdBy: (Song | Playlist)['createdBy'] for (const run of rowContent.subtitle.runs) { if (!run.navigationEndpoint) continue + const pageType = run.navigationEndpoint.browseEndpoint.browseEndpointContextSupportedConfigs.browseEndpointContextMusicConfig.pageType if (pageType === 'MUSIC_PAGE_TYPE_ARTIST') { const artist = { id: run.navigationEndpoint.browseEndpoint.browseId, name: run.text } @@ -195,8 +206,6 @@ const parseTwoRowItemRenderer = (connection: string, rowContent: InnerTube.music } } - const thumbnail = refineThumbnailUrl(rowContent.thumbnailRenderer.musicThumbnailRenderer.thumbnail.thumbnails[0].url) - if ('watchEndpoint' in rowContent.navigationEndpoint) { const id = rowContent.navigationEndpoint.watchEndpoint.videoId return { connection, id, name, type: 'song', artists, createdBy, thumbnail } satisfies Song @@ -222,6 +231,7 @@ const parseResponsiveListItemRenderer = (connection: string, listContent: InnerT let artists: (Song | Album)['artists'], createdBy: (Song | Playlist)['createdBy'] for (const run of listContent.flexColumns[1].musicResponsiveListItemFlexColumnRenderer.text.runs) { if (!run.navigationEndpoint) continue + const pageType = run.navigationEndpoint.browseEndpoint.browseEndpointContextSupportedConfigs.browseEndpointContextMusicConfig.pageType if (pageType === 'MUSIC_PAGE_TYPE_ARTIST') { const artist = { id: run.navigationEndpoint.browseEndpoint.browseId, name: run.text } diff --git a/src/lib/stores.ts b/src/lib/stores.ts index c9c38ab..60fe64a 100644 --- a/src/lib/stores.ts +++ b/src/lib/stores.ts @@ -1,4 +1,4 @@ -import { writable, readable, type Writable, type Readable } from 'svelte/store' +import { writable, readable, readonly, type Writable, type Readable } from 'svelte/store' import type { AlertType } from '$lib/components/util/alert.svelte' export const pageWidth: Writable = writable() @@ -9,43 +9,44 @@ const youtubeMusicBackground: string = 'https://www.gstatic.com/youtube/media/yt export const backgroundImage: Writable = writable(youtubeMusicBackground) class Queue { - public currentPos: number - public queue: Song[] + private currentPos: number | null + private songs: Song[] constructor() { - this.queue = [] - this.currentPos = 0 + this.currentPos = null + this.songs = [] } - public enqueue = (...songs: Song[]): void => { - this.queue.push(...songs) + public enqueue = (...songs: Song[]) => { + this.songs.push(...songs) + writeableQueue.set(this) } - public getStart = (): Song | undefined => { - return this.queue[0] - } + public next = () => { + if (this.songs.length === 0) return - public getCurrent = (): Song => { - return this.queue[this.currentPos] - } - - public next = (): Song | undefined => { - if (this.queue.length > this.currentPos + 1) { + if (!this.currentPos) { + this.currentPos = 0 + } else { + if (!(this.songs.length > this.currentPos + 1)) return this.currentPos += 1 - return this.queue[this.currentPos] } - return undefined + writeableQueue.set(this) } - public previous = (): Song | undefined => { - if (this.currentPos > 0) { - this.currentPos -= 1 - return this.queue[this.currentPos] + public current = () => { + if (this.songs.length > 0) { + if (!this.currentPos) this.currentPos = 0 + return this.songs[this.currentPos] } + return null + } - return undefined + public getSongs = () => { + return this.songs } } -export const queue: Readable = readable(new Queue()) +const writeableQueue: Writable = writable(new Queue()) +export const queue: Readable = readonly(writeableQueue) diff --git a/src/routes/(app)/search/+page.svelte b/src/routes/(app)/search/+page.svelte index 7e2a4f8..6ae5a0f 100644 --- a/src/routes/(app)/search/+page.svelte +++ b/src/routes/(app)/search/+page.svelte @@ -22,7 +22,7 @@