cody-music
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -1,215 +0,2 @@ | ||
import { PlayerName, TrackState, PlayerDevice, SpotifyAudioFeature } from "./lib/models"; | ||
/** | ||
* Set Credentials (currently only supports Spotify) | ||
* Accepted credentials: clientId, clientSecret, refreshToken, accessToken | ||
* @param credentials | ||
*/ | ||
export declare function setCredentials(credentials: any): void; | ||
/** | ||
* Get the accessToken provided via through the setCredentials api | ||
* @returns {string} the access token string | ||
*/ | ||
export declare function getAccessToken(): any; | ||
/** | ||
* Checks if the Spotify desktop or web player is running or not | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export declare function isSpotifyRunning(): Promise<boolean>; | ||
/** | ||
* Checks if the iTunes desktop player is running or not | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export declare function isItunesRunning(): Promise<boolean>; | ||
/** | ||
* Checks if one of the specified players is running | ||
* @param player {spotify|spotify-web|itunes} | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export declare function isPlayerRunning(player: PlayerName): Promise<boolean>; | ||
/** | ||
* Returns the currently running track info (player and track). | ||
* This only supports returning the state for itunes and spotify desktop | ||
* on Mac and spotify desktop on windows. | ||
**/ | ||
export declare function getRunningPlayerState(): Promise<TrackState>; | ||
/** | ||
* Returns the player state and track of a given player {spotify|spotify-web|itunes} | ||
* - Spotify does not return a "genre" | ||
* - duration is in milliseconds | ||
* @param player {spotify|spotif-web|itunes} | ||
* @returns {artist, album, genre, disc_number, duration, played_count, track_number, id, name, state} | ||
*/ | ||
export declare function getPlayerState(player: PlayerName): Promise<TrackState>; | ||
/** | ||
* Returns the tracks that are found by the given playlist name | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param playListName | ||
*/ | ||
export declare function getTracksByPlaylistName(player: PlayerName, playListName: string): Promise<any>; | ||
/** | ||
* Plays a specific track on the Spotify or iTunes desktop | ||
* @param player | ||
* @param params (e.g. ["spotify:track:0R8P9KfGJCDULmlEoBagcO", "spotify:album:6ZG5lRT77aJ3btmArcykra"] | ||
*/ | ||
export declare function playTrackInContext(player: PlayerName, params: any[]): Promise<any>; | ||
/** | ||
* Initiate and play the specified Spotify device | ||
* @param device_id {string} | ||
* @param play {boolean} | ||
*/ | ||
export declare function playSpotifyDevice(device_id: string): Promise<any>; | ||
/** | ||
* Initiate the play command for a specific player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function play(player: PlayerName, options: any): Promise<any>; | ||
/** | ||
* Initiate the play command for a given trackId for a specific player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param trackId {string} | ||
*/ | ||
export declare function playTrack(player: PlayerName, trackId: string): Promise<any>; | ||
/** | ||
* Initiate the pause command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function pause(player: PlayerName, options: any): Promise<any>; | ||
/** | ||
* Initiate the play/pause command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function playPause(player: PlayerName): Promise<any>; | ||
/** | ||
* Initiate the next command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function next(player: PlayerName, options: any): Promise<any>; | ||
/** | ||
* Initiate the previous command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function previous(player: PlayerName, options: any): Promise<any>; | ||
/** | ||
* Turn on/off repeat for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function setRepeat(player: PlayerName, repeat: boolean): Promise<any>; | ||
/** | ||
* Turn on/off shuffling for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function setShuffle(player: PlayerName, shuffle: boolean): Promise<any>; | ||
/** | ||
* Return whether shuffling is on or not | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function isShuffling(player: PlayerName): Promise<any>; | ||
/** | ||
* Returns whether the player is on repeat or not | ||
* - spotify returns true or false, and itunes returns "off", "one", "all" | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function isRepeating(player: PlayerName): Promise<any>; | ||
/** | ||
* Update the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
export declare function setVolume(player: PlayerName, volume: number): Promise<any>; | ||
/** | ||
* Increments the players volume by a number | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
export declare function volumeUp(player: PlayerName, volume: number): Promise<any>; | ||
/** | ||
* Decrements the players volume by a number | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
export declare function volumeDown(player: PlayerName, volume: number): Promise<any>; | ||
/** | ||
* Mutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function mute(player: PlayerName): Promise<any>; | ||
/** | ||
* Unmutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function unmute(player: PlayerName): Promise<any>; | ||
/** | ||
* Unmutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function setItunesLoved(loved: boolean): Promise<any>; | ||
/** | ||
* Get the full list of the playlist names for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export declare function getPlaylistNames(player: PlayerName): Promise<any>; | ||
/** | ||
* Launches a player device | ||
* @param playerName {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export declare function launchPlayer(playerName: PlayerName, options: any): Promise<any>; | ||
/** | ||
* Returns available Spotify devices | ||
* @returns {Promise<PlayerDevice[]>} | ||
*/ | ||
export declare function getSpotifyDevices(): Promise<PlayerDevice[]>; | ||
/** | ||
* Returns the genre for a provided arguments | ||
* @param artist {string} is required | ||
* @param songName {string} is optional | ||
*/ | ||
export declare function getGenre(artist: string, songName?: string): Promise<string>; | ||
/** | ||
* Returns the spotify genre for a provided arguments | ||
* @param artist {string} is required | ||
*/ | ||
export declare function getSpotifyGenre(artist: string): Promise<string>; | ||
/** | ||
* Returns the audio features of the given track IDs | ||
* @param ids these are the track ids (sans spotify:track) | ||
*/ | ||
export declare function getSpotifyAudioFeatures(ids: string[]): Promise<SpotifyAudioFeature[]>; | ||
/** | ||
* Deprecated - use "getRunningPlayerState()" instead | ||
*/ | ||
export declare function getCurrentlyRunningTrackState(): Promise<TrackState>; | ||
/** | ||
* deprecated, please use "getPlayerState" | ||
*/ | ||
export declare function getState(player: PlayerName): Promise<TrackState>; | ||
/** | ||
* deprecated, please use "launchPlayer('spotify')" | ||
**/ | ||
export declare function startSpotifyIfNotRunning(): Promise<any>; | ||
/** | ||
* deprecated, please use "launchPlayer('itunes')" | ||
*/ | ||
export declare function startItunesIfNotRunning(): Promise<any>; | ||
/** | ||
* deprecated, please use "isSpotifyRunning" or "isItunesRunning" | ||
*/ | ||
export declare function isRunning(player: PlayerName): Promise<boolean>; | ||
/** | ||
* deprecated, pluse use "setRepat(player, repeat)" | ||
*/ | ||
export declare function repeatOn(player: PlayerName): Promise<any>; | ||
/** | ||
* deprecated, pluse use "setRepat(player, repeat)" | ||
*/ | ||
export declare function repeatOff(player: PlayerName): Promise<any>; | ||
/** | ||
* deprecated, pluse use "unmute(player)" | ||
*/ | ||
export declare function unMute(player: PlayerName): Promise<any>; | ||
export * from "./lib/apis"; | ||
export { PlayerName, PlayerDevice, SpotifyAudioFeature } from "./lib/models"; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const controller_1 = require("./lib/controller"); | ||
const models_1 = require("./lib/models"); | ||
const playerstate_1 = require("./lib/playerstate"); | ||
const store_1 = require("./lib/store"); | ||
const util_1 = require("./lib/util"); | ||
// get the instances | ||
const musicCtr = controller_1.MusicController.getInstance(); | ||
const musicPlayerCtr = playerstate_1.MusicPlayerState.getInstance(); | ||
const musicStore = store_1.MusicStore.getInstance(); | ||
const musicUtil = new util_1.MusicUtil(); | ||
/** | ||
* Set Credentials (currently only supports Spotify) | ||
* Accepted credentials: clientId, clientSecret, refreshToken, accessToken | ||
* @param credentials | ||
*/ | ||
function setCredentials(credentials) { | ||
musicStore.setCredentials(credentials); | ||
} | ||
exports.setCredentials = setCredentials; | ||
/** | ||
* Get the accessToken provided via through the setCredentials api | ||
* @returns {string} the access token string | ||
*/ | ||
function getAccessToken() { | ||
return musicStore.credentialByKey("accessToken"); | ||
} | ||
exports.getAccessToken = getAccessToken; | ||
/** | ||
* Checks if the Spotify desktop or web player is running or not | ||
* @returns {Promise<boolean>} | ||
*/ | ||
async function isSpotifyRunning() { | ||
let running = await isPlayerRunning(models_1.PlayerName.SpotifyDesktop); | ||
if (!running) { | ||
// check the web | ||
running = await musicPlayerCtr.isSpotifyWebRunning(); | ||
} | ||
return running; | ||
} | ||
exports.isSpotifyRunning = isSpotifyRunning; | ||
/** | ||
* Checks if the iTunes desktop player is running or not | ||
* @returns {Promise<boolean>} | ||
*/ | ||
function isItunesRunning() { | ||
return isPlayerRunning(models_1.PlayerName.ItunesDesktop); | ||
} | ||
exports.isItunesRunning = isItunesRunning; | ||
/** | ||
* Checks if one of the specified players is running | ||
* @param player {spotify|spotify-web|itunes} | ||
* @returns {Promise<boolean>} | ||
*/ | ||
async function isPlayerRunning(player) { | ||
if (player === models_1.PlayerName.SpotifyWeb) { | ||
return await musicPlayerCtr.isSpotifyWebRunning(); | ||
} | ||
else { | ||
return await musicCtr.isMusicPlayerActive(player); | ||
} | ||
} | ||
exports.isPlayerRunning = isPlayerRunning; | ||
/** | ||
* Returns the currently running track info (player and track). | ||
* This only supports returning the state for itunes and spotify desktop | ||
* on Mac and spotify desktop on windows. | ||
**/ | ||
async function getRunningPlayerState() { | ||
if (await musicCtr.isMusicPlayerActive(models_1.PlayerName.SpotifyDesktop)) { | ||
return getPlayerState(models_1.PlayerName.SpotifyDesktop); | ||
} | ||
else if (await musicCtr.isMusicPlayerActive(models_1.PlayerName.ItunesDesktop)) { | ||
return getPlayerState(models_1.PlayerName.ItunesDesktop); | ||
} | ||
return getPlayerState(models_1.PlayerName.SpotifyWeb); | ||
} | ||
exports.getRunningPlayerState = getRunningPlayerState; | ||
/** | ||
* Returns the player state and track of a given player {spotify|spotify-web|itunes} | ||
* - Spotify does not return a "genre" | ||
* - duration is in milliseconds | ||
* @param player {spotify|spotif-web|itunes} | ||
* @returns {artist, album, genre, disc_number, duration, played_count, track_number, id, name, state} | ||
*/ | ||
async function getPlayerState(player) { | ||
if (player === models_1.PlayerName.SpotifyWeb) { | ||
return await musicPlayerCtr.getSpotifyWebCurrentTrack(); | ||
} | ||
const state = await musicCtr.run(player, "state"); | ||
if (state) { | ||
return JSON.parse(state); | ||
} | ||
return new models_1.TrackState(); | ||
} | ||
exports.getPlayerState = getPlayerState; | ||
/** | ||
* Returns the tracks that are found by the given playlist name | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param playListName | ||
*/ | ||
async function getTracksByPlaylistName(player, playListName) { | ||
const params = null; | ||
const argv = [playListName]; | ||
const result = await musicCtr.run(player, "playlistTracksOfPlaylist", params, argv); | ||
let jsonResult = {}; | ||
if (result) { | ||
let jsonList = result.split("[TRACK_END],"); | ||
if (jsonList && jsonList.length > 0) { | ||
for (let i = 0; i < jsonList.length; i++) { | ||
let jsonStr = jsonList[i].trim(); | ||
jsonResult[i] = JSON.parse(jsonStr); | ||
} | ||
} | ||
} | ||
return jsonResult; | ||
} | ||
exports.getTracksByPlaylistName = getTracksByPlaylistName; | ||
/** | ||
* Plays a specific track on the Spotify or iTunes desktop | ||
* @param player | ||
* @param params (e.g. ["spotify:track:0R8P9KfGJCDULmlEoBagcO", "spotify:album:6ZG5lRT77aJ3btmArcykra"] | ||
*/ | ||
function playTrackInContext(player, params) { | ||
return musicCtr.playTrackInContext(player, params); | ||
} | ||
exports.playTrackInContext = playTrackInContext; | ||
/** | ||
* Initiate and play the specified Spotify device | ||
* @param device_id {string} | ||
* @param play {boolean} | ||
*/ | ||
function playSpotifyDevice(device_id) { | ||
return musicCtr.playSpotifyDevice(device_id); | ||
} | ||
exports.playSpotifyDevice = playSpotifyDevice; | ||
/** | ||
* Initiate the play command for a specific player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function play(player, options) { | ||
if (player === models_1.PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebPlay(options); | ||
} | ||
else { | ||
return musicCtr.run(player, "play"); | ||
} | ||
} | ||
exports.play = play; | ||
/** | ||
* Initiate the play command for a given trackId for a specific player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param trackId {string} | ||
*/ | ||
function playTrack(player, trackId) { | ||
return musicCtr.playTrack(player, trackId); | ||
} | ||
exports.playTrack = playTrack; | ||
/** | ||
* Initiate the pause command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function pause(player, options) { | ||
if (player === models_1.PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebPause(options); | ||
} | ||
else { | ||
return musicCtr.run(player, "pause"); | ||
} | ||
} | ||
exports.pause = pause; | ||
/** | ||
* Initiate the play/pause command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function playPause(player) { | ||
return musicCtr.run(player, "playPause"); | ||
} | ||
exports.playPause = playPause; | ||
/** | ||
* Initiate the next command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function next(player, options) { | ||
if (player === models_1.PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebNext(options); | ||
} | ||
else { | ||
return musicCtr.run(player, "next"); | ||
} | ||
} | ||
exports.next = next; | ||
/** | ||
* Initiate the previous command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function previous(player, options) { | ||
if (player === models_1.PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebPrevious(options); | ||
} | ||
else { | ||
return musicCtr.run(player, "previous"); | ||
} | ||
} | ||
exports.previous = previous; | ||
/** | ||
* Turn on/off repeat for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function setRepeat(player, repeat) { | ||
let repeatParam = repeat ? "repeatOn" : "repeatOff"; | ||
return musicCtr.run(player, repeatParam); | ||
} | ||
exports.setRepeat = setRepeat; | ||
/** | ||
* Turn on/off shuffling for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
function setShuffle(player, shuffle) { | ||
let shuffleParam = shuffle ? ["true"] : ["false"]; | ||
return musicCtr.run(player, "setShuffling", shuffleParam); | ||
} | ||
exports.setShuffle = setShuffle; | ||
/** | ||
* Return whether shuffling is on or not | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
async function isShuffling(player) { | ||
const val = await musicCtr.run(player, "isShuffling"); | ||
if (musicUtil.isBooleanString(val)) { | ||
return JSON.parse(val); | ||
} | ||
return val; | ||
} | ||
exports.isShuffling = isShuffling; | ||
/** | ||
* Returns whether the player is on repeat or not | ||
* - spotify returns true or false, and itunes returns "off", "one", "all" | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
async function isRepeating(player) { | ||
let val = await musicCtr.run(player, "isRepeating"); | ||
if (musicUtil.isBooleanString(val)) { | ||
return JSON.parse(val); | ||
} | ||
return val; | ||
} | ||
exports.isRepeating = isRepeating; | ||
/** | ||
* Update the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
function setVolume(player, volume) { | ||
return musicCtr.setVolume(player, volume); | ||
} | ||
exports.setVolume = setVolume; | ||
/** | ||
* Increments the players volume by a number | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
function volumeUp(player, volume) { | ||
return musicCtr.run(player, "volumeUp"); | ||
} | ||
exports.volumeUp = volumeUp; | ||
/** | ||
* Decrements the players volume by a number | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
function volumeDown(player, volume) { | ||
return musicCtr.run(player, "volumeDown"); | ||
} | ||
exports.volumeDown = volumeDown; | ||
/** | ||
* Mutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
function mute(player) { | ||
return musicCtr.run(player, "mute"); | ||
} | ||
exports.mute = mute; | ||
/** | ||
* Unmutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
function unmute(player) { | ||
return musicCtr.run(player, "unMute"); | ||
} | ||
exports.unmute = unmute; | ||
/** | ||
* Unmutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
function setItunesLoved(loved) { | ||
return musicCtr.setItunesLoved(loved); | ||
} | ||
exports.setItunesLoved = setItunesLoved; | ||
/** | ||
* Get the full list of the playlist names for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
async function getPlaylistNames(player) { | ||
let result = await musicCtr.run(player, "playlistNames"); | ||
// turn this into a string list | ||
if (result) { | ||
result = result.split(","); | ||
// now trim | ||
result = result.map((name) => { | ||
return name.trim(); | ||
}); | ||
} | ||
return result; | ||
} | ||
exports.getPlaylistNames = getPlaylistNames; | ||
/** | ||
* Launches a player device | ||
* @param playerName {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
function launchPlayer(playerName, options) { | ||
if (playerName === models_1.PlayerName.SpotifyWeb) { | ||
return musicPlayerCtr.launchWebPlayer(options); | ||
} | ||
else { | ||
return musicCtr.startPlayer(playerName); | ||
} | ||
} | ||
exports.launchPlayer = launchPlayer; | ||
/** | ||
* Returns available Spotify devices | ||
* @returns {Promise<PlayerDevice[]>} | ||
*/ | ||
function getSpotifyDevices() { | ||
return musicPlayerCtr.getSpotifyDevices(); | ||
} | ||
exports.getSpotifyDevices = getSpotifyDevices; | ||
/** | ||
* Returns the genre for a provided arguments | ||
* @param artist {string} is required | ||
* @param songName {string} is optional | ||
*/ | ||
function getGenre(artist, songName = "") { | ||
return musicCtr.getGenre(artist, songName); | ||
} | ||
exports.getGenre = getGenre; | ||
/** | ||
* Returns the spotify genre for a provided arguments | ||
* @param artist {string} is required | ||
*/ | ||
function getSpotifyGenre(artist) { | ||
return musicCtr.getGenreFromSpotify(artist); | ||
} | ||
exports.getSpotifyGenre = getSpotifyGenre; | ||
/** | ||
* Returns the audio features of the given track IDs | ||
* @param ids these are the track ids (sans spotify:track) | ||
*/ | ||
function getSpotifyAudioFeatures(ids) { | ||
return musicPlayerCtr.getSpotifyAudioFeatures(ids); | ||
} | ||
exports.getSpotifyAudioFeatures = getSpotifyAudioFeatures; | ||
/** | ||
* Deprecated - use "getRunningPlayerState()" instead | ||
*/ | ||
function getCurrentlyRunningTrackState() { | ||
return musicPlayerCtr.getCurrentlyRunningTrackState(); | ||
} | ||
exports.getCurrentlyRunningTrackState = getCurrentlyRunningTrackState; | ||
/** | ||
* deprecated, please use "getPlayerState" | ||
*/ | ||
function getState(player) { | ||
return getPlayerState(player); | ||
} | ||
exports.getState = getState; | ||
/** | ||
* deprecated, please use "launchPlayer('spotify')" | ||
**/ | ||
function startSpotifyIfNotRunning() { | ||
return musicCtr.launchApp(models_1.PlayerName.SpotifyDesktop); | ||
} | ||
exports.startSpotifyIfNotRunning = startSpotifyIfNotRunning; | ||
/** | ||
* deprecated, please use "launchPlayer('itunes')" | ||
*/ | ||
function startItunesIfNotRunning() { | ||
return musicCtr.launchApp(models_1.PlayerName.ItunesDesktop); | ||
} | ||
exports.startItunesIfNotRunning = startItunesIfNotRunning; | ||
/** | ||
* deprecated, please use "isSpotifyRunning" or "isItunesRunning" | ||
*/ | ||
function isRunning(player) { | ||
return isPlayerRunning(player); | ||
} | ||
exports.isRunning = isRunning; | ||
/** | ||
* deprecated, pluse use "setRepat(player, repeat)" | ||
*/ | ||
function repeatOn(player) { | ||
return setRepeat(player, true); | ||
} | ||
exports.repeatOn = repeatOn; | ||
/** | ||
* deprecated, pluse use "setRepat(player, repeat)" | ||
*/ | ||
function repeatOff(player) { | ||
return setRepeat(player, false); | ||
} | ||
exports.repeatOff = repeatOff; | ||
/** | ||
* deprecated, pluse use "unmute(player)" | ||
*/ | ||
function unMute(player) { | ||
return unmute(player); | ||
} | ||
exports.unMute = unMute; | ||
__export(require("./lib/apis")); | ||
var models_1 = require("./lib/models"); | ||
exports.PlayerName = models_1.PlayerName; | ||
exports.PlayerDevice = models_1.PlayerDevice; | ||
exports.SpotifyAudioFeature = models_1.SpotifyAudioFeature; |
@@ -14,3 +14,2 @@ import { PlayerName } from "./models"; | ||
execScript(player: string, scriptName: string, params?: any, argv?: any): Promise<any>; | ||
playTrack(player: string, trackId: string): Promise<any>; | ||
run(player: PlayerName, scriptName: string, params?: any, argv?: any): Promise<any>; | ||
@@ -17,0 +16,0 @@ setVolume(player: string, volume: number): Promise<any>; |
@@ -84,3 +84,3 @@ "use strict"; | ||
let result = await musicUtil.execCmd(command); | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -94,3 +94,3 @@ } | ||
let result = await musicUtil.execCmd(command); | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -140,3 +140,3 @@ } | ||
let result = await musicUtil.execCmd(command); | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -146,17 +146,2 @@ } | ||
} | ||
playTrack(player, trackId) { | ||
let params = null; | ||
if (player === "Spotify") { | ||
params = [`"${trackId}"`]; | ||
} | ||
else { | ||
params = [`${trackId}`]; | ||
} | ||
return this.execScript(player, "playTrack", params).then(result => { | ||
if (result === null || result === undefined) { | ||
result = "ok"; | ||
} | ||
return result; | ||
}); | ||
} | ||
async run(player, scriptName, params = null, argv = null) { | ||
@@ -221,3 +206,3 @@ player = musicUtil.getPlayerName(player); | ||
return this.execScript(player, scriptName, params, argv).then(result => { | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -231,3 +216,3 @@ } | ||
return this.execScript(player, "setVolume", [volume]).then(result => { | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -241,7 +226,12 @@ } | ||
loved | ||
]).then(result => { | ||
if (result === null || result === undefined) { | ||
]) | ||
.then(result => { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
} | ||
return result; | ||
}) | ||
.catch(err => { | ||
console.log("Error updating itunes track loved status, error: ", err.message); | ||
return false; | ||
}); | ||
@@ -251,3 +241,3 @@ } | ||
return this.execScript(player, "playTrackInContext", params).then(result => { | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -254,0 +244,0 @@ } |
@@ -19,12 +19,2 @@ export declare enum PlayerType { | ||
} | ||
export declare class TrackState { | ||
/** | ||
* type of the player | ||
*/ | ||
type: PlayerType; | ||
/** | ||
* The track data | ||
*/ | ||
track: Track; | ||
} | ||
export declare class Track { | ||
@@ -35,2 +25,3 @@ artist: string; | ||
disc_number: number; | ||
disk_number: number; | ||
duration: number; | ||
@@ -48,2 +39,5 @@ duration_ms: number; | ||
type: string; | ||
playerType: PlayerType; | ||
loved: boolean; | ||
volume: number; | ||
status: TrackStatus; | ||
@@ -50,0 +44,0 @@ } |
@@ -24,15 +24,2 @@ "use strict"; | ||
})(TrackStatus = exports.TrackStatus || (exports.TrackStatus = {})); | ||
class TrackState { | ||
constructor() { | ||
/** | ||
* type of the player | ||
*/ | ||
this.type = PlayerType.NotAssigned; | ||
/** | ||
* The track data | ||
*/ | ||
this.track = new Track(); | ||
} | ||
} | ||
exports.TrackState = TrackState; | ||
// {artist, album, genre, disc_number, duration, played_count, track_number, id, name, state} | ||
@@ -45,2 +32,3 @@ class Track { | ||
this.disc_number = 0; | ||
this.disk_number = 0; | ||
this.duration = 0; | ||
@@ -60,2 +48,5 @@ this.duration_ms = 0; | ||
this.type = ""; | ||
this.playerType = PlayerType.NotAssigned; | ||
this.loved = false; | ||
this.volume = 0; | ||
this.status = TrackStatus.NotAssigned; | ||
@@ -62,0 +53,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { TrackState, PlayerDevice, PlayerContext, SpotifyAudioFeature } from "./models"; | ||
import { PlayerDevice, Track, PlayerContext, SpotifyAudioFeature } from "./models"; | ||
export declare class MusicPlayerState { | ||
@@ -6,3 +6,3 @@ private static instance; | ||
static getInstance(): MusicPlayerState; | ||
getCurrentlyRunningTrackState(): Promise<TrackState>; | ||
getCurrentlyRunningTrackState(): Promise<Track>; | ||
isWindowsSpotifyRunning(): Promise<boolean>; | ||
@@ -28,3 +28,3 @@ isSpotifyDesktopRunning(): Promise<boolean>; | ||
getSpotifyDevices(): Promise<PlayerDevice[]>; | ||
getDesktopTrackState(): Promise<TrackState>; | ||
getDesktopTrackState(): Promise<Track>; | ||
/** | ||
@@ -49,5 +49,5 @@ * returns i.e. | ||
getWindowsSpotifyTrackInfo(): Promise<any>; | ||
getSpotifyWebCurrentTrack(): Promise<TrackState>; | ||
getSpotifyWebCurrentTrack(): Promise<Track>; | ||
getSpotifyWebPlayerState(): Promise<PlayerContext>; | ||
launchWebPlayer(options: any): Promise<any>; | ||
} |
@@ -23,3 +23,3 @@ "use strict"; | ||
async getCurrentlyRunningTrackState() { | ||
let trackState = new models_1.TrackState(); | ||
let trackState = new models_1.Track(); | ||
let spotifyDesktopRunning = await this.isSpotifyDesktopRunning(); | ||
@@ -154,6 +154,5 @@ let itunesDesktopRunning = await this.isItunesDesktopRunning(); | ||
async getDesktopTrackState() { | ||
let trackState = new models_1.TrackState(); | ||
let playingTrack = new models_1.Track(); | ||
let pausedTrack = new models_1.Track(); | ||
let pausedType = models_1.PlayerType.NotAssigned; | ||
let outgoingTrack; | ||
let spotifyTrack; | ||
let itunesTrack; | ||
if (musicUtil.isMac()) { | ||
@@ -165,18 +164,8 @@ const spotifyRunning = await musicCtr.isMusicPlayerActive(models_1.PlayerName.SpotifyDesktop); | ||
if (state) { | ||
playingTrack = JSON.parse(state); | ||
spotifyTrack = JSON.parse(state); | ||
} | ||
if (playingTrack) { | ||
playingTrack.type = models_1.PlayerName.SpotifyDesktop; | ||
if (spotifyTrack) { | ||
spotifyTrack.type = models_1.PlayerName.SpotifyDesktop; | ||
spotifyTrack.playerType = models_1.PlayerType.MacSpotifyDesktop; | ||
} | ||
if (playingTrack && playingTrack.state === "playing") { | ||
trackState = { | ||
type: models_1.PlayerType.MacSpotifyDesktop, | ||
track: playingTrack | ||
}; | ||
} | ||
else if (playingTrack) { | ||
// save this one if itunes isn't running | ||
pausedTrack = playingTrack; | ||
pausedType = models_1.PlayerType.MacSpotifyDesktop; | ||
} | ||
} | ||
@@ -188,21 +177,20 @@ // next itunes | ||
if (state) { | ||
playingTrack = JSON.parse(state); | ||
itunesTrack = JSON.parse(state); | ||
} | ||
if (playingTrack) { | ||
playingTrack.type = models_1.PlayerName.ItunesDesktop; | ||
if (itunesTrack) { | ||
itunesTrack.type = models_1.PlayerName.ItunesDesktop; | ||
itunesTrack.playerType = models_1.PlayerType.MacItunesDesktop; | ||
} | ||
if (playingTrack && playingTrack.state === "playing") { | ||
trackState = { | ||
type: models_1.PlayerType.MacItunesDesktop, | ||
track: playingTrack | ||
}; | ||
} | ||
if (spotifyTrack && itunesTrack) { | ||
if (itunesTrack.state !== "playing") { | ||
outgoingTrack = spotifyTrack; | ||
} | ||
else if (!pausedTrack && playingTrack) { | ||
pausedTrack = playingTrack; | ||
pausedType = models_1.PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
if (pausedTrack) { | ||
trackState = { type: pausedType, track: pausedTrack }; | ||
else if (spotifyTrack) { | ||
outgoingTrack = spotifyTrack; | ||
} | ||
else { | ||
outgoingTrack = itunesTrack; | ||
} | ||
} | ||
@@ -213,9 +201,6 @@ else if (musicUtil.isWindows()) { | ||
if (winSpotifyRunning) { | ||
playingTrack = await this.getWindowsSpotifyTrackInfo(); | ||
if (playingTrack) { | ||
playingTrack.type = models_1.PlayerName.SpotifyDesktop; | ||
trackState = { | ||
type: models_1.PlayerType.MacSpotifyDesktop, | ||
track: playingTrack | ||
}; | ||
outgoingTrack = await this.getWindowsSpotifyTrackInfo(); | ||
if (outgoingTrack) { | ||
outgoingTrack.type = models_1.PlayerName.SpotifyDesktop; | ||
outgoingTrack.playerType = models_1.PlayerType.MacSpotifyDesktop; | ||
} | ||
@@ -225,22 +210,22 @@ } | ||
// make sure it's not an advertisement | ||
if (trackState && !musicUtil.isEmptyObj(trackState.track)) { | ||
if (outgoingTrack && !musicUtil.isEmptyObj(outgoingTrack)) { | ||
// "artist":"","album":"","id":"spotify:ad:000000012c603a6600000020316a17a1" | ||
if (trackState.type === models_1.PlayerType.MacSpotifyDesktop && | ||
trackState.track.id.includes("spotify:ad:")) { | ||
if (outgoingTrack.type === models_1.PlayerType.MacSpotifyDesktop && | ||
outgoingTrack.id.includes("spotify:ad:")) { | ||
// it's a spotify ad | ||
trackState.track.status = models_1.TrackStatus.Advertisement; | ||
outgoingTrack.status = models_1.TrackStatus.Advertisement; | ||
} | ||
else if (!trackState.track.artist && !trackState.track.album) { | ||
else if (!outgoingTrack.artist && !outgoingTrack.album) { | ||
// not enough info to send | ||
trackState.track.status = models_1.TrackStatus.NotAssigned; | ||
outgoingTrack.status = models_1.TrackStatus.NotAssigned; | ||
} | ||
} | ||
// include common attributes | ||
if (trackState && | ||
!musicUtil.isEmptyObj(trackState.track) && | ||
trackState.track.duration) { | ||
if (outgoingTrack && | ||
!musicUtil.isEmptyObj(outgoingTrack) && | ||
outgoingTrack.duration) { | ||
// create the attributes | ||
trackState.track["duration_ms"] = trackState.track.duration; | ||
outgoingTrack["duration_ms"] = outgoingTrack.duration; | ||
} | ||
return trackState; | ||
return outgoingTrack; | ||
} | ||
@@ -304,3 +289,3 @@ /** | ||
async getSpotifyWebCurrentTrack() { | ||
let trackState = new models_1.TrackState(); | ||
let trackState = new models_1.Track(); | ||
let api = "/v1/me/player/currently-playing"; | ||
@@ -323,4 +308,4 @@ let response = await musicClient.spotifyApiGet(api); | ||
musicUtil.extractAristFromSpotifyTrack(track); | ||
trackState.track = track; | ||
trackState.type = models_1.PlayerType.WebSpotify; | ||
trackState = track; | ||
trackState.playerType = models_1.PlayerType.WebSpotify; | ||
return trackState; | ||
@@ -327,0 +312,0 @@ } |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const expect = require("chai").expect; | ||
const CodyMusic = require("../../dist/index.js"); | ||
const CodyMusic = __importStar(require("../../index")); | ||
const util_1 = require("../../lib/util"); | ||
const models_1 = require("../../lib/models"); | ||
const controller_1 = require("../../lib/controller"); | ||
@@ -21,6 +27,6 @@ const musicUtil = new util_1.MusicUtil(); | ||
musicCtr | ||
.quitApp(models_1.PlayerName.SpotifyDesktop) | ||
.quitApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then((result) => { | ||
musicCtr | ||
.quitApp(models_1.PlayerName.ItunesDesktop) | ||
.quitApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then((result) => { | ||
@@ -35,3 +41,3 @@ done(); | ||
musicCtr | ||
.quitApp(models_1.PlayerName.ItunesDesktop) | ||
.quitApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then((result) => { | ||
@@ -48,3 +54,3 @@ done(); | ||
musicCtr | ||
.quitApp(models_1.PlayerName.SpotifyDesktop) | ||
.quitApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then((result) => { | ||
@@ -58,6 +64,6 @@ done(); | ||
it("Should Show The Playlist Names AND Show The Tracks Of A Playlist", done => { | ||
CodyMusic.getPlaylistNames("iTunes").then((names) => { | ||
expect(names).to.not.equal(0); | ||
CodyMusic.getPlaylistNames(CodyMusic.PlayerName.ItunesDesktop).then((names) => { | ||
expect(names.length).to.not.equal(0); | ||
// get the last name in the list and get the tracks | ||
CodyMusic.getTracksByPlaylistName(models_1.PlayerName.ItunesDesktop, names[names.length - 1]).then((result) => { | ||
CodyMusic.getTracksByPlaylistName(CodyMusic.PlayerName.ItunesDesktop, names[names.length - 1]).then((result) => { | ||
expect(result.length).to.not.equal(0); | ||
@@ -69,12 +75,14 @@ done(); | ||
it("Should Set An Itunes Song's Love State", done => { | ||
musicCtr.launchApp(models_1.PlayerName.ItunesDesktop).then(async () => { | ||
musicCtr | ||
.launchApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.setVolume("iTunes", 5); | ||
await CodyMusic.play("iTunes"); | ||
await CodyMusic.setVolume(CodyMusic.PlayerName.ItunesDesktop, 5); | ||
await CodyMusic.play(CodyMusic.PlayerName.ItunesDesktop); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("iTunes"); | ||
let result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
let loved = result.loved; | ||
CodyMusic.setItunesLoved(!loved).then(async (result) => { | ||
// get the state again | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.loved).to.equal(!loved); | ||
@@ -87,5 +95,5 @@ done(); | ||
// play a bad track number | ||
CodyMusic.playTrack("iTunes", 1000000000).then((result) => { | ||
CodyMusic.playTrack(CodyMusic.PlayerName.ItunesDesktop, 1000000000).then((result) => { | ||
expect(result.error).to.not.equal(null); | ||
musicCtr.quitApp(models_1.PlayerName.ItunesDesktop).then(() => { | ||
musicCtr.quitApp(CodyMusic.PlayerName.ItunesDesktop).then(() => { | ||
done(); | ||
@@ -96,3 +104,3 @@ }); | ||
it("Should Show Spotify Is Not Running", done => { | ||
musicCtr.quitApp(models_1.PlayerName.SpotifyDesktop).then(() => { | ||
musicCtr.quitApp(CodyMusic.PlayerName.SpotifyDesktop).then(() => { | ||
CodyMusic.isSpotifyRunning().then((result) => { | ||
@@ -105,3 +113,3 @@ expect(result).to.equal(false); | ||
it("Should Show Itunes Is Not Running", done => { | ||
musicCtr.quitApp(models_1.PlayerName.ItunesDesktop).then(() => { | ||
musicCtr.quitApp(CodyMusic.PlayerName.ItunesDesktop).then(() => { | ||
CodyMusic.isItunesRunning().then((result) => { | ||
@@ -118,5 +126,7 @@ expect(result).to.equal(false); | ||
it("Get Spotify Track Info", done => { | ||
musicCtr.launchApp(models_1.PlayerName.SpotifyDesktop).then(async () => { | ||
musicCtr | ||
.launchApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(2000); | ||
await CodyMusic.setVolume("Spotify", 5); | ||
await CodyMusic.setVolume(CodyMusic.PlayerName.SpotifyDesktop, 5); | ||
// artist: 'Martin Garrix', | ||
@@ -126,5 +136,5 @@ // album: 'High On Life (feat. Bonn)' | ||
let params = ["spotify:track:4ut5G4rgB1ClpMTMfjoIuy"]; | ||
await CodyMusic.playTrackInContext("Spotify", params); | ||
await CodyMusic.playTrackInContext(CodyMusic.PlayerName.SpotifyDesktop, params); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("Spotify"); | ||
let result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result.id).to.equal("spotify:track:4ut5G4rgB1ClpMTMfjoIuy"); | ||
@@ -139,8 +149,10 @@ done(); | ||
it("Get iTunes Track Info", done => { | ||
musicCtr.launchApp(models_1.PlayerName.ItunesDesktop).then(async () => { | ||
musicCtr | ||
.launchApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.setVolume("iTunes", 5); | ||
await CodyMusic.play("iTunes"); | ||
await CodyMusic.setVolume(CodyMusic.PlayerName.ItunesDesktop, 5); | ||
await CodyMusic.play(CodyMusic.PlayerName.ItunesDesktop); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("iTunes"); | ||
let result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.artist).to.not.equal(null); | ||
@@ -152,3 +164,3 @@ done(); | ||
musicCtr | ||
.launchApp(models_1.PlayerName.SpotifyDesktop) | ||
.launchApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then(async () => { | ||
@@ -160,5 +172,5 @@ musicUtil.sleep(1500); | ||
]; | ||
await CodyMusic.playTrackInContext("Spotify", params); | ||
await CodyMusic.playTrackInContext(CodyMusic.PlayerName.SpotifyDesktop, params); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("Spotify"); | ||
let result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
// make sure it's playing | ||
@@ -172,11 +184,11 @@ expect(result.state).to.equal("playing"); | ||
// we're at disk one, just test using next and previous | ||
await CodyMusic.next("Spotify"); | ||
await CodyMusic.next(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
await CodyMusic.next("Spotify"); | ||
await CodyMusic.next(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
// go to the previous song | ||
await CodyMusic.previous("Spotify"); | ||
await CodyMusic.previous(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(3000); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
previousSong = result.name; | ||
@@ -187,6 +199,6 @@ expect(previousSong).to.not.equal("Trouble"); | ||
// go to the previous song | ||
await CodyMusic.previous("Spotify"); | ||
await CodyMusic.previous(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(3000); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
previousSong = result.name; | ||
@@ -196,46 +208,47 @@ expect(previousSong).to.equal("Trouble"); | ||
// pause it | ||
await CodyMusic.pause("Spotify"); | ||
await CodyMusic.pause(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result.state).to.equal("paused"); | ||
// go to next | ||
await CodyMusic.next("Spotify"); | ||
await CodyMusic.next(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
let nextSong = result.name; | ||
expect(nextSong).to.not.equal(previousSong); | ||
// turn repeat on | ||
await CodyMusic.setRepeat("Spotify", true); | ||
await CodyMusic.setRepeat(CodyMusic.PlayerName.SpotifyDesktop, true); | ||
// check | ||
result = await CodyMusic.isRepeating("Spotify"); | ||
result = await CodyMusic.isRepeating(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result).to.equal(true); | ||
// turn repeat off | ||
await CodyMusic.setRepeat("Spotify", false); | ||
await CodyMusic.setRepeat(CodyMusic.PlayerName.SpotifyDesktop, false); | ||
// check | ||
result = await CodyMusic.isRepeating("Spotify"); | ||
result = await CodyMusic.isRepeating(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result).to.equal(false); | ||
// up the volume | ||
await CodyMusic.volumeUp("Spotify"); | ||
await CodyMusic.volumeUp(CodyMusic.PlayerName.SpotifyDesktop); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
const volume = result.volume; | ||
expect(volume).to.be.greaterThan(5); | ||
// mute and unmute tests | ||
await CodyMusic.mute("Spotify"); | ||
result = await CodyMusic.getState("Spotify"); | ||
await CodyMusic.mute(CodyMusic.PlayerName.SpotifyDesktop); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result.volume).to.equal(0); | ||
await CodyMusic.unmute("Spotify"); | ||
result = await CodyMusic.getState("Spotify"); | ||
await CodyMusic.unmute(CodyMusic.PlayerName.SpotifyDesktop); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result.volume).to.be.within(volume - 1, volume + 1); | ||
// play track | ||
await CodyMusic.playTrack("Spotify", "spotify:track:4ut5G4rgB1ClpMTMfjoIuy"); | ||
result = await CodyMusic.getState("Spotify"); | ||
await CodyMusic.playTrack(CodyMusic.PlayerName.SpotifyDesktop, "spotify:track:4ut5G4rgB1ClpMTMfjoIuy"); | ||
musicUtil.sleep(1500); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result.artist).to.equal("Martin Garrix"); | ||
// shuffle test | ||
await CodyMusic.setShuffle("Spotify", true); | ||
result = await CodyMusic.isShuffling("Spotify"); | ||
await CodyMusic.setShuffle(CodyMusic.PlayerName.SpotifyDesktop, true); | ||
result = await CodyMusic.isShuffling(CodyMusic.PlayerName.SpotifyDesktop); | ||
expect(result).to.equal(true); | ||
await musicCtr.quitApp(models_1.PlayerName.SpotifyDesktop); | ||
await musicCtr.quitApp(CodyMusic.PlayerName.SpotifyDesktop); | ||
done(); | ||
@@ -250,8 +263,8 @@ }) | ||
musicCtr | ||
.launchApp(models_1.PlayerName.ItunesDesktop) | ||
.launchApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.play("iTunes"); | ||
await CodyMusic.play(CodyMusic.PlayerName.ItunesDesktop); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("iTunes"); | ||
let result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
// make sure it's playing | ||
@@ -261,50 +274,50 @@ expect(result.state).to.equal("playing"); | ||
// go to the next song | ||
await CodyMusic.next("iTunes"); | ||
await CodyMusic.next(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.name).to.not.equal(songName); | ||
songName = result.name; | ||
// pause it | ||
await CodyMusic.pause("iTunes"); | ||
await CodyMusic.pause(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.state).to.equal("paused"); | ||
// go to previous | ||
await CodyMusic.previous("iTunes"); | ||
await CodyMusic.previous(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.name).to.not.equal(songName); | ||
songName = result.name; | ||
// turn repeat on | ||
await CodyMusic.setRepeat("iTunes", true); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.setRepeat(CodyMusic.PlayerName.ItunesDesktop, true); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.isRepeating("iTunes"); | ||
result = await CodyMusic.isRepeating(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result).to.equal("one"); | ||
// turn repeat off | ||
await CodyMusic.setRepeat("iTunes", false); | ||
await CodyMusic.setRepeat(CodyMusic.PlayerName.ItunesDesktop, false); | ||
// check | ||
result = await CodyMusic.isRepeating("iTunes"); | ||
result = await CodyMusic.isRepeating(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result).to.equal("off"); | ||
// up the volume | ||
await CodyMusic.volumeUp("iTunes"); | ||
await CodyMusic.volumeUp(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
const volume = result.volume; | ||
expect(volume).to.be.greaterThan(5); | ||
// mute and unmute tests | ||
await CodyMusic.mute("iTunes"); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.mute(CodyMusic.PlayerName.ItunesDesktop); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.volume).to.equal(0); | ||
await CodyMusic.unmute("iTunes"); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.unmute(CodyMusic.PlayerName.ItunesDesktop); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result.volume).to.equal(volume); | ||
// play track | ||
await CodyMusic.playTrack("iTunes", 1); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.playTrack(CodyMusic.PlayerName.ItunesDesktop, 1); | ||
result = await CodyMusic.getState(CodyMusic.PlayerName.ItunesDesktop); | ||
// shuffle test | ||
await CodyMusic.setShuffle("iTunes", true); | ||
result = await CodyMusic.isShuffling("iTunes"); | ||
await CodyMusic.setShuffle(CodyMusic.PlayerName.ItunesDesktop, true); | ||
result = await CodyMusic.isShuffling(CodyMusic.PlayerName.ItunesDesktop); | ||
expect(result).to.equal(true); | ||
await musicCtr.quitApp(models_1.PlayerName.ItunesDesktop); | ||
await musicCtr.quitApp(CodyMusic.PlayerName.ItunesDesktop); | ||
done(); | ||
@@ -311,0 +324,0 @@ }) |
"use strict"; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const models_1 = require("../../lib/models"); | ||
const util_1 = require("../../lib/util"); | ||
const expect = require("chai").expect; | ||
const CodyMusic = require("../../dist/index.js"); | ||
const CodyMusic = __importStar(require("../../index")); | ||
const musicUtil = new util_1.MusicUtil(); | ||
@@ -25,3 +31,3 @@ /** | ||
}); | ||
let setAccessToken = CodyMusic.getAccessToken("accessToken"); | ||
let setAccessToken = CodyMusic.getAccessToken(); | ||
expect(setAccessToken).to.equal(accessToken); | ||
@@ -43,3 +49,3 @@ CodyMusic.getSpotifyDevices().then(async (response) => { | ||
const albumId = "1GUfof1gHsqYjoHFym3aim"; | ||
await CodyMusic.launchPlayer(models_1.PlayerName.SpotifyWeb, { | ||
await CodyMusic.launchPlayer(CodyMusic.PlayerName.SpotifyWeb, { | ||
album: albumId | ||
@@ -59,3 +65,3 @@ }); | ||
}; | ||
response = await CodyMusic.pause(models_1.PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.pause(CodyMusic.PlayerName.SpotifyWeb, options); | ||
done(); | ||
@@ -83,3 +89,3 @@ }); | ||
const device_id = response[0].id; | ||
response = await CodyMusic.playSpotifyDevice(device_id, true /* play */); | ||
response = await CodyMusic.playSpotifyDevice(device_id); | ||
musicUtil.sleep(3000); | ||
@@ -97,3 +103,3 @@ expect(response.status).to.equal(204); | ||
}; | ||
response = await CodyMusic.pause(models_1.PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.pause(CodyMusic.PlayerName.SpotifyWeb, options); | ||
musicUtil.sleep(3000); | ||
@@ -116,7 +122,7 @@ expect(response.status).to.equal(204); | ||
}; | ||
response = await CodyMusic.play(models_1.PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.play(CodyMusic.PlayerName.SpotifyWeb, options); | ||
musicUtil.sleep(3000); | ||
expect(response.status).to.equal(204); | ||
CodyMusic.getState(models_1.PlayerName.SpotifyWeb).then((response) => { | ||
expect(response.track.uri).to.equal(track_id); | ||
CodyMusic.getState(CodyMusic.PlayerName.SpotifyWeb).then((response) => { | ||
expect(response.uri).to.equal(track_id); | ||
done(); | ||
@@ -137,8 +143,8 @@ }); | ||
}; | ||
response = await CodyMusic.play(models_1.PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.play(CodyMusic.PlayerName.SpotifyWeb, options); | ||
musicUtil.sleep(3000); | ||
delete options["track_ids"]; | ||
response = await CodyMusic.next(models_1.PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.next(CodyMusic.PlayerName.SpotifyWeb, options); | ||
musicUtil.sleep(1000); | ||
response = await CodyMusic.next(models_1.PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.next(CodyMusic.PlayerName.SpotifyWeb, options); | ||
musicUtil.sleep(1000); | ||
@@ -145,0 +151,0 @@ done(); |
439
index.ts
"use strict"; | ||
import { MusicController } from "./lib/controller"; | ||
import { | ||
PlayerName, | ||
TrackState, | ||
PlayerDevice, | ||
SpotifyAudioFeature | ||
} from "./lib/models"; | ||
import { MusicPlayerState } from "./lib/playerstate"; | ||
import { MusicStore } from "./lib/store"; | ||
import { MusicUtil } from "./lib/util"; | ||
// get the instances | ||
const musicCtr = MusicController.getInstance(); | ||
const musicPlayerCtr = MusicPlayerState.getInstance(); | ||
const musicStore = MusicStore.getInstance(); | ||
const musicUtil = new MusicUtil(); | ||
/** | ||
* Set Credentials (currently only supports Spotify) | ||
* Accepted credentials: clientId, clientSecret, refreshToken, accessToken | ||
* @param credentials | ||
*/ | ||
export function setCredentials(credentials: any) { | ||
musicStore.setCredentials(credentials); | ||
} | ||
/** | ||
* Get the accessToken provided via through the setCredentials api | ||
* @returns {string} the access token string | ||
*/ | ||
export function getAccessToken() { | ||
return musicStore.credentialByKey("accessToken"); | ||
} | ||
/** | ||
* Checks if the Spotify desktop or web player is running or not | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export async function isSpotifyRunning() { | ||
let running = await isPlayerRunning(PlayerName.SpotifyDesktop); | ||
if (!running) { | ||
// check the web | ||
running = await musicPlayerCtr.isSpotifyWebRunning(); | ||
} | ||
return running; | ||
} | ||
/** | ||
* Checks if the iTunes desktop player is running or not | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export function isItunesRunning() { | ||
return isPlayerRunning(PlayerName.ItunesDesktop); | ||
} | ||
/** | ||
* Checks if one of the specified players is running | ||
* @param player {spotify|spotify-web|itunes} | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export async function isPlayerRunning(player: PlayerName) { | ||
if (player === PlayerName.SpotifyWeb) { | ||
return await musicPlayerCtr.isSpotifyWebRunning(); | ||
} else { | ||
return await musicCtr.isMusicPlayerActive(player); | ||
} | ||
} | ||
/** | ||
* Returns the currently running track info (player and track). | ||
* This only supports returning the state for itunes and spotify desktop | ||
* on Mac and spotify desktop on windows. | ||
**/ | ||
export async function getRunningPlayerState(): Promise<TrackState> { | ||
if (await musicCtr.isMusicPlayerActive(PlayerName.SpotifyDesktop)) { | ||
return getPlayerState(PlayerName.SpotifyDesktop); | ||
} else if (await musicCtr.isMusicPlayerActive(PlayerName.ItunesDesktop)) { | ||
return getPlayerState(PlayerName.ItunesDesktop); | ||
} | ||
return getPlayerState(PlayerName.SpotifyWeb); | ||
} | ||
/** | ||
* Returns the player state and track of a given player {spotify|spotify-web|itunes} | ||
* - Spotify does not return a "genre" | ||
* - duration is in milliseconds | ||
* @param player {spotify|spotif-web|itunes} | ||
* @returns {artist, album, genre, disc_number, duration, played_count, track_number, id, name, state} | ||
*/ | ||
export async function getPlayerState(player: PlayerName): Promise<TrackState> { | ||
if (player === PlayerName.SpotifyWeb) { | ||
return await musicPlayerCtr.getSpotifyWebCurrentTrack(); | ||
} | ||
const state = await musicCtr.run(player, "state"); | ||
if (state) { | ||
return JSON.parse(state); | ||
} | ||
return new TrackState(); | ||
} | ||
/** | ||
* Returns the tracks that are found by the given playlist name | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param playListName | ||
*/ | ||
export async function getTracksByPlaylistName( | ||
player: PlayerName, | ||
playListName: string | ||
) { | ||
const params = null; | ||
const argv = [playListName]; | ||
const result = await musicCtr.run( | ||
player, | ||
"playlistTracksOfPlaylist", | ||
params, | ||
argv | ||
); | ||
let jsonResult: any = {}; | ||
if (result) { | ||
let jsonList = result.split("[TRACK_END],"); | ||
if (jsonList && jsonList.length > 0) { | ||
for (let i = 0; i < jsonList.length; i++) { | ||
let jsonStr = jsonList[i].trim(); | ||
jsonResult[i] = JSON.parse(jsonStr); | ||
} | ||
} | ||
} | ||
return jsonResult; | ||
} | ||
/** | ||
* Plays a specific track on the Spotify or iTunes desktop | ||
* @param player | ||
* @param params (e.g. ["spotify:track:0R8P9KfGJCDULmlEoBagcO", "spotify:album:6ZG5lRT77aJ3btmArcykra"] | ||
*/ | ||
export function playTrackInContext(player: PlayerName, params: any[]) { | ||
return musicCtr.playTrackInContext(player, params); | ||
} | ||
/** | ||
* Initiate and play the specified Spotify device | ||
* @param device_id {string} | ||
* @param play {boolean} | ||
*/ | ||
export function playSpotifyDevice(device_id: string) { | ||
return musicCtr.playSpotifyDevice(device_id); | ||
} | ||
/** | ||
* Initiate the play command for a specific player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function play(player: PlayerName, options: any) { | ||
if (player === PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebPlay(options); | ||
} else { | ||
return musicCtr.run(player, "play"); | ||
} | ||
} | ||
/** | ||
* Initiate the play command for a given trackId for a specific player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param trackId {string} | ||
*/ | ||
export function playTrack(player: PlayerName, trackId: string) { | ||
return musicCtr.playTrack(player, trackId); | ||
} | ||
/** | ||
* Initiate the pause command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function pause(player: PlayerName, options: any) { | ||
if (player === PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebPause(options); | ||
} else { | ||
return musicCtr.run(player, "pause"); | ||
} | ||
} | ||
/** | ||
* Initiate the play/pause command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function playPause(player: PlayerName) { | ||
return musicCtr.run(player, "playPause"); | ||
} | ||
/** | ||
* Initiate the next command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function next(player: PlayerName, options: any) { | ||
if (player === PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebNext(options); | ||
} else { | ||
return musicCtr.run(player, "next"); | ||
} | ||
} | ||
/** | ||
* Initiate the previous command for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function previous(player: PlayerName, options: any) { | ||
if (player === PlayerName.SpotifyWeb) { | ||
return musicCtr.spotifyWebPrevious(options); | ||
} else { | ||
return musicCtr.run(player, "previous"); | ||
} | ||
} | ||
/** | ||
* Turn on/off repeat for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function setRepeat(player: PlayerName, repeat: boolean) { | ||
let repeatParam = repeat ? "repeatOn" : "repeatOff"; | ||
return musicCtr.run(player, repeatParam); | ||
} | ||
/** | ||
* Turn on/off shuffling for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export function setShuffle(player: PlayerName, shuffle: boolean) { | ||
let shuffleParam = shuffle ? ["true"] : ["false"]; | ||
return musicCtr.run(player, "setShuffling", shuffleParam); | ||
} | ||
/** | ||
* Return whether shuffling is on or not | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export async function isShuffling(player: PlayerName) { | ||
const val = await musicCtr.run(player, "isShuffling"); | ||
if (musicUtil.isBooleanString(val)) { | ||
return JSON.parse(val); | ||
} | ||
return val; | ||
} | ||
/** | ||
* Returns whether the player is on repeat or not | ||
* - spotify returns true or false, and itunes returns "off", "one", "all" | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export async function isRepeating(player: PlayerName) { | ||
let val = await musicCtr.run(player, "isRepeating"); | ||
if (musicUtil.isBooleanString(val)) { | ||
return JSON.parse(val); | ||
} | ||
return val; | ||
} | ||
/** | ||
* Update the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
export function setVolume(player: PlayerName, volume: number) { | ||
return musicCtr.setVolume(player, volume); | ||
} | ||
/** | ||
* Increments the players volume by a number | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
export function volumeUp(player: PlayerName, volume: number) { | ||
return musicCtr.run(player, "volumeUp"); | ||
} | ||
/** | ||
* Decrements the players volume by a number | ||
* @param player {spotify|spotify-web|itunes} | ||
* @param volume {0-100} | ||
*/ | ||
export function volumeDown(player: PlayerName, volume: number) { | ||
return musicCtr.run(player, "volumeDown"); | ||
} | ||
/** | ||
* Mutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export function mute(player: PlayerName) { | ||
return musicCtr.run(player, "mute"); | ||
} | ||
/** | ||
* Unmutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export function unmute(player: PlayerName) { | ||
return musicCtr.run(player, "unMute"); | ||
} | ||
/** | ||
* Unmutes the players volume | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export function setItunesLoved(loved: boolean) { | ||
return musicCtr.setItunesLoved(loved); | ||
} | ||
/** | ||
* Get the full list of the playlist names for a given player | ||
* @param player {spotify|spotify-web|itunes} | ||
*/ | ||
export async function getPlaylistNames(player: PlayerName) { | ||
let result = await musicCtr.run(player, "playlistNames"); | ||
// turn this into a string list | ||
if (result) { | ||
result = result.split(","); | ||
// now trim | ||
result = result.map((name: string) => { | ||
return name.trim(); | ||
}); | ||
} | ||
return result; | ||
} | ||
/** | ||
* Launches a player device | ||
* @param playerName {spotify|spotify-web|itunes} | ||
* @param options | ||
*/ | ||
export function launchPlayer(playerName: PlayerName, options: any) { | ||
if (playerName === PlayerName.SpotifyWeb) { | ||
return musicPlayerCtr.launchWebPlayer(options); | ||
} else { | ||
return musicCtr.startPlayer(playerName); | ||
} | ||
} | ||
/** | ||
* Returns available Spotify devices | ||
* @returns {Promise<PlayerDevice[]>} | ||
*/ | ||
export function getSpotifyDevices(): Promise<PlayerDevice[]> { | ||
return musicPlayerCtr.getSpotifyDevices(); | ||
} | ||
/** | ||
* Returns the genre for a provided arguments | ||
* @param artist {string} is required | ||
* @param songName {string} is optional | ||
*/ | ||
export function getGenre( | ||
artist: string, | ||
songName: string = "" | ||
): Promise<string> { | ||
return musicCtr.getGenre(artist, songName); | ||
} | ||
/** | ||
* Returns the spotify genre for a provided arguments | ||
* @param artist {string} is required | ||
*/ | ||
export function getSpotifyGenre(artist: string): Promise<string> { | ||
return musicCtr.getGenreFromSpotify(artist); | ||
} | ||
/** | ||
* Returns the audio features of the given track IDs | ||
* @param ids these are the track ids (sans spotify:track) | ||
*/ | ||
export function getSpotifyAudioFeatures( | ||
ids: string[] | ||
): Promise<SpotifyAudioFeature[]> { | ||
return musicPlayerCtr.getSpotifyAudioFeatures(ids); | ||
} | ||
/** | ||
* Deprecated - use "getRunningPlayerState()" instead | ||
*/ | ||
export function getCurrentlyRunningTrackState(): Promise<TrackState> { | ||
return musicPlayerCtr.getCurrentlyRunningTrackState(); | ||
} | ||
/** | ||
* deprecated, please use "getPlayerState" | ||
*/ | ||
export function getState(player: PlayerName): Promise<TrackState> { | ||
return getPlayerState(player); | ||
} | ||
/** | ||
* deprecated, please use "launchPlayer('spotify')" | ||
**/ | ||
export function startSpotifyIfNotRunning() { | ||
return musicCtr.launchApp(PlayerName.SpotifyDesktop); | ||
} | ||
/** | ||
* deprecated, please use "launchPlayer('itunes')" | ||
*/ | ||
export function startItunesIfNotRunning() { | ||
return musicCtr.launchApp(PlayerName.ItunesDesktop); | ||
} | ||
/** | ||
* deprecated, please use "isSpotifyRunning" or "isItunesRunning" | ||
*/ | ||
export function isRunning(player: PlayerName): Promise<boolean> { | ||
return isPlayerRunning(player); | ||
} | ||
/** | ||
* deprecated, pluse use "setRepat(player, repeat)" | ||
*/ | ||
export function repeatOn(player: PlayerName) { | ||
return setRepeat(player, true); | ||
} | ||
/** | ||
* deprecated, pluse use "setRepat(player, repeat)" | ||
*/ | ||
export function repeatOff(player: PlayerName) { | ||
return setRepeat(player, false); | ||
} | ||
/** | ||
* deprecated, pluse use "unmute(player)" | ||
*/ | ||
export function unMute(player: PlayerName) { | ||
return unmute(player); | ||
} | ||
export * from "./lib/apis"; | ||
export { PlayerName, PlayerDevice, SpotifyAudioFeature } from "./lib/models"; |
@@ -95,3 +95,3 @@ import { MusicUtil } from "./util"; | ||
let result = await musicUtil.execCmd(command); | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -106,3 +106,3 @@ } | ||
let result = await musicUtil.execCmd(command); | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -156,4 +156,5 @@ } | ||
} | ||
let result = await musicUtil.execCmd(command); | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -164,17 +165,2 @@ } | ||
playTrack(player: string, trackId: string) { | ||
let params = null; | ||
if (player === "Spotify") { | ||
params = [`"${trackId}"`]; | ||
} else { | ||
params = [`${trackId}`]; | ||
} | ||
return this.execScript(player, "playTrack", params).then(result => { | ||
if (result === null || result === undefined) { | ||
result = "ok"; | ||
} | ||
return result; | ||
}); | ||
} | ||
async run( | ||
@@ -234,3 +220,3 @@ player: PlayerName, | ||
result => { | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -246,3 +232,3 @@ } | ||
return this.execScript(player, "setVolume", [volume]).then(result => { | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -257,8 +243,16 @@ } | ||
loved | ||
]).then(result => { | ||
if (result === null || result === undefined) { | ||
result = "ok"; | ||
} | ||
return result; | ||
}); | ||
]) | ||
.then(result => { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
} | ||
return result; | ||
}) | ||
.catch(err => { | ||
console.log( | ||
"Error updating itunes track loved status, error: ", | ||
err.message | ||
); | ||
return false; | ||
}); | ||
} | ||
@@ -269,3 +263,3 @@ | ||
result => { | ||
if (result === null || result === undefined) { | ||
if (result === null || result === undefined || result === "") { | ||
result = "ok"; | ||
@@ -272,0 +266,0 @@ } |
@@ -22,13 +22,2 @@ export enum PlayerType { | ||
export class TrackState { | ||
/** | ||
* type of the player | ||
*/ | ||
type: PlayerType = PlayerType.NotAssigned; | ||
/** | ||
* The track data | ||
*/ | ||
track: Track = new Track(); | ||
} | ||
// {artist, album, genre, disc_number, duration, played_count, track_number, id, name, state} | ||
@@ -40,2 +29,3 @@ export class Track { | ||
disc_number: number = 0; | ||
disk_number: number = 0; | ||
duration: number = 0; | ||
@@ -55,2 +45,5 @@ duration_ms: number = 0; | ||
type: string = ""; | ||
playerType: PlayerType = PlayerType.NotAssigned; | ||
loved: boolean = false; | ||
volume: number = 0; | ||
status: TrackStatus = TrackStatus.NotAssigned; | ||
@@ -57,0 +50,0 @@ } |
@@ -6,3 +6,2 @@ import { MusicUtil } from "./util"; | ||
import { | ||
TrackState, | ||
PlayerDevice, | ||
@@ -34,4 +33,4 @@ Track, | ||
async getCurrentlyRunningTrackState(): Promise<TrackState> { | ||
let trackState: TrackState = new TrackState(); | ||
async getCurrentlyRunningTrackState(): Promise<Track> { | ||
let trackState: Track = new Track(); | ||
let spotifyDesktopRunning = await this.isSpotifyDesktopRunning(); | ||
@@ -181,7 +180,6 @@ let itunesDesktopRunning = await this.isItunesDesktopRunning(); | ||
async getDesktopTrackState(): Promise<TrackState> { | ||
let trackState: TrackState = new TrackState(); | ||
let playingTrack: Track = new Track(); | ||
let pausedTrack: Track = new Track(); | ||
let pausedType: PlayerType = PlayerType.NotAssigned; | ||
async getDesktopTrackState(): Promise<Track> { | ||
let outgoingTrack; | ||
let spotifyTrack; | ||
let itunesTrack; | ||
if (musicUtil.isMac()) { | ||
@@ -198,18 +196,9 @@ const spotifyRunning = await musicCtr.isMusicPlayerActive( | ||
if (state) { | ||
playingTrack = JSON.parse(state); | ||
spotifyTrack = JSON.parse(state); | ||
} | ||
if (playingTrack) { | ||
playingTrack.type = PlayerName.SpotifyDesktop; | ||
if (spotifyTrack) { | ||
spotifyTrack.type = PlayerName.SpotifyDesktop; | ||
spotifyTrack.playerType = PlayerType.MacSpotifyDesktop; | ||
} | ||
if (playingTrack && playingTrack.state === "playing") { | ||
trackState = { | ||
type: PlayerType.MacSpotifyDesktop, | ||
track: playingTrack | ||
}; | ||
} else if (playingTrack) { | ||
// save this one if itunes isn't running | ||
pausedTrack = playingTrack; | ||
pausedType = PlayerType.MacSpotifyDesktop; | ||
} | ||
} | ||
@@ -227,21 +216,19 @@ | ||
if (state) { | ||
playingTrack = JSON.parse(state); | ||
itunesTrack = JSON.parse(state); | ||
} | ||
if (playingTrack) { | ||
playingTrack.type = PlayerName.ItunesDesktop; | ||
if (itunesTrack) { | ||
itunesTrack.type = PlayerName.ItunesDesktop; | ||
itunesTrack.playerType = PlayerType.MacItunesDesktop; | ||
} | ||
if (playingTrack && playingTrack.state === "playing") { | ||
trackState = { | ||
type: PlayerType.MacItunesDesktop, | ||
track: playingTrack | ||
}; | ||
} else if (!pausedTrack && playingTrack) { | ||
pausedTrack = playingTrack; | ||
pausedType = PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
if (pausedTrack) { | ||
trackState = { type: pausedType, track: pausedTrack }; | ||
if (spotifyTrack && itunesTrack) { | ||
if (itunesTrack.state !== "playing") { | ||
outgoingTrack = spotifyTrack; | ||
} | ||
} else if (spotifyTrack) { | ||
outgoingTrack = spotifyTrack; | ||
} else { | ||
outgoingTrack = itunesTrack; | ||
} | ||
@@ -252,9 +239,6 @@ } else if (musicUtil.isWindows()) { | ||
if (winSpotifyRunning) { | ||
playingTrack = await this.getWindowsSpotifyTrackInfo(); | ||
if (playingTrack) { | ||
playingTrack.type = PlayerName.SpotifyDesktop; | ||
trackState = { | ||
type: PlayerType.MacSpotifyDesktop, | ||
track: playingTrack | ||
}; | ||
outgoingTrack = await this.getWindowsSpotifyTrackInfo(); | ||
if (outgoingTrack) { | ||
outgoingTrack.type = PlayerName.SpotifyDesktop; | ||
outgoingTrack.playerType = PlayerType.MacSpotifyDesktop; | ||
} | ||
@@ -265,13 +249,13 @@ } | ||
// make sure it's not an advertisement | ||
if (trackState && !musicUtil.isEmptyObj(trackState.track)) { | ||
if (outgoingTrack && !musicUtil.isEmptyObj(outgoingTrack)) { | ||
// "artist":"","album":"","id":"spotify:ad:000000012c603a6600000020316a17a1" | ||
if ( | ||
trackState.type === PlayerType.MacSpotifyDesktop && | ||
trackState.track.id.includes("spotify:ad:") | ||
outgoingTrack.type === PlayerType.MacSpotifyDesktop && | ||
outgoingTrack.id.includes("spotify:ad:") | ||
) { | ||
// it's a spotify ad | ||
trackState.track.status = TrackStatus.Advertisement; | ||
} else if (!trackState.track.artist && !trackState.track.album) { | ||
outgoingTrack.status = TrackStatus.Advertisement; | ||
} else if (!outgoingTrack.artist && !outgoingTrack.album) { | ||
// not enough info to send | ||
trackState.track.status = TrackStatus.NotAssigned; | ||
outgoingTrack.status = TrackStatus.NotAssigned; | ||
} | ||
@@ -282,11 +266,11 @@ } | ||
if ( | ||
trackState && | ||
!musicUtil.isEmptyObj(trackState.track) && | ||
trackState.track.duration | ||
outgoingTrack && | ||
!musicUtil.isEmptyObj(outgoingTrack) && | ||
outgoingTrack.duration | ||
) { | ||
// create the attributes | ||
trackState.track["duration_ms"] = trackState.track.duration; | ||
outgoingTrack["duration_ms"] = outgoingTrack.duration; | ||
} | ||
return trackState; | ||
return outgoingTrack; | ||
} | ||
@@ -358,4 +342,4 @@ | ||
async getSpotifyWebCurrentTrack(): Promise<TrackState> { | ||
let trackState: TrackState = new TrackState(); | ||
async getSpotifyWebCurrentTrack(): Promise<Track> { | ||
let trackState: Track = new Track(); | ||
@@ -382,4 +366,4 @@ let api = "/v1/me/player/currently-playing"; | ||
trackState.track = track; | ||
trackState.type = PlayerType.WebSpotify; | ||
trackState = track; | ||
trackState.playerType = PlayerType.WebSpotify; | ||
@@ -386,0 +370,0 @@ return trackState; |
{ | ||
"name": "cody-music", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "mac osx spotify and itunes music player controller, spotify audio features, itunes and spotify genre, and playlist control", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -11,3 +11,3 @@ #!/usr/bin/env node | ||
await runCommand( | ||
"cp scripts/* dist/lib/scripts/.", | ||
"cp lib/scripts/* dist/lib/scripts/.", | ||
"copy the contents of scripts to dist/sripts" | ||
@@ -14,0 +14,0 @@ ); |
@@ -22,3 +22,3 @@ # cody-music | ||
```javascript | ||
import * as music from "cody-music"; | ||
import * as CodyMusic from "cody-music"; | ||
``` | ||
@@ -42,3 +42,3 @@ | ||
// get the track info using get state | ||
await music.getPlayerState("iTunes").then(state => { | ||
await CodyMusic.getPlayerState("iTunes").then(state => { | ||
// - "genre" will be empty from Spotify | ||
@@ -58,3 +58,3 @@ // - duration is in milliseconds | ||
// play an iTunes track number | ||
await music.playTrack("iTunes", 1).then(result => { | ||
await CodyMusic.playTrack("iTunes", 1).then(result => { | ||
// track is playing | ||
@@ -64,3 +64,3 @@ }); | ||
// handling errors | ||
await music.playTrack("iTunes", 1000000000).then(result => { | ||
await CodyMusic.playTrack("iTunes", 1000000000).then(result => { | ||
// result will contain the "error" attribute with the error message | ||
@@ -115,3 +115,3 @@ if (result.error) { | ||
*/ | ||
getPlayerState(player: PlayerName): Promise<TrackState> | ||
getPlayerState(player: PlayerName): Promise<Track> | ||
@@ -121,3 +121,3 @@ /** | ||
*/ | ||
getRunningPlayerState(): Promise<TrackState> | ||
getRunningPlayerState(): Promise<Track> | ||
@@ -129,3 +129,3 @@ /** | ||
*/ | ||
getTracksByPlaylistName(player: PlayerName,playListName: string) | ||
getTracksByPlaylistName(player: PlayerName, playListName: string) | ||
@@ -151,3 +151,3 @@ /** | ||
*/ | ||
play(player: PlayerName, options: any) | ||
play(player: PlayerName, options: any = {}) | ||
@@ -166,3 +166,3 @@ /** | ||
*/ | ||
pause(player: PlayerName, options: any) | ||
pause(player: PlayerName, options: any = {}) | ||
@@ -181,3 +181,3 @@ /** | ||
*/ | ||
next(player: PlayerName, options: any) | ||
next(player: PlayerName, options: any = {}) | ||
@@ -189,3 +189,3 @@ /** | ||
*/ | ||
previous(player: PlayerName, options: any) | ||
previous(player: PlayerName, options: any = {}) | ||
@@ -261,3 +261,3 @@ /** | ||
*/ | ||
getPlaylistNames(player: PlayerName) | ||
getPlaylistNames(player: PlayerName):Promise<string[]> | ||
@@ -301,6 +301,6 @@ /** | ||
// deprecated, please use "getRunningPlayerState()" | ||
getCurrentlyRunningTrackState(): Promise<TrackState> | ||
getCurrentlyRunningTrackState(): Promise<Track> | ||
// deprecated, please use "getPlayerState" | ||
getState(player: PlayerName): Promise<TrackState> | ||
getState(player: PlayerName): Promise<Track> | ||
@@ -307,0 +307,0 @@ // deprecated, please use "launchPlayer('spotify')" |
const expect = require("chai").expect; | ||
const CodyMusic = require("../../dist/index.js"); | ||
import * as CodyMusic from "../../index"; | ||
import { MusicUtil } from "../../lib/util"; | ||
import { PlayerName } from "../../lib/models"; | ||
import { MusicController } from "../../lib/controller"; | ||
@@ -21,6 +20,6 @@ | ||
musicCtr | ||
.quitApp(PlayerName.SpotifyDesktop) | ||
.quitApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then((result: any) => { | ||
musicCtr | ||
.quitApp(PlayerName.ItunesDesktop) | ||
.quitApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then((result: any) => { | ||
@@ -35,3 +34,3 @@ done(); | ||
musicCtr | ||
.quitApp(PlayerName.ItunesDesktop) | ||
.quitApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then((result: any) => { | ||
@@ -49,3 +48,3 @@ done(); | ||
musicCtr | ||
.quitApp(PlayerName.SpotifyDesktop) | ||
.quitApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then((result: any) => { | ||
@@ -60,30 +59,42 @@ done(); | ||
it("Should Show The Playlist Names AND Show The Tracks Of A Playlist", done => { | ||
CodyMusic.getPlaylistNames("iTunes").then((names: []) => { | ||
expect(names).to.not.equal(0); | ||
// get the last name in the list and get the tracks | ||
CodyMusic.getTracksByPlaylistName( | ||
PlayerName.ItunesDesktop, | ||
names[names.length - 1] | ||
).then((result: any) => { | ||
expect(result.length).to.not.equal(0); | ||
done(); | ||
}); | ||
}); | ||
CodyMusic.getPlaylistNames(CodyMusic.PlayerName.ItunesDesktop).then( | ||
(names: string[]) => { | ||
expect(names.length).to.not.equal(0); | ||
// get the last name in the list and get the tracks | ||
CodyMusic.getTracksByPlaylistName( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
names[names.length - 1] | ||
).then((result: any) => { | ||
expect(result.length).to.not.equal(0); | ||
done(); | ||
}); | ||
} | ||
); | ||
}); | ||
it("Should Set An Itunes Song's Love State", done => { | ||
musicCtr.launchApp(PlayerName.ItunesDesktop).then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.setVolume("iTunes", 5); | ||
await CodyMusic.play("iTunes"); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("iTunes"); | ||
let loved = result.loved; | ||
CodyMusic.setItunesLoved(!loved).then(async (result: any) => { | ||
// get the state again | ||
result = await CodyMusic.getState("iTunes"); | ||
expect(result.loved).to.equal(!loved); | ||
done(); | ||
musicCtr | ||
.launchApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.setVolume( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
5 | ||
); | ||
await CodyMusic.play(CodyMusic.PlayerName.ItunesDesktop); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
let loved = result.loved; | ||
CodyMusic.setItunesLoved(!loved).then(async (result: any) => { | ||
// get the state again | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.loved).to.equal(!loved); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -93,5 +104,8 @@ | ||
// play a bad track number | ||
CodyMusic.playTrack("iTunes", 1000000000).then((result: any) => { | ||
CodyMusic.playTrack( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
1000000000 | ||
).then((result: any) => { | ||
expect(result.error).to.not.equal(null); | ||
musicCtr.quitApp(PlayerName.ItunesDesktop).then(() => { | ||
musicCtr.quitApp(CodyMusic.PlayerName.ItunesDesktop).then(() => { | ||
done(); | ||
@@ -103,3 +117,3 @@ }); | ||
it("Should Show Spotify Is Not Running", done => { | ||
musicCtr.quitApp(PlayerName.SpotifyDesktop).then(() => { | ||
musicCtr.quitApp(CodyMusic.PlayerName.SpotifyDesktop).then(() => { | ||
CodyMusic.isSpotifyRunning().then((result: any) => { | ||
@@ -113,3 +127,3 @@ expect(result).to.equal(false); | ||
it("Should Show Itunes Is Not Running", done => { | ||
musicCtr.quitApp(PlayerName.ItunesDesktop).then(() => { | ||
musicCtr.quitApp(CodyMusic.PlayerName.ItunesDesktop).then(() => { | ||
CodyMusic.isItunesRunning().then((result: any) => { | ||
@@ -127,16 +141,28 @@ expect(result).to.equal(false); | ||
it("Get Spotify Track Info", done => { | ||
musicCtr.launchApp(PlayerName.SpotifyDesktop).then(async () => { | ||
musicUtil.sleep(2000); | ||
await CodyMusic.setVolume("Spotify", 5); | ||
// artist: 'Martin Garrix', | ||
// album: 'High On Life (feat. Bonn)' | ||
// id: spotify:track:4ut5G4rgB1ClpMTMfjoIuy | ||
let params = ["spotify:track:4ut5G4rgB1ClpMTMfjoIuy"]; | ||
musicCtr | ||
.launchApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(2000); | ||
await CodyMusic.setVolume( | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
5 | ||
); | ||
// artist: 'Martin Garrix', | ||
// album: 'High On Life (feat. Bonn)' | ||
// id: spotify:track:4ut5G4rgB1ClpMTMfjoIuy | ||
let params = ["spotify:track:4ut5G4rgB1ClpMTMfjoIuy"]; | ||
await CodyMusic.playTrackInContext("Spotify", params); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("Spotify"); | ||
expect(result.id).to.equal("spotify:track:4ut5G4rgB1ClpMTMfjoIuy"); | ||
done(); | ||
}); | ||
await CodyMusic.playTrackInContext( | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
params | ||
); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result.id).to.equal( | ||
"spotify:track:4ut5G4rgB1ClpMTMfjoIuy" | ||
); | ||
done(); | ||
}); | ||
}); | ||
@@ -149,11 +175,18 @@ | ||
it("Get iTunes Track Info", done => { | ||
musicCtr.launchApp(PlayerName.ItunesDesktop).then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.setVolume("iTunes", 5); | ||
await CodyMusic.play("iTunes"); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("iTunes"); | ||
expect(result.artist).to.not.equal(null); | ||
done(); | ||
}); | ||
musicCtr | ||
.launchApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.setVolume( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
5 | ||
); | ||
await CodyMusic.play(CodyMusic.PlayerName.ItunesDesktop); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.artist).to.not.equal(null); | ||
done(); | ||
}); | ||
}); | ||
@@ -163,3 +196,3 @@ | ||
musicCtr | ||
.launchApp(PlayerName.SpotifyDesktop) | ||
.launchApp(CodyMusic.PlayerName.SpotifyDesktop) | ||
.then(async () => { | ||
@@ -172,6 +205,11 @@ musicUtil.sleep(1500); | ||
await CodyMusic.playTrackInContext("Spotify", params); | ||
await CodyMusic.playTrackInContext( | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
params | ||
); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("Spotify"); | ||
let result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
// make sure it's playing | ||
@@ -185,12 +223,16 @@ expect(result.state).to.equal("playing"); | ||
// we're at disk one, just test using next and previous | ||
await CodyMusic.next("Spotify"); | ||
await CodyMusic.next(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
await CodyMusic.next("Spotify"); | ||
await CodyMusic.next(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
// go to the previous song | ||
await CodyMusic.previous("Spotify"); | ||
await CodyMusic.previous( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
musicUtil.sleep(3000); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
previousSong = result.name; | ||
@@ -200,6 +242,10 @@ expect(previousSong).to.not.equal("Trouble"); | ||
// go to the previous song | ||
await CodyMusic.previous("Spotify"); | ||
await CodyMusic.previous( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
musicUtil.sleep(3000); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
previousSong = result.name; | ||
@@ -210,28 +256,44 @@ expect(previousSong).to.equal("Trouble"); | ||
// pause it | ||
await CodyMusic.pause("Spotify"); | ||
await CodyMusic.pause(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result.state).to.equal("paused"); | ||
// go to next | ||
await CodyMusic.next("Spotify"); | ||
await CodyMusic.next(CodyMusic.PlayerName.SpotifyDesktop); | ||
musicUtil.sleep(1500); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
let nextSong = result.name; | ||
expect(nextSong).to.not.equal(previousSong); | ||
// turn repeat on | ||
await CodyMusic.setRepeat("Spotify", true); | ||
await CodyMusic.setRepeat( | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
true | ||
); | ||
// check | ||
result = await CodyMusic.isRepeating("Spotify"); | ||
result = await CodyMusic.isRepeating( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result).to.equal(true); | ||
// turn repeat off | ||
await CodyMusic.setRepeat("Spotify", false); | ||
await CodyMusic.setRepeat( | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
false | ||
); | ||
// check | ||
result = await CodyMusic.isRepeating("Spotify"); | ||
result = await CodyMusic.isRepeating( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result).to.equal(false); | ||
// up the volume | ||
await CodyMusic.volumeUp("Spotify"); | ||
await CodyMusic.volumeUp(CodyMusic.PlayerName.SpotifyDesktop); | ||
// check | ||
result = await CodyMusic.getState("Spotify"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
const volume = result.volume; | ||
@@ -241,8 +303,12 @@ expect(volume).to.be.greaterThan(5); | ||
// mute and unmute tests | ||
await CodyMusic.mute("Spotify"); | ||
result = await CodyMusic.getState("Spotify"); | ||
await CodyMusic.mute(CodyMusic.PlayerName.SpotifyDesktop); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result.volume).to.equal(0); | ||
await CodyMusic.unmute("Spotify"); | ||
result = await CodyMusic.getState("Spotify"); | ||
await CodyMusic.unmute(CodyMusic.PlayerName.SpotifyDesktop); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result.volume).to.be.within(volume - 1, volume + 1); | ||
@@ -252,14 +318,23 @@ | ||
await CodyMusic.playTrack( | ||
"Spotify", | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
"spotify:track:4ut5G4rgB1ClpMTMfjoIuy" | ||
); | ||
result = await CodyMusic.getState("Spotify"); | ||
musicUtil.sleep(1500); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result.artist).to.equal("Martin Garrix"); | ||
// shuffle test | ||
await CodyMusic.setShuffle("Spotify", true); | ||
result = await CodyMusic.isShuffling("Spotify"); | ||
await CodyMusic.setShuffle( | ||
CodyMusic.PlayerName.SpotifyDesktop, | ||
true | ||
); | ||
result = await CodyMusic.isShuffling( | ||
CodyMusic.PlayerName.SpotifyDesktop | ||
); | ||
expect(result).to.equal(true); | ||
await musicCtr.quitApp(PlayerName.SpotifyDesktop); | ||
await musicCtr.quitApp(CodyMusic.PlayerName.SpotifyDesktop); | ||
@@ -276,9 +351,11 @@ done(); | ||
musicCtr | ||
.launchApp(PlayerName.ItunesDesktop) | ||
.launchApp(CodyMusic.PlayerName.ItunesDesktop) | ||
.then(async () => { | ||
musicUtil.sleep(1500); | ||
await CodyMusic.play("iTunes"); | ||
await CodyMusic.play(CodyMusic.PlayerName.ItunesDesktop); | ||
musicUtil.sleep(1500); | ||
let result = await CodyMusic.getState("iTunes"); | ||
let result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
// make sure it's playing | ||
@@ -289,34 +366,54 @@ expect(result.state).to.equal("playing"); | ||
// go to the next song | ||
await CodyMusic.next("iTunes"); | ||
await CodyMusic.next(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.name).to.not.equal(songName); | ||
songName = result.name; | ||
// pause it | ||
await CodyMusic.pause("iTunes"); | ||
await CodyMusic.pause(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.state).to.equal("paused"); | ||
// go to previous | ||
await CodyMusic.previous("iTunes"); | ||
await CodyMusic.previous(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.name).to.not.equal(songName); | ||
songName = result.name; | ||
// turn repeat on | ||
await CodyMusic.setRepeat("iTunes", true); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.setRepeat( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
true | ||
); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
// check | ||
result = await CodyMusic.isRepeating("iTunes"); | ||
result = await CodyMusic.isRepeating( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result).to.equal("one"); | ||
// turn repeat off | ||
await CodyMusic.setRepeat("iTunes", false); | ||
await CodyMusic.setRepeat( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
false | ||
); | ||
// check | ||
result = await CodyMusic.isRepeating("iTunes"); | ||
result = await CodyMusic.isRepeating( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result).to.equal("off"); | ||
// up the volume | ||
await CodyMusic.volumeUp("iTunes"); | ||
await CodyMusic.volumeUp(CodyMusic.PlayerName.ItunesDesktop); | ||
// check | ||
result = await CodyMusic.getState("iTunes"); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
const volume = result.volume; | ||
@@ -326,20 +423,34 @@ expect(volume).to.be.greaterThan(5); | ||
// mute and unmute tests | ||
await CodyMusic.mute("iTunes"); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.mute(CodyMusic.PlayerName.ItunesDesktop); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.volume).to.equal(0); | ||
await CodyMusic.unmute("iTunes"); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.unmute(CodyMusic.PlayerName.ItunesDesktop); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result.volume).to.equal(volume); | ||
// play track | ||
await CodyMusic.playTrack("iTunes", 1); | ||
result = await CodyMusic.getState("iTunes"); | ||
await CodyMusic.playTrack( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
1 | ||
); | ||
result = await CodyMusic.getState( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
// shuffle test | ||
await CodyMusic.setShuffle("iTunes", true); | ||
result = await CodyMusic.isShuffling("iTunes"); | ||
await CodyMusic.setShuffle( | ||
CodyMusic.PlayerName.ItunesDesktop, | ||
true | ||
); | ||
result = await CodyMusic.isShuffling( | ||
CodyMusic.PlayerName.ItunesDesktop | ||
); | ||
expect(result).to.equal(true); | ||
await musicCtr.quitApp(PlayerName.ItunesDesktop); | ||
await musicCtr.quitApp(CodyMusic.PlayerName.ItunesDesktop); | ||
@@ -346,0 +457,0 @@ done(); |
@@ -1,5 +0,4 @@ | ||
import { PlayerName, PlayerType } from "../../lib/models"; | ||
import { MusicUtil } from "../../lib/util"; | ||
const expect = require("chai").expect; | ||
const CodyMusic = require("../../dist/index.js"); | ||
import * as CodyMusic from "../../index"; | ||
@@ -27,29 +26,34 @@ const musicUtil = new MusicUtil(); | ||
let setAccessToken = CodyMusic.getAccessToken("accessToken"); | ||
let setAccessToken = CodyMusic.getAccessToken(); | ||
expect(setAccessToken).to.equal(accessToken); | ||
CodyMusic.getSpotifyDevices().then(async (response: []) => { | ||
let hasComputerDevice = false; | ||
if (response) { | ||
for (let i = 0; i < response.length; i++) { | ||
let element: any = response[i]; | ||
if (element.type === "Computer") { | ||
hasComputerDevice = true; | ||
break; | ||
CodyMusic.getSpotifyDevices().then( | ||
async (response: CodyMusic.PlayerDevice[]) => { | ||
let hasComputerDevice = false; | ||
if (response) { | ||
for (let i = 0; i < response.length; i++) { | ||
let element: any = response[i]; | ||
if (element.type === "Computer") { | ||
hasComputerDevice = true; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (!hasComputerDevice) { | ||
// launch the web player | ||
// High on Life | ||
const albumId = "1GUfof1gHsqYjoHFym3aim"; | ||
await CodyMusic.launchPlayer(PlayerName.SpotifyWeb, { | ||
album: albumId | ||
}); | ||
musicUtil.sleep(5000); | ||
if (!hasComputerDevice) { | ||
// launch the web player | ||
// High on Life | ||
const albumId = "1GUfof1gHsqYjoHFym3aim"; | ||
await CodyMusic.launchPlayer( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
{ | ||
album: albumId | ||
} | ||
); | ||
musicUtil.sleep(5000); | ||
} | ||
done(); | ||
} | ||
done(); | ||
}); | ||
); | ||
}); | ||
@@ -64,3 +68,6 @@ | ||
}; | ||
response = await CodyMusic.pause(PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.pause( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
options | ||
); | ||
done(); | ||
@@ -90,6 +97,3 @@ }); | ||
const device_id = response[0].id; | ||
response = await CodyMusic.playSpotifyDevice( | ||
device_id, | ||
true /* play */ | ||
); | ||
response = await CodyMusic.playSpotifyDevice(device_id); | ||
musicUtil.sleep(3000); | ||
@@ -108,3 +112,6 @@ expect(response.status).to.equal(204); | ||
}; | ||
response = await CodyMusic.pause(PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.pause( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
options | ||
); | ||
musicUtil.sleep(3000); | ||
@@ -128,10 +135,15 @@ expect(response.status).to.equal(204); | ||
}; | ||
response = await CodyMusic.play(PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.play( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
options | ||
); | ||
musicUtil.sleep(3000); | ||
expect(response.status).to.equal(204); | ||
CodyMusic.getState(PlayerName.SpotifyWeb).then((response: any) => { | ||
expect(response.track.uri).to.equal(track_id); | ||
done(); | ||
}); | ||
CodyMusic.getState(CodyMusic.PlayerName.SpotifyWeb).then( | ||
(response: any) => { | ||
expect(response.uri).to.equal(track_id); | ||
done(); | ||
} | ||
); | ||
}); | ||
@@ -152,3 +164,6 @@ }); | ||
response = await CodyMusic.play(PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.play( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
options | ||
); | ||
musicUtil.sleep(3000); | ||
@@ -158,6 +173,12 @@ | ||
response = await CodyMusic.next(PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.next( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
options | ||
); | ||
musicUtil.sleep(1000); | ||
response = await CodyMusic.next(PlayerName.SpotifyWeb, options); | ||
response = await CodyMusic.next( | ||
CodyMusic.PlayerName.SpotifyWeb, | ||
options | ||
); | ||
musicUtil.sleep(1000); | ||
@@ -164,0 +185,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
223242
61
5259