cody-music
Cody Music is an open source package designed to help you perform Mac iTunes and Spotify Web API playback functionality
Player control support
- Mac Spotify and iTunes desktop
- Spotify Web
Spotify web API support
- Fetching Spotify audio features
- Playlists
(create, delete, fetch playlist tracks, replace playlist tracks)
- Genre search (Recently updated to return highest frequency Spotify Genre)
- Fetching Spotify devices
- Access token refresh retry
- Track recommendations from Spotify
- Add and remove to the Liked playlist
- Prevents adding duplicate playlists by name
- Follow or unfollow a playlist
- Spotify Recommendations
iTunes API support
Installation
npm
$ npm install cody-music --save
yarn
$ yarn add cody-music
Running unit tests
$ npm test
Load the module
import {
getRunningTrack,
Track,
PlayerType,
TrackStatus,
setConfig } from "cody-music";
...
setConfig({
spotifyAccessToken: <spotify_access_token>,
spotifyRefreshToken: <spotify_refresh_token>;
spotifyClientSecret: <spotify_client_secret>;
spotifyClientId: <spotify_client_id>;
enableItunesDesktop: <enable_itunes_desktop_track_lookup>;
enableSpotifyDesktop: <enable_spotify_desktop_track_lookup>;
enableSpotifyApi: <enable_spotify_api>;
});
const track:Track = await getRunningTrack();
if (track.state === TrackStatus.Playing) {
}
if (track.playerType === PlayerType.WebSpotify) {
}
OR
import * as CodyMusic from "cody-music";
OR
const CodyMusic = require("cody-music");
API
playTrack(uri)
Play a track with Music URI uri
.
Specify either "Spotify" or "iTunes" (case-insensitive).
await CodyMusic.getRunningTrack().then((track: Track) => {
});
await CodyMusic.playTrack(
"spotify",
"spotify:track:2YarjDYjBJuH63dUIh9OWv"
).then(result => {
});
await CodyMusic.playTrack("itunes", 1).then(result => {
});
await CodyMusic.playTrack("spotify", 1000000000).then(result => {
if (result.error) {
console.log(`Unable to play track, error: ${result.error}`);
}
});
await CodyMusic.getRunningTrack().then(result => {
});
Full set of APIs
setConfig(config: CodyConfig)
searchTracks(keywords: string, limit: number = 50)
searchArtists(keywords: string, limit: number = 50)
isItunesAccessGranted()
isItunesDesktopEnabled()
isItunesDesktopSongTrackingEnabled()
getSpotifyAccessToken()
isSpotifyDesktopEnabled()
isSpotifyRunning()
isItunesRunning()
isPlayerRunning(player: PlayerName)
hasActiveTrack(): Promise<boolean>
getRecommendationsForTracks(
trackIds: string[] = [],
limit: number = 40,
market: string = "",
min_popularity: number = 20,
target_popularity: number = 100,
seed_genres: string[] = [],
seed_artists: string[] = [],
features: any = {}
): Promise<Track[]> {
getRunningTrack(): Promise<Track>
getSpotifyRecentlyPlayedTracks(limit: number): Promise<Track[]>
getSpotifyPlayerContext(): Promise<PlayerContext>
getSpotifyTrackById(
id: string,
includeFullArtistData: boolean = false,
includeAudioFeaturesData: boolean = false,
includeGenre: boolean = false
): Promise<Track>
export async function getSpotifyTracks(
ids: string[],
includeFullArtistData: boolean = false,
includeAudioFeaturesData: boolean = false,
includeGenre: boolean = false
): Promise<Track[]>
getTrack(player: PlayerName): Promise<Track>
getTracksByPlaylistName(
player: PlayerName,
playListName: string
): Promise<Track[]>
getSpotifyLikedSongs(
qsOptions: any = {}
): Promise<Track[]>
getSavedTracks(player: PlayerName, qsOptions: any = {}): Promise<Track[]>
getSpotifyPlaylist(playlist_id: string): Promise<PlaylistItem>
getPlaylistTracks(
player: PlayerName,
playlist_id: string,
qsOptions: any = {}
): Promise<CodyResponse>
playSpotifyPlaylist(
playlistId: string,
startingTrackId: string = "",
deviceId: string = ""
)
playTrackInContext(player: PlayerName, params: any[])
playItunesTrackNumberInPlaylist(
playlistName: string,
trackNumber: number
)
quitMacPlayer(player: PlayerName)
playTrackInLibrary(player: PlayerName, params: any[])
playSpotifyDevice(device_id: string)
transferSpotifyDevice(device_id: string, play: boolean)
getUserProfile(): Promise<SpotifyUser>
spotifyAuthState(): Promise<SpotifyAuthState>
play(player: PlayerName, options: any = {})
playSpotifyTrack(trackId: string, deviceId: string = "")
playTrack(PlayerName: PlayerName, trackId: any)
pause(player: PlayerName, options: any = {})
playPause(player: PlayerName)
next(player: PlayerName, options: any = {})
previous(player: PlayerName, options: any = {})
setRepeat(player: PlayerName, repeat: boolean)
setShuffle(player: PlayerName, shuffle: boolean)
isShuffling(player: PlayerName)
isRepeating(player: PlayerName)
setVolume(player: PlayerName, volume: number)
volumeUp(player: PlayerName)
volumeDown(player: PlayerName)
mute(player: PlayerName)
unmute(player: PlayerName)
setItunesLoved(loved: boolean)
saveToSpotifyLiked(trackIds: string[]): Promise<CodyResponse>
removeFromSpotifyLiked(
trackIds: string[]
): Promise<CodyResponse>
getPlaylists(
player: PlayerName,
qsOptions: any = {}
): Promise<PlaylistItem[]>
getPlaylistNames(
player: PlayerName,
qsOptions: any = {}
): Promise<string[]>
launchPlayer(playerName: PlayerName, options: any = {})
launchAndPlaySpotifyTrack(trackId: string = "", playlistId: string = "", playerName: string = PlayerName.SpotifyWeb)
playSpotifyMacDesktopTrack(trackId: string = "", playlistId: string = "")
getSpotifyDevices(): Promise<PlayerDevice[]>
getGenre(
artist: string,
songName: string = "",
spotifyArtistId: string = ""
): Promise<string>
getSpotifyGenre(artist: string): Promise<string>
getHighestFrequencySpotifyGenre(genreList: string[]): string
getSpotifyGenreByArtistId(spotifyArtistId: string): Promise<string> {
getTopSpotifyTracks(): Promise<Track[]>
getSpotifyAudioFeatures(
ids: string[]
): Promise<SpotifyAudioFeature[]>
createPlaylist(
name: string,
isPublic: boolean,
description: string = ""
)
deletePlaylist(playlist_id: string): CodyResponse
followPlaylist(playlist_id: string): CodyResponse
replacePlaylistTracks(
playlist_id: string,
track_ids: string[]
)
addTracksToPlaylist(
playlist_id: string,
tracks: string[],
position: number = 0
)
removeTracksFromPlaylist(
playlist_id: string,
tracks: string[]
)
requiresSpotifyAccessInfo(): boolean
getPlayerState(player: PlayerName): Promise<Track>
getCurrentlyRunningTrackState(): Promise<Track>
getState(player: PlayerName): Promise<Track>
startSpotifyIfNotRunning()
startItunesIfNotRunning()
isRunning(player: PlayerName): Promise<boolean>
repeatOn(player: PlayerName)
repeatOff(player: PlayerName)
unMute(player: PlayerName)
setCredentials(credentials: any)
getAccessToken()
Contributors
License