Linux Want to install a plugin from uknown sources via JSON-RPC? Here is how
#1
I'm working on automating an installation for my pi running buster. I want to be able to setup a fresh raspberry pi os installation, and then run some bash scripts to automate the installation of kodi and a couple of plugins. This example shows you how to install the castagnait repository which hosts the plugin.video.netflix. This will only work, if the json-rpc interface for kodi is enabled, of course.

First, you should run the kodi-variables.sh script to set some locations:
 
bash:
variablesScript=$(basename "$0")

kodiHome=~/.kodi
downloadDir=~/Downloads
jsonRpcPort=8080
jsonRpcAddress=localhost

if [ "$variablesScript" == "kodi-variables.sh" ]
then

read -e -i "$kodiHome" -p "Provide kodi home path: " kodiHomeInput
kodiHome=${kodiHomeInput:-$kodiHome}
sed -i -E "s|^(kodiHome=)/.*$|\1$kodiHome|g" "$variablesScript"

read -e -i "$downloadDir" -p "Provide the Downloads directoy location: " downloadDirInput
downloadDir=${downloadDirInput:-$downloadDir}
sed -i -E "s|^(downloadDir=)/.*$|\1$downloadDir|g" "$variablesScript"

read -e -i "$jsonRpcPort" -p "Define the tcp port the jsonRpc server should listen on: " jsonRpcPortInput
jsonRpcPort=${jsonRpcPortInput:-$jsonRpcPort}
sed -i -E "s|^(jsonRpcPort=)[0-9]*$|\1$jsonRpcPort|g" "$variablesScript"

read -e -i "$jsonRpcAddress" -p "Define the address the jsonRpc server will listen on: " jsonRpcAddressInput
jsonRpcAddress=${jsonRpcAddressInput:-$jsonRpcAddress}
sed -i -E "s%^(jsonRpcAddress=).*$%\1$jsonRpcAddress%g" "$variablesScript"

fi

I figured out how to fire post requests without having to wait for a reply. That's done with ncat. For that to work, http requests need to be generated manually, therefore I created a supporting script that helps with that: http-request.sh
bash:
function generateHttpRequest () {
method=$1
endpoint=$2
contentType=$3
targetAddress=$4
targetPort=$5
payload=$6
local result=$(cat <<EOF
$method $endpoint HTTP/1.1
Content-Type: $contentType
Accept: */*
Host: $targetAddress:$targetPort
Connection: close
Content-Length: ${#payload}

$payload
EOF
)
echo "$result"
}

Now, if we want to install plugins or repositories from unknown sources, we'll first need to enable that option. That's what this script does (call it what you want):
bash:
source kodi-variables.sh
source http-request.sh

dpkg -s ncat &> /dev/null
ncatInstalled=$?
if [ $ncatInstalled -ne 0 ]
then
sudo apt -yq install ncat
fi

unknownSourcesJson='{"jsonrpc":"2.0","id":1,"method": "Settings.SetSettingValue", "params": {"setting": "addons.unknownsources", "value": true}}'
enableUnknownSourcesRequest=$(generateHttpRequest "POST" "/jsonrpc" "application/json" "$jsonRpcAddress" "$jsonRpcPort" "$unknownSourcesJson")
echo "$enableUnknownSourcesRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

leftJson='{"jsonrpc":"2.0","id":1,"method": "Input.ExecuteAction", "params": {"action": "left"}}'
leftRequest=$(generateHttpRequest "POST" "/jsonrpc" "application/json" "$jsonRpcAddress" "$jsonRpcPort" "$leftJson")
echo "$leftRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

selectJson='{"jsonrpc":"2.0","id":1,"method": "Input.ExecuteAction", "params": {"action": "select"}}'
selectRequest=$(generateHttpRequest "POST" "/jsonrpc" "application/json" "$jsonRpcAddress" "$jsonRpcPort" "$selectJson")
echo "$selectRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

if [ $ncatInstalled -ne 0 ]
then
sudo apt -yq purge ncat
fi

Lastly you can install the repository with this script:
bash:
source kodi-variables.sh
source http-request.sh

dpkg -s ncat &> /dev/null
ncatInstalled=$?
if [ $ncatInstalled -ne 0 ]
then
sudo apt -yq install ncat
fi

dpkg -s jq &> /dev/null
jqInstalled=$?
if [ $jqInstalled -ne 0 ]
then
sudo apt -yq install jq
fi

getVersionPropertyJson='{"jsonrpc":"2.0","id":1,"method": "Application.GetProperties", "params": {"properties":["version"]}}'
majorVersion=$(curl --silent -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getVersionPropertyJson" | jq ".result.version.major")

if [ ! -d "$downloadDir" ]
then
echo "$downloadDir does not exist" && exit
fi

if [ $majorVersion -eq 18 ]
then
if [ ! -f $downloadDir/repository.castagnait-1.0.1.zip ]
then
castagnaitRpositoryFileName=repository.castagnait-1.0.1.zip
wget https://github.com/castagnait/repository...-1.0.1.zip -P "$downloadDir"
fi
elif [ $majorVersion -eq 19 ]
then
if [ ! -f $downloadDir/repository.castagnait-1.0.0.zip ]
then
castagnaitRpositoryFileName=repository.castagnait-1.0.0.zip
wget https://github.com/castagnait/repository...-1.0.0.zip -P "$downloadDir"
fi
else
echo "Could not determine kodi version, check if jsonrpc interface is active." && exit
fi

installFromZipFile="Install from zip file"
homeFolder="Home folder"
downloadsFolder="Downloads"

addonWindowJson='{"jsonrpc":"2.0","method":"GUI.ActivateWindow","id":1,"params":{"window":"addonbrowser"}}'
addonWindowRequest=$(generateHttpRequest "POST" "/jsonrpc" "application/json" "$jsonRpcAddress" "$jsonRpcPort" "$addonWindowJson")
echo "$addonWindowRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

downJson='{"jsonrpc":"2.0","id":1,"method": "Input.ExecuteAction", "params": {"action": "down"}}'
downRequest=$(generateHttpRequest "POST" "/jsonrpc" "application/json" "$jsonRpcAddress" "$jsonRpcPort" "$downJson")

selectJson='{"jsonrpc":"2.0","id":1,"method": "Input.ExecuteAction", "params": {"action": "select"}}'
selectRequest=$(generateHttpRequest "POST" "/jsonrpc" "application/json" "$jsonRpcAddress" "$jsonRpcPort" "$selectJson")

getinfolabelJson='{"jsonrpc":"2.0","method":"XBMC.GetInfoLabels","id":1,"params":{"labels":["ListItem.Label"]}}'
label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
while [ "$label" != "$installFromZipFile" ]
do
echo "$downRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only
label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
done

echo "$selectRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
while [ "$label" != "$homeFolder" ]
do
echo "$downRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only
label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
done

echo "$selectRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
while [ "$label" != "$downloadsFolder" ]
do
echo "$downRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only
label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
done

echo "$selectRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
while [ "$label" != "$castagnaitRpositoryFileName" ]
do
echo "$downRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only
label=$(curl -s -X POST -H 'Content-Type: application/json' http://"$jsonRpcAddress":"$jsonRpcPort"/jsonrpc --data "$getinfolabelJson" | jq -r '.result."ListItem.Label"' )
done

echo "$selectRequest" | ncat "$jsonRpcAddress" "$jsonRpcPort" --send-only

if [ $ncatInstalled -ne 0 ]
then
sudo apt -yq purge ncat
fi

if [ $jqInstalled -ne 0 ]
then
sudo apt -yq purge jq
fi

I'd be very happy if you provided some feedback.
Reply
#2
I uploaded the scripts to the paste site too, somehow identation got removed in the forum editor:
kodi-variables.sh
http-request.sh
enable-unknown-sources.sh
install-castagnait-repo.sh
Reply

Logout Mark Read Team Forum Stats Members Help
Want to install a plugin from uknown sources via JSON-RPC? Here is how0