status
Version:
System automation on steroids
70 lines (58 loc) • 1.82 kB
text/coffeescript
{exec} = require "child_process"
dbus = require "dbus"
{which} = require "fractal"
throw "Spotify not installed" unless which "spotify"
getMeta = (cb) ->
exec "dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Get string:'org.mpris.MediaPlayer2.Player' string:'Metadata'", (err, stdout) ->
throw err if err?
out = {}
for def in stdout.match /entry\(([\S\s]*?)\)/ig
[head, key, val..., tail] = def.split '\n'
key = key.match(/"([\S\s]*?)"/i)[1].split(':')[1]
[_, val...] = val.join('').trim().split(' ').join('').trim().split(' ')
val = val.join ''
match = val.match /"(.*?)"/i
if match
out[key] = match[1]
else
try
out[key] = parseFloat val
catch err
out[key] = val
cb out
getInterface = ->
dbus.init()
session = dbus.session_bus()
return dbus.get_interface session, "org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player"
module.exports =
meta:
name: "spotify"
author: "Contra"
version: "0.0.1"
description: "Spotify controls/information"
next: ->
getInterface().Next()
success: true
previous: ->
getInterface().Previous()
success: true
toggle: ->
getInterface().PlayPause()
success: true
pause: ->
getInterface().Pause()
success: true
play: ->
getInterface().Play()
success: true
stop: ->
getInterface().Stop()
success: true
open: (uri) ->
return 'Missing uri' unless typeof uri is 'string'
getInterface().OpenUri uri
success: true
playing: ->
getMeta (meta) =>
meta.contentCreated = new Date meta.contentCreated
meta