-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): Shared generic typedef for player state, send transf…
…ormed WNP state
- Loading branch information
Showing
7 changed files
with
216 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import type { PlayerObj, PlayerState } from "@/types/player"; | ||
import type { PlaybackState } from "@spotify/web-api-ts-sdk"; | ||
|
||
function makeItemObj(item: PlaybackState["item"]): PlayerObj["item"] { | ||
const baseObj = { | ||
title: item.name, | ||
duration_ms: item.duration_ms, | ||
url: item.external_urls.spotify, | ||
}; | ||
|
||
if (item.type === "episode" && "show" in item) { | ||
const show = item.show; | ||
|
||
return { | ||
...baseObj, | ||
show: { | ||
name: show.name, | ||
url: show.external_urls.spotify, | ||
}, | ||
type: "episode", | ||
}; | ||
} | ||
|
||
const artists = "artists" in item ? item.artists : []; | ||
const album = "album" in item ? item.album : null; | ||
|
||
return { | ||
...baseObj, | ||
artists: artists.map((artist) => ({ | ||
name: artist.name, | ||
url: artist.external_urls.spotify, | ||
})), | ||
album: | ||
album ? | ||
{ | ||
name: album.name, | ||
url: album.external_urls.spotify, | ||
} | ||
: undefined, | ||
type: "track", | ||
}; | ||
} | ||
|
||
export default function makePlayerStateObj( | ||
state: PlaybackState | null | ||
): PlayerState { | ||
if (!state) return null; | ||
|
||
const item = state.item; | ||
if (!item) return null; | ||
|
||
const playerObj: PlayerState = { | ||
item: makeItemObj(item), | ||
meta: { | ||
is_playing: state.is_playing, | ||
shuffle_state: state.shuffle_state, | ||
repeat_state: state.repeat_state as "off" | "track" | "context", | ||
position_ms: state.progress_ms, | ||
duration_ms: state.item.duration_ms, | ||
main_img_url: ("album" in state.item ? state.item.album : state.item) | ||
.images[0]?.url, | ||
provider: "spotify", | ||
}, | ||
}; | ||
|
||
return playerObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export type WebNowPlayingPlayerState = { | ||
id: number; | ||
name: string /* Website name */; | ||
|
||
title: string; | ||
artist: string; | ||
album: string; | ||
cover: string; | ||
|
||
state: number; | ||
|
||
duration: number; | ||
position: number; | ||
volume: number; | ||
rating: number; | ||
repeat: number; | ||
shuffle: boolean; | ||
|
||
ratingSystem: number; | ||
availableRepeat: number; | ||
|
||
canSetState: boolean; | ||
canSkipPrevious: boolean; | ||
canSkipNext: boolean; | ||
canSetPosition: boolean; | ||
canSetVolume: boolean; | ||
canSetRating: boolean; | ||
canSetRepeat: boolean; | ||
canSetShuffle: boolean; | ||
|
||
createdAt: number; | ||
updatedAt: number; | ||
activeAt: number; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { WebNowPlayingPlayerState } from "@/providers/webnowplaying/types"; | ||
import type { PlayerState } from "@/types/player"; | ||
|
||
export default function makePlayerStateObj( | ||
state: WebNowPlayingPlayerState | null | ||
): PlayerState { | ||
if (!state) return null; | ||
|
||
const playerObj: PlayerState = { | ||
item: { | ||
title: state.title, | ||
artists: [ | ||
{ | ||
name: state.artist, | ||
}, | ||
], | ||
album: { | ||
name: state.album, | ||
images: [state.cover], | ||
}, | ||
duration_ms: state.duration * 1000, | ||
type: "track", | ||
}, | ||
meta: { | ||
is_playing: state.state === 0, | ||
shuffle_state: state.shuffle, | ||
repeat_state: | ||
state.repeat === 1 ? "track" | ||
: state.repeat === 2 ? "context" | ||
: "off", | ||
position_ms: state.position * 1000, | ||
duration_ms: state.duration * 1000, | ||
main_img_url: state.cover, | ||
provider: state.name, | ||
}, | ||
}; | ||
|
||
return playerObj; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,56 @@ | ||
import { PlaybackState } from "@spotify/web-api-ts-sdk"; | ||
type ArtistItem = { | ||
name: string; | ||
images?: string[]; | ||
url?: string; | ||
}; | ||
|
||
export type PlayerState = PlaybackState | null; | ||
type AlbumItem = { | ||
name?: string; | ||
images?: string[]; | ||
url?: string; | ||
}; | ||
|
||
type PlayerTrackItem = { | ||
title: string; | ||
artists: ArtistItem[]; | ||
album?: AlbumItem; | ||
duration_ms?: number; | ||
|
||
type?: "track" | string; | ||
url?: string; | ||
}; | ||
|
||
type PlayerShowItem = { | ||
name: string; | ||
episodes?: PlayerEpisodeItem[]; | ||
images?: string[]; | ||
url?: string; | ||
}; | ||
|
||
type PlayerEpisodeItem = { | ||
title: string; | ||
show?: PlayerShowItem; | ||
season?: number; | ||
episode?: number; | ||
duration_ms?: number; | ||
|
||
type: "episode"; | ||
url?: string; | ||
}; | ||
|
||
export type PlayerMeta = { | ||
is_playing?: boolean; | ||
shuffle_state?: boolean; | ||
repeat_state?: "off" | "track" | "context"; | ||
position_ms?: number; | ||
duration_ms?: number; | ||
main_img_url?: string; | ||
provider?: string; | ||
}; | ||
|
||
export type PlayerObj = { | ||
item: PlayerTrackItem | PlayerEpisodeItem; | ||
meta: PlayerMeta; | ||
}; | ||
|
||
export type PlayerState = PlayerObj | null; |