forked from nawar/kodi-cli
/
kodi-cli
executable file
·275 lines (232 loc) · 6.72 KB
/
kodi-cli
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/bash
## Author: Nawar
## License: GPL (c) 2014
##
## Note:
## This script used the youtube code of:
## YouTube XBMC Script 1.0
## (c) 2013, Tom Laermans - http://tom.laermans.net
## This script is released into the public domain.
##
## 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_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)
if [[ $2 = true ]];
then
echo $output
fi
}
function parse_json {
key=$1
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
xbmc_req '{"jsonrpc": "2.0", "method": "Playlist.Clear", "params":{"playlistid":1}, "id": 1}';
# 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}';
# open the video
xbmc_req '{"jsonrpc": "2.0", "method": "Player.Open", "params":{"item":{"playlistid":1, "position" : 0}}, "id": 1}';
echo " done."
}
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}';
echo " done."
}
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"`
echo "Pausing/Playing the player with ID => $player_id"
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"`
echo "Stopping the player with ID => $player_id"
xbmc_req '{"jsonrpc": "2.0", "method": "Player.Stop", "params": { "playerid": '$player_id' }, "id": 1}'
}
function send_text {
echo "Sending the text"
xbmc_req '{"jsonrpc": "2.0", "method": "Input.SendText", "params": { "text": "'$1'" }}'
}
function press_key {
ACTION=''
CLR="\e[K"
case "$1" in
A) ACTION='Up'
echo -ne "\rUp$CLR";
;;
B) ACTION='Down'
echo -ne "\rDown$CLR";
;;
C) ACTION='Right'
echo -ne "\rRight$CLR";
;;
D) ACTION='Left'
echo -ne "\rLeft$CLR";
;;
c) ACTION='ContextMenu'
echo -ne "\rContext Menu$CLR";
;;
i) ACTION='Info'
echo -ne "\rInformation$CLR";
;;
$'\177') ACTION='Back'
echo -ne "\rBack$CLR";
;;
"") ACTION='Select'
echo -ne "\rSelect$CLR";
;;
esac
if [[ "$ACTION" != " " ]] && [[ $LOCK == false ]]
then
LOCK=true
xbmc_req '{"jsonrpc": "2.0", "method": "Input.'$ACTION'"}'
LOCK=false
fi
}
function handle_keys {
echo "Interactive navigation key: ";
while :
do
read -s -n1 key
if [[ $key = q ]]
then
break
elif [[ $key != ' ' ]]
then
press_key "$key"
fi
done
}
function volume_up {
echo "Incrementing volume"
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 }'
}
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 {
# Get Active players first
output=`xbmc_req '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 99}' true`
player_id=`echo $output | parse_json "playerid"`
echo "Toggle fullscreen on the player with ID => $player_id"
xbmc_req '{ "jsonrpc": "2.0", "method": "GUI.SetFullscreen", "params": { "fullscreen": "toggle" }, "id": 1 }'
}
function show_help {
echo -e "\n kodi-cli -[p|i|h|s|y youtbe URL/ID|t 'text to send']\n\n" \
"-p Play/Pause the current played video\n" \
"-s Stop the current played video\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" \
"-u Increment the volume on Kodi\n" \
"-d Decrement the volume on Kodi\n" \
"-f toggle fullscreen\n" \
"-h showing this help message\n"
}
## Process command line arguments
while getopts "yqopstiudfhv" opt; do
case $opt in
y)
#play youtube video
play_youtube $2
;;
q)
#queue youtube video
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
;;
s)
stop
;;
t) send_text $2
handle_keys
;;
i)
handle_keys
;;
u)
volume_up
;;
d)
volume_down
;;
f)
fullscreen_toggle
;;
h)
show_help
;;
v)
handle_volume
;;
esac
done
if [ $# -eq 0 ];then
show_help
fi