playlist tab/view

This commit is contained in:
austinried
2021-07-19 13:14:13 +09:00
parent ac1970145f
commit 9d835f04aa
11 changed files with 433 additions and 26 deletions

View File

@@ -10,6 +10,8 @@ import {
GetCoverArtParams,
GetIndexesParams,
GetMusicDirectoryParams,
GetPlaylistParams,
GetPlaylistsParams,
GetTopSongsParams,
StreamParams,
} from '@app/subsonic/params'
@@ -23,6 +25,8 @@ import {
GetArtistsResponse,
GetIndexesResponse,
GetMusicDirectoryResponse,
GetPlaylistResponse,
GetPlaylistsResponse,
GetTopSongsResponse,
SubsonicResponse,
} from '@app/subsonic/responses'
@@ -75,7 +79,7 @@ export class SubsonicApiClient {
}
const url = `${this.address}/rest/${method}?${query}`
// console.log(url);
// console.log(url)
return url
}
@@ -95,7 +99,7 @@ export class SubsonicApiClient {
const response = await fetch(this.buildUrl(method, params))
const text = await response.text()
// console.log(text);
// console.log(text)
const xml = new DOMParser().parseFromString(text)
if (xml.documentElement.getAttribute('status') !== 'ok') {
@@ -186,6 +190,20 @@ export class SubsonicApiClient {
return new SubsonicResponse<GetAlbumList2Response>(xml, new GetAlbumList2Response(xml))
}
//
// Playlists
//
async getPlaylists(params?: GetPlaylistsParams): Promise<SubsonicResponse<GetPlaylistsResponse>> {
const xml = await this.apiGetXml('getPlaylists', params)
return new SubsonicResponse<GetPlaylistsResponse>(xml, new GetPlaylistsResponse(xml))
}
async getPlaylist(params: GetPlaylistParams): Promise<SubsonicResponse<GetPlaylistResponse>> {
const xml = await this.apiGetXml('getPlaylist', params)
return new SubsonicResponse<GetPlaylistResponse>(xml, new GetPlaylistResponse(xml))
}
//
// Media retrieval
//
@@ -195,7 +213,7 @@ export class SubsonicApiClient {
return await this.apiDownload('getCoverArt', path, params)
}
getCoverArtUri(params: GetCoverArtParams): string {
getCoverArtUri(params?: GetCoverArtParams): string {
return this.buildUrl('getCoverArt', params)
}

View File

@@ -235,3 +235,46 @@ export class AlbumID3Element {
this.genre = optionalString(e, 'genre')
}
}
export class PlaylistElement {
allowedUser: string[] = []
id: string
name: string
comment?: string
owner?: string
public?: boolean
songCount: number
duration: number
created: Date
changed: Date
coverArt?: string
constructor(e: Element) {
for (let i = 0; i < e.getElementsByTagName('allowedUser').length; i++) {
this.allowedUser.push(e.getElementsByTagName('allowedUser')[i].textContent as string)
}
this.id = requiredString(e, 'id')
this.name = requiredString(e, 'name')
this.comment = optionalString(e, 'comment')
this.owner = optionalString(e, 'owner')
this.public = optionalBoolean(e, 'public')
this.songCount = requiredInt(e, 'songCount')
this.duration = requiredInt(e, 'duration')
this.created = requiredDate(e, 'created')
this.changed = requiredDate(e, 'changed')
this.coverArt = optionalString(e, 'coverArt')
}
}
export class PlaylistWithSongsElement extends PlaylistElement {
songs: ChildElement[] = []
constructor(e: Element) {
super(e)
for (let i = 0; i < e.getElementsByTagName('entry').length; i++) {
this.songs.push(new ChildElement(e.getElementsByTagName('entry')[i]))
}
}
}

View File

@@ -72,6 +72,18 @@ export type GetAlbumList2Params =
export type GetAlbumListParams = GetAlbumList2Params
//
// Playlists
//
export type GetPlaylistsParams = {
username?: string
}
export type GetPlaylistParams = {
id: string
}
//
// Media retrieval
//

View File

@@ -6,6 +6,8 @@ import {
ArtistInfoElement,
ChildElement,
DirectoryElement,
PlaylistElement,
PlaylistWithSongsElement,
} from '@app/subsonic/elements'
export type ResponseStatus = 'ok' | 'failed'
@@ -153,3 +155,26 @@ export class GetAlbumList2Response extends BaseGetAlbumListResponse<AlbumID3Elem
super(xml, AlbumID3Element)
}
}
//
// Playlists
//
export class GetPlaylistsResponse {
playlists: PlaylistElement[] = []
constructor(xml: Document) {
const playlistElements = xml.getElementsByTagName('playlist')
for (let i = 0; i < playlistElements.length; i++) {
this.playlists.push(new PlaylistElement(playlistElements[i]))
}
}
}
export class GetPlaylistResponse {
playlist: PlaylistWithSongsElement
constructor(xml: Document) {
this.playlist = new PlaylistWithSongsElement(xml.getElementsByTagName('playlist')[0])
}
}