scripts/send-to-kodi.sh

113 lines
2.7 KiB
Bash
Raw Normal View History

2020-09-11 15:00:39 +00:00
#!/bin/bash
2022-10-31 22:07:44 +00:00
# TODO replace `zenity` with `wofi`
2020-09-11 15:00:39 +00:00
# Required settings
host=192.168.2.66
2020-09-11 15:00:39 +00:00
port=8080
# Optional login for Kodi
user=kodi
pass=kodi
2020-09-11 15:00:39 +00:00
# Settings for netcat (local file)
local_hostname=$(hostname)
local_port=12345
show_help()
{
2020-11-23 09:35:26 +00:00
cat<<EOF
2020-09-11 15:00:39 +00:00
Sends a video URL to Kodi
Usage: send-to-kodi.sh [URL]
If no URL is given, a dialog window is shown (requires zenity).
Supports:
Common file formats (mp4,flv,mp3,jpg and more)
Youtube (requires the Youtube plugin in Kodi)
Local media streaming (via netcat)
Manny more sites (requires youtube-dl)
Configuration is done in the head of the script.
EOF
}
error()
{
2020-11-23 09:35:26 +00:00
if type zenity &>/dev/null; then
zenity --error --ellipsize --text "$*"
else
echo "$*" 1>&2
fi
2020-09-11 15:00:39 +00:00
2020-11-23 09:35:26 +00:00
exit 1
2020-09-11 15:00:39 +00:00
}
send_json()
{
2020-11-23 09:35:26 +00:00
curl \
${user:+--user "$user:$pass"} \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"'"$1"'"}},"id":1}' \
http://$host:$port/jsonrpc \
|| error "Failed to send link - is Kodi running?"
2020-09-11 15:00:39 +00:00
}
ytplugin='plugin://plugin.video.youtube/?action=play_video&videoid='
[[ $host && $port ]] || error "Please set host and port in configuration"
[[ "$1" = --help ]] && show_help
# Dialog box?
input=$1
until [[ $input ]]; do
2020-11-23 09:35:26 +00:00
input="$(zenity --entry --title "Send to Kodi" --text \
"Paste a video link here")" || exit
2020-09-11 15:00:39 +00:00
done
if [[ $input =~ ^file:// ]]; then
2020-11-23 09:35:26 +00:00
# Remove file:// and carrige return (\r) at the end
input="$(sed 's%^file://%%;s/\r$//' <<< "$input")"
2020-09-11 15:00:39 +00:00
fi
# Get URL for...
# Local media
if [[ -e $input ]]; then
2020-11-23 09:35:26 +00:00
type nc &>/dev/null || error "netcat required"
[[ $local_hostname && $local_port ]] || \
error "Please set local hostname and port in configuration"
2020-09-11 15:00:39 +00:00
2020-11-23 09:35:26 +00:00
# Start netcat in background and kill it when we exit
nc -lp $local_port < "$input" &
trap "kill $!" EXIT
2020-09-11 15:00:39 +00:00
2020-11-23 09:35:26 +00:00
url="tcp://$local_hostname:$local_port"
2020-09-11 15:00:39 +00:00
# youtube.com / youtu.be
elif [[ $input =~ ^https?://(www\.)?youtu(\.be/|be\.com/watch\?v=) ]]; then
2020-11-23 09:35:26 +00:00
url="$ytplugin$(\
sed 's/.*\(youtu\.be\/\|[&?]v=\)\([a-zA-Z0-9_-]\+\).*/\2/'\
<<< "$input"\
)"
2020-09-11 15:00:39 +00:00
# Playable formats
elif [[ $input =~ \.(mp4|mkv|mov|avi|flv|wmv|asf|mp3|flac|mka|m4a|aac|ogg|pls|jpg|png|gif|jpeg|tiff)(\?.*)?$ ]]; then
url="$input"
# Youtube-dl
else
2020-11-23 09:35:26 +00:00
type youtube-dl &>/dev/null || error "youtube-dl required"
url="$(youtube-dl -gf best "$input")" || \
error "No videos found, or site not supported by youtube-dl"
2020-09-11 15:00:39 +00:00
fi
[[ $url ]] && send_json "$url"
# Wait for netcat to exit
wait
# Don't kill netcat
trap - EXIT