PlayerId=$(curl -s --data-binary '{ "jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": "mybash"}' -H 'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
PlayerId=(`echo "$PlayerId" | grep "result"`)
PlayerType=$( echo "$PlayerId" | cut -d',' -f4)
PlayerType=$(echo "$PlayerType" | cut -d':' -f2 | sed 's/"//'g | sed 's/}//g' | sed 's/]//'g )
#echo "Player type is : $PlayerType"
PlayerId=$(echo "$PlayerId" | cut -d'{' -f3 | sed 's/[^0-9]*//g')
NowPlayingRecord=$(curl -s --data-binary '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "showtitle", "streamdetails","title"], "playerid": '"$PlayerId"' }, "id": "VideoGetItem"}'
-H 'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
ShowTitle=$(echo "$NowPlayingRecord" | awk -Fshowtitle '{print $2}' | awk -Fstreamdetails '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
Type=$(echo "$NowPlayingRecord" | awk -Ftype '{print $2}' | sed 's/"://g'| sed 's/[",}]//g')
if [ "$Type" = "episode" ]
then
showDetails=$(curl -s --data-binary '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "season", "episode"], "playerid": '"$PlayerId"' }, "id": "VideoGetItem"}'
-H 'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
Episode=$(echo "$showDetails" | awk -Fepisode '{print $2}' | awk -Fid '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
Title=$(echo "$showDetails" | awk -Flabel '{print $2}' | awk -Fseason '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
Season=$(echo "$showDetails" | awk -Fseason '{print $2}' | awk -Ftype '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
echo "Playing $ShowTitle - S$Season E$Episode - $Title"
else
if [ "$Type" = "movie" ]
then
movieDetails=$(curl -s --data-binary '{"jsonrpc": "2.0", "method": "Player.GetItem", "params": { "properties": [ "title","runtime"], "playerid": '"$PlayerId"' }, "id": "VideoGetItem"}'
-H 'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
currentPosition=$(curl -s --data-binary '{"jsonrpc":"2.0","method":"Player.GetProperties","params":{"playerid":'"$PlayerId"',"properties":["time"]},"id":"1"}'
-H 'content-type: application/json;' http://$xbmcAddress:$xbmcPort/jsonrpc)
# percentage , totaltime, time
currenttime=$(echo "$currentPosition" | awk -Fhours '{print $2}' | awk -Fmilliseconds '{print $1}' | sed 's/"://g'| sed 's/[",]//g')
currenttime=$(($currenttime * 60)) # currenttime now = hours
currenttime2=$(echo "$currentPosition" | awk -Fminutes '{print $2}' | awk -Fseconds '{print $1}' | sed 's/"://g'| sed 's/[",]//g')
currenttime=$(($currenttime + $currenttime2))
movieTitle=$(echo "$movieDetails" | awk -Ftitle '{print $2}' | awk -Ftype '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
runtime=$(echo "$movieDetails" | awk -Fruntime '{print $2}' | awk -Ftitle '{print $1}' | sed 's/"://g'| sed 's/[",]//g')
runtime=$(( $runtime / 60 ))
runtime=$(( $runtime - $currenttime ))
echo "Playing film - $movieTitle - $runtime mins remaining"
else
if [ "$Type" = "channel" ]
then
Channel=$(echo "$NowPlayingRecord" | awk -Flabel '{print $2}' | awk -Ftitle '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
Title=$(echo "$NowPlayingRecord" | awk -Ftitle '{print $2}' | awk -Ftype '{print $1}' | sed 's/"://g'| sed 's/[",]//g')
# echo "Title is $Title"
echo -e "Watching \${color green}$Title\${color white} on \${color teal}$Channel\${color}"
else
if [ "$Type" = "unknown" ]
then
Video=$(echo "$NowPlayingRecord" | awk -Flabel '{print $2}' | awk -Fshowtitle '{print $1}'| sed 's/"://g'| sed 's/[",]//g')
echo "Playing video - $Video"
fi
fi
fi
fi
fi
exit 0