Hi,
a few days ago I asked for a possibility to stop the spotify player without killing and restarting the whole process!
In the meantime I’ve investigated a bit time and studied the code and concept of the python script ‘main.py’ . After a while I found a way to retrieve status and metadata from the spotify client by using a small self written python script. Here are some examples how this sript works:
spconnect_ctrl command [parameter]
pi@P1volumio:~$ ./spconnect_ctrl status
active : False
logged_in : True
repeat : False
shuffle : False
playing : False
pi@P1volumio:~$ ./spconnect_ctrl status active
False
pi@P1volumio:~$ ./spconnect_ctrl metadata
volume : 52428
context_uri : spotify:user:firstname.lastname:playlist:4gt54xczecpEK5C4k9S
cover_uri : spotify:image:87779c10f715c6f2afeb04c2d5619b933fc18c58
artist_name : Madrugada
track_uri : spotify:track:4C201yNPCEnMbTMO1yP5mR
artist_uri : spotify:artist:0iC8O5ABswVUFiYwM94bu3
track_name : Quite Emotional - Live; 2010 Remastered Version
album_uri : spotify:album:5zylhCTCHQstRpSU47BEiw
duration : 258934
album_name : The Best Of Madrugada
data0 : Best of Bests
pi@P1volumio:~$ ./spconnect_ctrl pause
OK
For those who are interested in the code:
[code]#!/usr/bin/python
-- coding: utf-8 --
Get status from Spotify Connect Client on Volumio
Creation: 29.02.2016
Last Update: 29.02.2016
Copyright © 2016 by Mr.Sponti
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
import required modules
import os
import sys
import requests
import json
sp_config = {
‘client’ : os.uname()[1],
‘port’ : ‘4000’,
‘status’ : (’/api/info/status’, ‘i’),
‘metadata’: (’/api/info/metadata’, ‘i’),
‘pause’ : (’/api/playback/pause’,‘p’),
‘play’ : (’/api/playback/play’, ‘p’)
}
def main ():
get command line parameter
if len(sys.argv) == 1:
print (“usage: %s command [parameter]” % sys.argv[0])
sys.exit()
if len(sys.argv) >= 2:
command = sys.argv[1]
if len(sys.argv) >= 3:
parameter = sys.argv[2]
else :
parameter = None
send http request to get data from spotify client
try:
rdata = requests.get(‘http://’+ sp_config[‘client’] + ‘:’ + sp_config[‘port’] + sp_config[command][0])
if rdata.ok :
if sp_config[command][1] == ‘p’ :
print (‘OK’)
else :
if not parameter :
for parameter,pvalue in rdata.json().items():
print (’%s : %s’ % (parameter, pvalue))
else :
print rdata.json()[parameter]
else :
print (‘Error: %s’ % rdata.status_code)
except requests.exceptions.ConnectionError as error:
print (‘Connection Error: %s’ % error)
except requests.exceptions.HTTPError as error:
print (‘HTTP Error: %s’ % error)
if name == ‘main’:
main()[/code]
after creating the file you need to change the ownership and permission.
chmod 755 spconnect_ctrl
sudo chown root spconnect_ctrl
sudo chgrp root spconnect_ctrl
pi@P1volumio:~$ ls -l spconnect_ctrl
-rwxr-xr-x 1 root root 1823 Feb 29 17:04 spconnect_ctrl
In case that someone is planning to build a player with a lcd, the sript can be used to read the metadata from the spotify client.