mirror of
https://codeberg.org/lucida/lucida.git
synced 2026-11-17 00:57:25 -05:00
add playlist
This commit is contained in:
@@ -1,5 +1,12 @@
|
|||||||
import Librespot, { LibrespotOptions } from 'librespot'
|
import Librespot, { LibrespotOptions } from 'librespot'
|
||||||
import { parseArtist, parseAlbum, parseTrack, parseEpisode, parsePodcast } from './parse.js'
|
import {
|
||||||
|
parseArtist,
|
||||||
|
parseAlbum,
|
||||||
|
parseTrack,
|
||||||
|
parseEpisode,
|
||||||
|
parsePodcast,
|
||||||
|
parsePlaylist
|
||||||
|
} from './parse.js'
|
||||||
import {
|
import {
|
||||||
GetByUrlResponse,
|
GetByUrlResponse,
|
||||||
ItemType,
|
ItemType,
|
||||||
@@ -41,7 +48,9 @@ class Spotify implements StreamerWithLogin {
|
|||||||
if (this.storedCredential) return await this.client.loginWithStoredCreds(username, password)
|
if (this.storedCredential) return await this.client.loginWithStoredCreds(username, password)
|
||||||
else return await this.client.login(username, password)
|
else return await this.client.login(username, password)
|
||||||
}
|
}
|
||||||
#getUrlParts(url: string): ['artist' | 'album' | 'track' | 'episode' | 'show', string] {
|
#getUrlParts(
|
||||||
|
url: string
|
||||||
|
): ['artist' | 'album' | 'track' | 'episode' | 'show' | 'playlist', string] {
|
||||||
const urlObj = new URL(url)
|
const urlObj = new URL(url)
|
||||||
const parts = urlObj.pathname.slice(1).split('/')
|
const parts = urlObj.pathname.slice(1).split('/')
|
||||||
if (parts.length > 2) throw new Error('Unknown Spotify URL')
|
if (parts.length > 2) throw new Error('Unknown Spotify URL')
|
||||||
@@ -50,7 +59,8 @@ class Spotify implements StreamerWithLogin {
|
|||||||
parts[0] != 'track' &&
|
parts[0] != 'track' &&
|
||||||
parts[0] != 'album' &&
|
parts[0] != 'album' &&
|
||||||
parts[0] != 'show' &&
|
parts[0] != 'show' &&
|
||||||
parts[0] != 'episode'
|
parts[0] != 'episode' &&
|
||||||
|
parts[0] != 'playlist'
|
||||||
) {
|
) {
|
||||||
throw new Error(`Spotify type "${parts[0]}" unsupported`)
|
throw new Error(`Spotify type "${parts[0]}" unsupported`)
|
||||||
}
|
}
|
||||||
@@ -132,6 +142,14 @@ class Spotify implements StreamerWithLogin {
|
|||||||
episodes: metadata.episodes?.map((e) => parseEpisode(e)) ?? []
|
episodes: metadata.episodes?.map((e) => parseEpisode(e)) ?? []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case 'playlist': {
|
||||||
|
const metadata = await this.client.get.playlist(id)
|
||||||
|
return {
|
||||||
|
type: 'playlist',
|
||||||
|
metadata: parsePlaylist(metadata),
|
||||||
|
tracks: metadata.tracks?.map((e) => parseTrack(e)) ?? []
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
async search(query: string): Promise<SearchResults> {
|
async search(query: string): Promise<SearchResults> {
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import type {
|
|||||||
SpotifyThumbnail,
|
SpotifyThumbnail,
|
||||||
SpotifyTrack,
|
SpotifyTrack,
|
||||||
SpotifyEpisode,
|
SpotifyEpisode,
|
||||||
SpotifyPodcast
|
SpotifyPodcast,
|
||||||
|
SpotifyPlaylist
|
||||||
} from 'librespot/types'
|
} from 'librespot/types'
|
||||||
import { Album, Artist, Episode, Podcast, Track } from '../../types.js'
|
import { Album, Artist, Episode, Playlist, Podcast, Track } from '../../types.js'
|
||||||
|
|
||||||
function parseThumbnails(raw: SpotifyThumbnail[]) {
|
function parseThumbnails(raw: SpotifyThumbnail[]) {
|
||||||
return raw
|
return raw
|
||||||
@@ -92,3 +93,14 @@ export function parsePodcast(raw: SpotifyPodcast) {
|
|||||||
|
|
||||||
return podcast
|
return podcast
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function parsePlaylist(raw: SpotifyPlaylist) {
|
||||||
|
const playlist: Playlist = {
|
||||||
|
id: raw.id,
|
||||||
|
title: raw.name,
|
||||||
|
url: raw.externalUrl,
|
||||||
|
trackCount: raw.totalTracks
|
||||||
|
}
|
||||||
|
|
||||||
|
return playlist
|
||||||
|
}
|
||||||
|
|||||||
@@ -97,8 +97,7 @@ export function parsePlaylist(raw: RawPlaylist): Playlist {
|
|||||||
id: raw.uuid,
|
id: raw.uuid,
|
||||||
title: raw.title,
|
title: raw.title,
|
||||||
url: raw.url,
|
url: raw.url,
|
||||||
trackCount: raw.numberOfTracks,
|
trackCount: raw.numberOfTracks
|
||||||
tracks: []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
playlist.coverArtwork = [
|
playlist.coverArtwork = [
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ export interface Playlist {
|
|||||||
url: string
|
url: string
|
||||||
coverArtwork?: CoverArtwork[]
|
coverArtwork?: CoverArtwork[]
|
||||||
trackCount?: number
|
trackCount?: number
|
||||||
tracks: Track[]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Album {
|
export interface Album {
|
||||||
|
|||||||
Reference in New Issue
Block a user