Skip to content

Commit

Permalink
Merge pull request #7 from jordanhillis/patch-1
Browse files Browse the repository at this point in the history
Created -v option for interactive volume control
  • Loading branch information
nawar authored Oct 23, 2017
2 parents 846ed09 + 510260d commit e7a01e8
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions kodi-cli
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
## Author: Nawar
## Author: Nawar
## License: GPL (c) 2014
##
## Note:
Expand All @@ -11,46 +11,46 @@
## Volume control added by elpraga
## using the command found at http://forum.kodi.tv/showthread.php?tid=176795


## Configure your KODI RPC details here
#set -x
KODI_HOST=127.0.0.1
KODI_PORT=8080

KODI_HOST=
KODI_PORT=
KODI_USER=
KODI_PASS=
LOCK=false

function xbmc_req {
output=$(curl -s -i -X POST --header "Content-Type: application/json" -d "$1" http://$KODI_USER:$KODI_PASS@$KODI_HOST:$KODI_PORT/jsonrpc)
output=$(curl -s -i -X POST --header "Content-Type: application/json" -d "$1" http://$KODI_USER:$KODI_PASS@$KODI_HOST:$KODI_PORT/jsonrpc)

if [[ $2 = true ]];
then
echo $output
fi
fi
}

function parse_json {
key=$1
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$key'\042/){print $(i+1)}}}' | tr -d '"'
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$key'\042/){print $(i+1)}}}' | tr -d '"'
}

function play_youtube {

REGEX="^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*"

ID=$1

if [ "$ID" == "" ];
then
echo "Syntax $0:$1 <id|url>"
exit
fi

if [[ $ID =~ $REGEX ]]; then
ID=${BASH_REMATCH[7]}
fi

echo -n "Opening video id $ID on $KODI_HOST ..."

# clear the list
Expand All @@ -68,22 +68,22 @@ function play_youtube {
function queue_yt_videos {

REGEX="^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*"

ID=$1

if [ "$ID" == "" ];
then
echo "Syntax $0:$1 <id|url>"
exit
fi

if [[ $ID =~ $REGEX ]]; then
ID=${BASH_REMATCH[7]}
fi

output=`xbmc_req '{"jsonrpc": "2.0", "method": "Playlist.GetItems", "params":{"playlistid":1}, "id": 99}' true`
numberitems=`echo $output | parse_json "total"`

echo -n "Video added to the current playlist $ID on $KODI_HOST which has ($numberitems) items..."
# add the video to the list
xbmc_req '{"jsonrpc": "2.0", "method": "Playlist.Add", "params":{"playlistid":1, "item" :{ "file" : "plugin://plugin.video.youtube/?action=play_video&videoid='$ID'"}}, "id" : 1}';
Expand All @@ -94,15 +94,15 @@ function queue_yt_videos {
function play_pause {
# Get Active players first
output=`xbmc_req '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 99}' true`
player_id=`echo $output | parse_json "playerid"`
player_id=`echo $output | parse_json "playerid"`
echo "Pausing/Playing the player with ID => $player_id"
xbmc_req '{"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": '$player_id' }, "id": 1}'
xbmc_req '{"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": '$player_id' }, "id": 1}'
}

function stop {
# Get Active players first
output=`xbmc_req '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 99}' true`
player_id=`echo $output | parse_json "playerid"`
player_id=`echo $output | parse_json "playerid"`
echo "Stopping the player with ID => $player_id"
xbmc_req '{"jsonrpc": "2.0", "method": "Player.Stop", "params": { "playerid": '$player_id' }, "id": 1}'
}
Expand All @@ -118,7 +118,7 @@ function press_key {
CLR="\e[K"

case "$1" in
A) ACTION='Up'
A) ACTION='Up'
echo -ne "\rUp$CLR";
;;
B) ACTION='Down'
Expand Down Expand Up @@ -163,19 +163,37 @@ do
break
elif [[ $key != ' ' ]]
then
press_key "$key"
press_key "$key"
fi
done
}

function volume_up {
echo "Incrementing volume"
xbmc_req '{ "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "increment" }, "id": 1 }'
xbmc_req '{ "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "increment" }, "id": 1 }'
}

function volume_down {
echo "Decrementing volume on"
xbmc_req '{ "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "decrement" }, "id": 1 }'
xbmc_req '{ "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "decrement" }, "id": 1 }'
}

function handle_volume {
echo "Press up/down for volume adjustment (q to quit): ";
while :
do
read -s -n1 key
if [[ $key = q ]];then
printf "\n"
break
elif [[ $key == 'A' ]]; then
printf "\r+ Volume increasing."
xbmc_req '{ "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "increment" }, "id": 1 }'
elif [[ $key == 'B' ]];then
printf "\r- Volume decreasing."
xbmc_req '{ "jsonrpc": "2.0", "method": "Application.SetVolume", "params": { "volume": "decrement" }, "id": 1 }'
fi
done
}

function fullscreen_toggle {
Expand All @@ -194,6 +212,7 @@ echo -e "\n kodi-cli -[p|i|h|s|y youtbe URL/ID|t 'text to send']\n\n" \
"-y play youtube video. Use either URL/ID (of video)\n" \
"-q queue youtube video to the current list. Use either URL/ID (of video). Use instead of -y.\n" \
"-o play youtube video directly on Kodi. Use the name of video.\n" \
"-v interactive volume control\n" \
"-i interactive navigation mode. Accept keyboard keys of Up, Down, Left, Right, Back,\n" \
" Context menu and information\n" \
"-t 'text to send'\n" \
Expand All @@ -205,44 +224,52 @@ echo -e "\n kodi-cli -[p|i|h|s|y youtbe URL/ID|t 'text to send']\n\n" \
}

## Process command line arguments
while getopts "yqopstiudfh" opt; do
while getopts "yqopstiudfhv" opt; do
case $opt in
y)
#play youtube video
play_youtube $2
play_youtube $2
;;
q)
#queue youtube video
queue_yt_videos $2
queue_yt_videos $2
;;
o)
#play youtube video directly
#this depends on using mps-youtube
play_youtube `mpsyt /$2, i 1, q | grep -i link | awk -F 'v=' '{ print $2 }'`
;;
p)
play_pause
play_pause
;;
s)
s)
stop
;;
t) send_text $2
handle_keys
;;
i)
handle_keys
handle_keys
;;
u)
volume_up
;;
d)
volume_down
volume_down
;;
f)
fullscreen_toggle
;;
fullscreen_toggle
;;
h)
show_help
show_help
;;
v)
handle_volume
;;
esac
done


if [ $# -eq 0 ];then
show_help
fi

0 comments on commit e7a01e8

Please sign in to comment.