CCCPaste

Kodi Shell Remote Control for WebUI

#!/bin/bash
###################################################################### 
#Copyright (C) 2021  Kris Occhipinti
#https://filmsbykris.com

#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 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see .
###################################################################### 
ip="192.168.1.4"
port="8080"
cmd="  play-pause"

function main(){
  playerid="$(get_playerid)"
  while [ 1 ]
  do
    menu  
  done
}

function get_playerid(){
  send_cmd '[{"jsonrpc":"2.0","method":"Player.GetActivePlayers","id":1,"params":[]},{"jsonrpc":"2.0","method":"Application.GetProperties","id":2,"params":[["volume","muted"]]}]'|\
    jq -r '.[0].result[0].playerid'
  }

function menu(){
  cmd="$(echo "$cmd
  quit
  play-pause
  seek 10
  seek -10
  seek 30
  seek -30
  volume"|fzf)"
  $cmd
}

function quit(){
  exit
}

function send_cmd(){
  curl -s 'http://192.168.1.4:8080/jsonrpc' \
    -H 'Connection: keep-alive' \
    -H 'Accept: application/json, text/javascript, */*; q=0.01' \
    -H 'X-Requested-With: XMLHttpRequest' \
    -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36' \
    -H 'Content-Type: application/json' \
    -H 'Sec-GPC: 1' \
    -H "Origin: http://$ip:$port" \
    -H "Referer: http://$ip:$port/" \
    -H 'Accept-Language: en-US,en;q=0.9' \
    --data-raw "$*" \
    --compressed \
    --insecure|jq .

}

function play-pause(){
  send_cmd "{\"jsonrpc\":\"2.0\",\"method\":\"Player.PlayPause\",\"id\":1,\"params\":[$playerid,\"toggle\"] }"
}

function seek(){
  send_cmd "{\"jsonrpc\":\"2.0\", \"method\":\"Player.Seek\", \"params\": { \"playerid\":$playerid, \"value\":{ \"seconds\": $* } }, \"id\":1}" 
}

function volume(){
  volume="$(seq 0 100|fzf --prompt "Select a Volume: ")"
  send_cmd "{\"jsonrpc\":\"2.0\",\"method\":\"Application.SetVolume\",\"id\":1,\"params\":[$volume]}"
}

main $*