diff --git a/README.md b/README.md new file mode 100644 index 0000000..a73676d --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# iTunes MPRIS service +A service intended to integrate iTunes into D-Bus session +## Support +Supports setting metadata, pause, resume, next, previous +## Configuration +To use this you should +* Set `wineprefix` in index.js to location of your iTunes prefix +* In the prefix, install `wsh57` (`WINEPREFIX=/path/ winetricks wsh57`) +* Run using node +(A more streamlined procedure involving a systemd unit is under development) \ No newline at end of file diff --git a/com-scripts/mainLoop.js b/com-scripts/mainLoop.js index 7e413df..6668190 100644 --- a/com-scripts/mainLoop.js +++ b/com-scripts/mainLoop.js @@ -26,7 +26,7 @@ function ITEvent_OnPlayerPlayEvent (newTrack) { } //Write current image var a = Math.random() * 10; - newTrack.artwork.item(1).saveArtworkToFile(path + "\\artwork" + a); + if (newTrack.artwork.count > 0) newTrack.artwork.item(1).saveArtworkToFile(path + "\\artwork" + a); var updatePacket = { newStatus: itunesApp.playerState, name: newTrack.name, @@ -49,7 +49,16 @@ function ITEvent_OnAboutToPromptUserToQuitEvent(){ } var ITEvent_OnPlayerPlayingTrackChangedEvent = ITEvent_OnPlayerPlayEvent; -var ITEvent_OnPlayerStopEvent = ITEvent_OnPlayerPlayEvent; + +function ITEvent_OnPlayerStopEvent(newTrack) { + WScript.StdOut.WriteBlankLines(1); + if (newTrack.artist) + var updatePacket = { + newStatus: "Stopped" + } + if (newTrack.artist) updatePacket.newStatus = "Paused" + WScript.StdOut.Write(JSON.stringify(updatePacket)); +} WScript.ConnectObject(itunesApp, "ITEvent_"); diff --git a/index.js b/index.js index aa8c339..94b4781 100644 --- a/index.js +++ b/index.js @@ -18,16 +18,25 @@ child.stdout.on('data', function(data) { if (data.replace(/[\x00-\x1F\x7F]*/g, "").replace(/\[(\?25\S|K)/g, "") == "") return; let result = data.replace(/[\x00-\x1F\x7F]*/g, "").replace(/\[(\?25\S|K)/g, ""); // console.log(require("util").inspect(result), result.length, result[0], result[result.length], result[result.length-1]); - let obj = JSON.parse(result); - player.metadata = { - "mpris:trackid": player.objectPath('track/' + Math.floor(Math.random() * 100)), - "mpris.length": obj.duration * 1000 * 1000, - "mpris:artUrl": "file://" + __dirname + "/com-scripts/artwork" + obj.artKey, - "xesam:title": obj.name, - "xesam:album": obj.album, - "xesam:artist": [ obj.artist ] + let obj; + try { + obj = JSON.parse(result); + } catch { + console.log(result); + } + if (obj.name){ + player.metadata = { + "mpris:trackid": player.objectPath('track/' + Math.floor(Math.random() * 100)), + "mpris.length": obj.duration * 1000 * 1000, + "mpris:artUrl": "file://" + __dirname + "/com-scripts/artwork" + obj.artKey, + "xesam:title": obj.name, + "xesam:album": obj.album, + "xesam:artist": [ obj.artist ] + } + player.playbackStatus = MprisPlayer.PLAYBACK_STATUS_PLAYING; + } else { + player.playbackStatus = obj.newStatus } - player.playbackStatus = MprisPlayer.PLAYBACK_STATUS_PLAYING; }); child.on('close', function(code) { @@ -51,7 +60,7 @@ function pause() { let a = cp.execSync('script --return -qc "bash -c \'WINEPREFIX=' + wineprefix +' wine wscript ./com-scripts/togglePlayPause.js\' 2> /dev/null " /dev/null').toString(); switch (a) { case "0": - player.playbackStatus = MprisPlayer.PLAYBACK_STATUS_PAUSED; + player.playbackStatus = "Paused"; break; case "1": player.playbackStatus = MprisPlayer.PLAYBACK_STATUS_PLAYING; @@ -66,7 +75,7 @@ player.on("next", () => { let a = cp.execSync('script --return -qc "bash -c \'WINEPREFIX=' + wineprefix +' wine wscript ./com-scripts/next.js\' 2> /dev/null " /dev/null').toString(); switch (a) { case "0": - player.playbackStatus = MprisPlayer.PLAYBACK_STATUS_PAUSED; + player.playbackStatus = "Stopped"; break; case "1": player.playbackStatus = MprisPlayer.PLAYBACK_STATUS_PLAYING;