FiXato / tui_launcher

A proof of concept for a TUI-based launcher written in Python using urwid
MIT License
3 stars 1 forks source link

Feature Requeset: Allow appending multiple shell commands to one command function #3

Open klundry opened 3 years ago

klundry commented 3 years ago

Would be nice to be able to fire off a list of commands that correspond to a single button command in the script.

FiXato commented 3 years ago

if you combine the commands as a string with && you should already be able to do this:

commands={
  'play': f"""curl http://localhost:1234/api/playback/stop && curl http://localhost:1234/api/playback/rewind"""
}

but I'll also be adding support for supplying multiple commands via a tuple. I'll have to double check whether these will be run concurrently or sequentially though... My guess would be that because a subprocess is launched, the requests might not be run in succession, but rather in parallel, in which case the && solution might be more appropriate.

FiXato commented 3 years ago

as an aside, can you post an example of the curl command you use to set your volume now?

I have an idea for how you can increase/decrease your volume with a fixed amount, rather than just set it to preset values.

I recall you mention btw that you wanted to just increase your volume with a fixed amount, rather than having preset volumes, and I think you can achieve that with:

basically with a command like: f"""curl http://localhost:1234/api/set/volume/?volume=$(curl 'http://localhost:1234/api/info/status' | jq '.config.volume.percent + 10')""" which relies on jq to parse the Client.GetStatus response, find the volume percentage, and increase it by ten, and then pass that on to the Client.SetVolume request as variable, using Command Substitution.

(guessing the URLs and the data format here, but I hope you get the gist)

echo '{"config":{"instance":1,"latency":0,"name":"","volume":{"muted":false,"percent":74}},"connected":true,"host":{"arch":"x86_64","ip":"127.0.0.1","mac":"00:21:6a:7d:74:fc","name":"T400","os":"Linux Mint 17.3 Rosa"},"id":"00:21:6a:7d:74:fc","lastSeen":{"sec":1488026416,"usec":135973},"snapclient":{"name":"Snapclient","protocolVersion":2,"version":"0.10.0"}}' | jq '.config.volume.percent + 10'

klundry commented 3 years ago

I will try using &&.

Yes being able to set volume incrementally to be able to fine tune the volume would be nicer than having to set absolute values. Volume curl commands.

curl -d '{"id":8,"jsonrpc":"2.0","method":"Client.SetVolume","params":{"id":"7c:c7:09:62:5e:ad","volume":{"muted":false,"percent":30}}}' http://192.168.1.4:1780/jsonrpc
curl -d '{"id":8,"jsonrpc":"2.0","method":"Client.SetVolume","params":{"id":"b8:27:eb:0f:58:68","volume":{"muted":false,"percent":30}}}' http://192.168.1.4:1780/jsonrpc
FiXato commented 3 years ago

While it can probably be done in a single command inside the python script, I would suggest creating a set of helper shell scripts that you can also run by themselves, and thus can reuse in other scripts.

Start by creating the following shell script, get_volume.sh:

#!/bin/sh
# Error when a variable is not set
set -u
RPC_URL="http://192.168.1.4:1780/jsonrpc"
# Set the ID to the value of the first commandline argument, or default to the 7c address
DEFAULT_ID="7c:c7:09:62:5e:ad"
ID="${1:-$DEFAULT_ID}"
curl --silent -d '{"id":8,"jsonrpc":"2.0","method":"Client.GetStatus","params":{"id":"'$ID'"}}' "$RPC_URL" | jq '.config.volume.percent'

(You'll need to have jq installed to easily extract the current volume percentage from the json output)

be sure to make get_volume.sh executable, and put it on your PATH (put it in ~/bin is usually a good idea), or specify the full path to the script in the next script.

and create: increase_volume.sh:

#!/bin/sh
set -u
VOLUME_INCREMENT=10
# Set the ID to the value of the first commandline argument, or default to the 7c address
DEFAULT_ID="7c:c7:09:62:5e:ad"
ID="${1:-$DEFAULT_ID}"
NEW_VOLUME="$(echo "$(get_volume.sh "$ID")" + "$VOLUME_INCREMENT" | bc)"
RPC_URL="http://192.168.1.4:1780/jsonrpc"
curl --silent -d '{"id":8,"jsonrpc":"2.0","method":"Client.SetVolume","params":{"id":"'$ID'","volume":{"muted":false,"percent":'$NEW_VOLUME'}}}' "$RPC_URL"

(You'll need to have bc installed to do the calculation.)

Now just set the increase_volume.sh script as your command:

commands={
  'vol+': f"""curl increase_volume.sh && curl increase_volume.sh b8:27:eb:0f:58:68"""
}

This command should increase the volume on the default (7c) address, and the b8 address. For decrease volume you could just copy the increase script, and change the + for a - :) (Cleaner of course might be to further abstract it, and allow specifying the volume change via a command line argument as well.)

I hope it helps, or at least gives you some more ideas about what you can do with it. :)

(NOTE: I don't have snapcast myself, so I've based this on your commands and the examples found at https://github.com/badaix/snapcast/blob/master/doc/json_rpc_api/v2_0_0.md#clientgetstatus)

Further notes for if you plan to create a whole suite of snapcast scripts:

klundry commented 3 years ago

Ok, I'm getting out of "null" on the get-volume script. I think the issue lies with the jq command. Because if I skip the script and run the curl command directly I still get null

curl --silent -d '{"id":8,"jsonrpc":"2.0","method":"Client.GetStatus","params":{"id":"7c:c7:09:62:5e:ad"}}' http://192.168.1.4:1780/
jsonrpc | jq '.config.volume.percent'