cody-music
Advanced tools
Comparing version 1.4.3 to 2.0.0
@@ -116,13 +116,21 @@ "use strict"; | ||
} | ||
return track; | ||
} | ||
track = await musicCtr.run(player, "state"); | ||
if (track) { | ||
track = JSON.parse(track); | ||
if (player === models_1.PlayerName.SpotifyDesktop) { | ||
track.playerType = models_1.PlayerType.MacSpotifyDesktop; | ||
if (!track || !track.id) { | ||
track = await musicCtr.run(player, "state"); | ||
if (track) { | ||
track = JSON.parse(track); | ||
if (player === models_1.PlayerName.SpotifyDesktop) { | ||
track.playerType = models_1.PlayerType.MacSpotifyDesktop; | ||
// applescript "id" returns the web equivelant of the "uri" value | ||
// check if it's an advertisement | ||
if (track.id.includes("spotify:ad:")) { | ||
track.state = models_1.TrackStatus.Advertisement; | ||
} | ||
} | ||
else { | ||
track.playerType = models_1.PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
else { | ||
track.playerType = models_1.PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
if (track && track.id) { | ||
return track; | ||
@@ -466,3 +474,3 @@ } | ||
function getCurrentlyRunningTrackState() { | ||
return musicPlayerCtr.getCurrentlyRunningTrack(); | ||
return getRunningTrack(); | ||
} | ||
@@ -469,0 +477,0 @@ exports.getCurrentlyRunningTrackState = getCurrentlyRunningTrackState; |
@@ -14,6 +14,6 @@ export declare enum PlayerType { | ||
export declare enum TrackStatus { | ||
Playing = 1, | ||
Paused = 2, | ||
Advertisement = 3, | ||
NotAssigned = 4 | ||
Playing = "playing", | ||
Paused = "paused", | ||
Advertisement = "advertisement", | ||
NotAssigned = "notassigned" | ||
} | ||
@@ -45,3 +45,2 @@ export declare enum CodyResponseType { | ||
name: string; | ||
state: string; | ||
explicit: boolean; | ||
@@ -53,3 +52,3 @@ href: string; | ||
volume: number; | ||
status: TrackStatus; | ||
state: TrackStatus; | ||
} | ||
@@ -56,0 +55,0 @@ export declare class PlayerDevice { |
@@ -19,6 +19,6 @@ "use strict"; | ||
(function (TrackStatus) { | ||
TrackStatus[TrackStatus["Playing"] = 1] = "Playing"; | ||
TrackStatus[TrackStatus["Paused"] = 2] = "Paused"; | ||
TrackStatus[TrackStatus["Advertisement"] = 3] = "Advertisement"; | ||
TrackStatus[TrackStatus["NotAssigned"] = 4] = "NotAssigned"; | ||
TrackStatus["Playing"] = "playing"; | ||
TrackStatus["Paused"] = "paused"; | ||
TrackStatus["Advertisement"] = "advertisement"; | ||
TrackStatus["NotAssigned"] = "notassigned"; | ||
})(TrackStatus = exports.TrackStatus || (exports.TrackStatus = {})); | ||
@@ -56,3 +56,2 @@ var CodyResponseType; | ||
this.name = ""; | ||
this.state = ""; | ||
this.explicit = false; | ||
@@ -66,3 +65,3 @@ // href:"https://api.spotify.com/v1/playlists/0mwG8hCL4scWi8Nkt7jyoV/tracks" | ||
this.volume = 0; | ||
this.status = TrackStatus.NotAssigned; | ||
this.state = TrackStatus.NotAssigned; | ||
} | ||
@@ -69,0 +68,0 @@ } |
@@ -6,6 +6,3 @@ import { PlayerDevice, Track, PlayerContext } from "./models"; | ||
static getInstance(): MusicPlayerState; | ||
getCurrentlyRunningTrack(): Promise<Track>; | ||
isWindowsSpotifyRunning(): Promise<boolean>; | ||
isSpotifyDesktopRunning(): Promise<boolean>; | ||
isItunesDesktopRunning(): Promise<boolean>; | ||
isSpotifyWebRunning(): Promise<boolean>; | ||
@@ -27,3 +24,2 @@ /** | ||
getSpotifyDevices(): Promise<PlayerDevice[]>; | ||
getDesktopTrackState(): Promise<Track>; | ||
/** | ||
@@ -30,0 +26,0 @@ * returns i.e. |
@@ -8,3 +8,2 @@ "use strict"; | ||
const models_1 = require("./models"); | ||
const musicCtr = controller_1.MusicController.getInstance(); | ||
const musicStore = store_1.MusicStore.getInstance(); | ||
@@ -23,14 +22,2 @@ const musicClient = client_1.MusicClient.getInstance(); | ||
} | ||
async getCurrentlyRunningTrack() { | ||
let trackState = new models_1.Track(); | ||
let spotifyDesktopRunning = await this.isSpotifyDesktopRunning(); | ||
let itunesDesktopRunning = await this.isItunesDesktopRunning(); | ||
if (spotifyDesktopRunning || itunesDesktopRunning) { | ||
trackState = await this.getDesktopTrackState(); | ||
} | ||
else if (await this.isSpotifyWebRunning()) { | ||
trackState = await this.getSpotifyWebCurrentTrack(); | ||
} | ||
return trackState; | ||
} | ||
async isWindowsSpotifyRunning() { | ||
@@ -52,21 +39,2 @@ /** | ||
} | ||
async isSpotifyDesktopRunning() { | ||
let isRunning = false; | ||
if (musicUtil.isMac()) { | ||
isRunning = await musicCtr.isMusicPlayerActive(models_1.PlayerName.SpotifyDesktop); | ||
} | ||
else if (musicUtil.isWindows()) { | ||
isRunning = await this.isWindowsSpotifyRunning(); | ||
} | ||
// currently do not support linux desktop for spotify | ||
return isRunning; | ||
} | ||
async isItunesDesktopRunning() { | ||
let isRunning = false; | ||
if (musicUtil.isMac()) { | ||
isRunning = await musicCtr.isMusicPlayerActive(models_1.PlayerName.ItunesDesktop); | ||
} | ||
// currently do not supoport windows or linux desktop for itunes | ||
return isRunning; | ||
} | ||
async isSpotifyWebRunning() { | ||
@@ -112,76 +80,2 @@ let accessToken = musicStore.spotifyAccessToken; | ||
} | ||
async getDesktopTrackState() { | ||
let outgoingTrack; | ||
let spotifyTrack; | ||
let itunesTrack; | ||
if (musicUtil.isMac()) { | ||
const spotifyRunning = await musicCtr.isMusicPlayerActive(models_1.PlayerName.SpotifyDesktop); | ||
// spotify first | ||
if (spotifyRunning) { | ||
const state = await musicCtr.run(models_1.PlayerName.SpotifyDesktop, "state"); | ||
if (state) { | ||
spotifyTrack = JSON.parse(state); | ||
} | ||
if (spotifyTrack) { | ||
spotifyTrack.type = models_1.PlayerName.SpotifyDesktop; | ||
spotifyTrack.playerType = models_1.PlayerType.MacSpotifyDesktop; | ||
} | ||
} | ||
// next itunes | ||
const itunesRunning = await musicCtr.isMusicPlayerActive(models_1.PlayerName.ItunesDesktop); | ||
if (itunesRunning) { | ||
const state = await musicCtr.run(models_1.PlayerName.ItunesDesktop, "state"); | ||
if (state) { | ||
itunesTrack = JSON.parse(state); | ||
} | ||
if (itunesTrack) { | ||
itunesTrack.type = models_1.PlayerName.ItunesDesktop; | ||
itunesTrack.playerType = models_1.PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
if (spotifyTrack && itunesTrack) { | ||
if (itunesTrack.state !== "playing") { | ||
outgoingTrack = spotifyTrack; | ||
} | ||
} | ||
else if (spotifyTrack) { | ||
outgoingTrack = spotifyTrack; | ||
} | ||
else { | ||
outgoingTrack = itunesTrack; | ||
} | ||
} | ||
else if (musicUtil.isWindows()) { | ||
// supports only spotify for now | ||
const winSpotifyRunning = await this.isWindowsSpotifyRunning(); | ||
if (winSpotifyRunning) { | ||
outgoingTrack = await this.getWindowsSpotifyTrackInfo(); | ||
if (outgoingTrack) { | ||
outgoingTrack.type = models_1.PlayerName.SpotifyDesktop; | ||
outgoingTrack.playerType = models_1.PlayerType.MacSpotifyDesktop; | ||
} | ||
} | ||
} | ||
// make sure it's not an advertisement | ||
if (outgoingTrack && !musicUtil.isEmptyObj(outgoingTrack)) { | ||
// "artist":"","album":"","id":"spotify:ad:000000012c603a6600000020316a17a1" | ||
if (outgoingTrack.type === models_1.PlayerType.MacSpotifyDesktop && | ||
outgoingTrack.id.includes("spotify:ad:")) { | ||
// it's a spotify ad | ||
outgoingTrack.status = models_1.TrackStatus.Advertisement; | ||
} | ||
else if (!outgoingTrack.artist && !outgoingTrack.album) { | ||
// not enough info to send | ||
outgoingTrack.status = models_1.TrackStatus.NotAssigned; | ||
} | ||
} | ||
// include common attributes | ||
if (outgoingTrack && | ||
!musicUtil.isEmptyObj(outgoingTrack) && | ||
outgoingTrack.duration) { | ||
// create the attributes | ||
outgoingTrack["duration_ms"] = outgoingTrack.duration; | ||
} | ||
return outgoingTrack; | ||
} | ||
/** | ||
@@ -244,3 +138,3 @@ * returns i.e. | ||
async getSpotifyWebCurrentTrack() { | ||
let trackState = new models_1.Track(); | ||
let track = new models_1.Track(); | ||
let api = "/v1/me/player/currently-playing"; | ||
@@ -256,3 +150,3 @@ let response = await musicClient.spotifyApiGet(api); | ||
if (response && response.data && response.data.item) { | ||
let track = response.data.item; | ||
track = response.data.item; | ||
// override "type" with "spotify" | ||
@@ -264,7 +158,24 @@ track.type = "spotify"; | ||
musicUtil.extractAristFromSpotifyTrack(track); | ||
trackState = track; | ||
trackState.playerType = models_1.PlayerType.WebSpotify; | ||
return trackState; | ||
track.playerType = models_1.PlayerType.WebSpotify; | ||
} | ||
return trackState; | ||
// initialize it with not assigned | ||
if (track) { | ||
track["state"] = models_1.TrackStatus.NotAssigned; | ||
} | ||
if (track && track.uri) { | ||
if (track.uri.includes("spotify:ad:")) { | ||
track.state = models_1.TrackStatus.Advertisement; | ||
} | ||
else { | ||
let context = await this.getSpotifyPlayerContext(); | ||
// is_playing | ||
if (context && context.is_playing) { | ||
track["state"] = models_1.TrackStatus.Playing; | ||
} | ||
else { | ||
track["state"] = models_1.TrackStatus.Paused; | ||
} | ||
} | ||
} | ||
return track; | ||
} | ||
@@ -271,0 +182,0 @@ async getSpotifyPlayerContext() { |
@@ -13,2 +13,3 @@ "use strict"; | ||
const CodyMusic = __importStar(require("../../index")); | ||
const models_1 = require("../../lib/models"); | ||
const musicUtil = new util_1.MusicUtil(); | ||
@@ -96,3 +97,2 @@ /** | ||
response = await CodyMusic.playSpotifyDevice(device_id); | ||
musicUtil.sleep(3000); | ||
expect(response.status).to.equal(204); | ||
@@ -110,3 +110,2 @@ done(); | ||
response = await CodyMusic.pause(CodyMusic.PlayerName.SpotifyWeb, options); | ||
musicUtil.sleep(3000); | ||
expect(response.status).to.equal(204); | ||
@@ -132,2 +131,3 @@ done(); | ||
CodyMusic.getState(CodyMusic.PlayerName.SpotifyWeb).then((response) => { | ||
expect(response.state).to.equal(models_1.TrackStatus.Playing); | ||
expect(response.uri).to.equal(track_id); | ||
@@ -134,0 +134,0 @@ done(); |
@@ -9,3 +9,4 @@ "use strict"; | ||
SpotifyAudioFeature, | ||
PlayerType | ||
PlayerType, | ||
TrackStatus | ||
} from "./models"; | ||
@@ -124,13 +125,22 @@ import { MusicPlayerState } from "./playerstate"; | ||
} | ||
return track; | ||
} | ||
track = await musicCtr.run(player, "state"); | ||
if (track) { | ||
track = JSON.parse(track); | ||
if (player === PlayerName.SpotifyDesktop) { | ||
track.playerType = PlayerType.MacSpotifyDesktop; | ||
} else { | ||
track.playerType = PlayerType.MacItunesDesktop; | ||
if (!track || !track.id) { | ||
track = await musicCtr.run(player, "state"); | ||
if (track) { | ||
track = JSON.parse(track); | ||
if (player === PlayerName.SpotifyDesktop) { | ||
track.playerType = PlayerType.MacSpotifyDesktop; | ||
// applescript "id" returns the web equivelant of the "uri" value | ||
// check if it's an advertisement | ||
if (track.id.includes("spotify:ad:")) { | ||
track.state = TrackStatus.Advertisement; | ||
} | ||
} else { | ||
track.playerType = PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
} | ||
if (track && track.id) { | ||
return track; | ||
@@ -493,3 +503,3 @@ } | ||
export function getCurrentlyRunningTrackState(): Promise<Track> { | ||
return musicPlayerCtr.getCurrentlyRunningTrack(); | ||
return getRunningTrack(); | ||
} | ||
@@ -496,0 +506,0 @@ |
@@ -16,6 +16,6 @@ export enum PlayerType { | ||
export enum TrackStatus { | ||
Playing = 1, | ||
Paused = 2, | ||
Advertisement = 3, | ||
NotAssigned = 4 | ||
Playing = "playing", | ||
Paused = "paused", | ||
Advertisement = "advertisement", | ||
NotAssigned = "notassigned" | ||
} | ||
@@ -51,3 +51,2 @@ | ||
name: string = ""; | ||
state: string = ""; | ||
explicit: boolean = false; | ||
@@ -61,3 +60,3 @@ // href:"https://api.spotify.com/v1/playlists/0mwG8hCL4scWi8Nkt7jyoV/tracks" | ||
volume: number = 0; | ||
status: TrackStatus = TrackStatus.NotAssigned; | ||
state: TrackStatus = TrackStatus.NotAssigned; | ||
} | ||
@@ -64,0 +63,0 @@ |
@@ -9,8 +9,6 @@ import { MusicUtil } from "./util"; | ||
PlayerType, | ||
TrackStatus, | ||
PlayerContext, | ||
PlayerName | ||
TrackStatus | ||
} from "./models"; | ||
const musicCtr = MusicController.getInstance(); | ||
const musicStore = MusicStore.getInstance(); | ||
@@ -32,14 +30,2 @@ const musicClient = MusicClient.getInstance(); | ||
async getCurrentlyRunningTrack(): Promise<Track> { | ||
let trackState: Track = new Track(); | ||
let spotifyDesktopRunning = await this.isSpotifyDesktopRunning(); | ||
let itunesDesktopRunning = await this.isItunesDesktopRunning(); | ||
if (spotifyDesktopRunning || itunesDesktopRunning) { | ||
trackState = await this.getDesktopTrackState(); | ||
} else if (await this.isSpotifyWebRunning()) { | ||
trackState = await this.getSpotifyWebCurrentTrack(); | ||
} | ||
return trackState; | ||
} | ||
async isWindowsSpotifyRunning(): Promise<boolean> { | ||
@@ -65,26 +51,2 @@ /** | ||
async isSpotifyDesktopRunning() { | ||
let isRunning = false; | ||
if (musicUtil.isMac()) { | ||
isRunning = await musicCtr.isMusicPlayerActive( | ||
PlayerName.SpotifyDesktop | ||
); | ||
} else if (musicUtil.isWindows()) { | ||
isRunning = await this.isWindowsSpotifyRunning(); | ||
} | ||
// currently do not support linux desktop for spotify | ||
return isRunning; | ||
} | ||
async isItunesDesktopRunning() { | ||
let isRunning = false; | ||
if (musicUtil.isMac()) { | ||
isRunning = await musicCtr.isMusicPlayerActive( | ||
PlayerName.ItunesDesktop | ||
); | ||
} | ||
// currently do not supoport windows or linux desktop for itunes | ||
return isRunning; | ||
} | ||
async isSpotifyWebRunning(): Promise<boolean> { | ||
@@ -134,94 +96,2 @@ let accessToken = musicStore.spotifyAccessToken; | ||
async getDesktopTrackState(): Promise<Track> { | ||
let outgoingTrack; | ||
let spotifyTrack; | ||
let itunesTrack; | ||
if (musicUtil.isMac()) { | ||
const spotifyRunning = await musicCtr.isMusicPlayerActive( | ||
PlayerName.SpotifyDesktop | ||
); | ||
// spotify first | ||
if (spotifyRunning) { | ||
const state = await musicCtr.run( | ||
PlayerName.SpotifyDesktop, | ||
"state" | ||
); | ||
if (state) { | ||
spotifyTrack = JSON.parse(state); | ||
} | ||
if (spotifyTrack) { | ||
spotifyTrack.type = PlayerName.SpotifyDesktop; | ||
spotifyTrack.playerType = PlayerType.MacSpotifyDesktop; | ||
} | ||
} | ||
// next itunes | ||
const itunesRunning = await musicCtr.isMusicPlayerActive( | ||
PlayerName.ItunesDesktop | ||
); | ||
if (itunesRunning) { | ||
const state = await musicCtr.run( | ||
PlayerName.ItunesDesktop, | ||
"state" | ||
); | ||
if (state) { | ||
itunesTrack = JSON.parse(state); | ||
} | ||
if (itunesTrack) { | ||
itunesTrack.type = PlayerName.ItunesDesktop; | ||
itunesTrack.playerType = PlayerType.MacItunesDesktop; | ||
} | ||
} | ||
if (spotifyTrack && itunesTrack) { | ||
if (itunesTrack.state !== "playing") { | ||
outgoingTrack = spotifyTrack; | ||
} | ||
} else if (spotifyTrack) { | ||
outgoingTrack = spotifyTrack; | ||
} else { | ||
outgoingTrack = itunesTrack; | ||
} | ||
} else if (musicUtil.isWindows()) { | ||
// supports only spotify for now | ||
const winSpotifyRunning = await this.isWindowsSpotifyRunning(); | ||
if (winSpotifyRunning) { | ||
outgoingTrack = await this.getWindowsSpotifyTrackInfo(); | ||
if (outgoingTrack) { | ||
outgoingTrack.type = PlayerName.SpotifyDesktop; | ||
outgoingTrack.playerType = PlayerType.MacSpotifyDesktop; | ||
} | ||
} | ||
} | ||
// make sure it's not an advertisement | ||
if (outgoingTrack && !musicUtil.isEmptyObj(outgoingTrack)) { | ||
// "artist":"","album":"","id":"spotify:ad:000000012c603a6600000020316a17a1" | ||
if ( | ||
outgoingTrack.type === PlayerType.MacSpotifyDesktop && | ||
outgoingTrack.id.includes("spotify:ad:") | ||
) { | ||
// it's a spotify ad | ||
outgoingTrack.status = TrackStatus.Advertisement; | ||
} else if (!outgoingTrack.artist && !outgoingTrack.album) { | ||
// not enough info to send | ||
outgoingTrack.status = TrackStatus.NotAssigned; | ||
} | ||
} | ||
// include common attributes | ||
if ( | ||
outgoingTrack && | ||
!musicUtil.isEmptyObj(outgoingTrack) && | ||
outgoingTrack.duration | ||
) { | ||
// create the attributes | ||
outgoingTrack["duration_ms"] = outgoingTrack.duration; | ||
} | ||
return outgoingTrack; | ||
} | ||
/** | ||
@@ -292,3 +162,3 @@ * returns i.e. | ||
async getSpotifyWebCurrentTrack(): Promise<Track> { | ||
let trackState: Track = new Track(); | ||
let track: Track = new Track(); | ||
@@ -307,3 +177,3 @@ let api = "/v1/me/player/currently-playing"; | ||
if (response && response.data && response.data.item) { | ||
let track: Track = response.data.item; | ||
track = response.data.item; | ||
// override "type" with "spotify" | ||
@@ -316,8 +186,24 @@ track.type = "spotify"; | ||
trackState = track; | ||
trackState.playerType = PlayerType.WebSpotify; | ||
track.playerType = PlayerType.WebSpotify; | ||
} | ||
return trackState; | ||
// initialize it with not assigned | ||
if (track) { | ||
track["state"] = TrackStatus.NotAssigned; | ||
} | ||
return trackState; | ||
if (track && track.uri) { | ||
if (track.uri.includes("spotify:ad:")) { | ||
track.state = TrackStatus.Advertisement; | ||
} else { | ||
let context: PlayerContext = await this.getSpotifyPlayerContext(); | ||
// is_playing | ||
if (context && context.is_playing) { | ||
track["state"] = TrackStatus.Playing; | ||
} else { | ||
track["state"] = TrackStatus.Paused; | ||
} | ||
} | ||
} | ||
return track; | ||
} | ||
@@ -324,0 +210,0 @@ |
{ | ||
"name": "cody-music", | ||
"version": "1.4.3", | ||
"version": "2.0.0", | ||
"description": "mac osx spotify and itunes music player controller, spotify audio features, itunes and spotify genre, and playlist control", | ||
@@ -10,3 +10,4 @@ "main": "dist/index.js", | ||
"test": "npm run build && mocha -r ts-node/register test/**/test.ts --reporter spec --timeout 20000 --exit", | ||
"playlist-test": "npm run build && mocha -r ts-node/register test/playlists/test.ts --reporter spec --timeout 20000 --exit" | ||
"playlist-test": "npm run build && mocha -r ts-node/register test/playlists/test.ts --reporter spec --timeout 20000 --exit", | ||
"web-test": "npm run build && mocha -r ts-node/register test/web/test.ts --reporter spec --timeout 20000 --exit" | ||
}, | ||
@@ -13,0 +14,0 @@ "repository": { |
import { MusicUtil } from "../../lib/util"; | ||
const expect = require("chai").expect; | ||
import * as CodyMusic from "../../index"; | ||
import { TrackStatus } from "../../lib/models"; | ||
@@ -104,3 +105,2 @@ const musicUtil = new MusicUtil(); | ||
response = await CodyMusic.playSpotifyDevice(device_id); | ||
musicUtil.sleep(3000); | ||
expect(response.status).to.equal(204); | ||
@@ -122,3 +122,2 @@ done(); | ||
); | ||
musicUtil.sleep(3000); | ||
expect(response.status).to.equal(204); | ||
@@ -150,2 +149,3 @@ done(); | ||
(response: any) => { | ||
expect(response.state).to.equal(TrackStatus.Playing); | ||
expect(response.uri).to.equal(track_id); | ||
@@ -152,0 +152,0 @@ done(); |
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
263291
6332