-
Notifications
You must be signed in to change notification settings - Fork 0
/
2kodi
executable file
·108 lines (81 loc) · 2.49 KB
/
2kodi
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
#!/bin/bash
# Required settings
host=192.168.x.x
port=8080
# Optional login for Kodi
#user=
#pass=
#based on https://github.com/allejok96/send-to-kodi
# Settings for netcat (local file)
local_hostname=$(hostname)
local_port=12345
show_help()
{
cat<<EOF
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()
{
if type zenity &>/dev/null; then
zenity --error --ellipsize --text "$*"
else
echo "$*" 1>&2
fi
exit 1
}
send_json()
{
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?"
}
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
input="$(zenity --entry --title "Send to Kodi" --text "Paste a video link here")" || exit
done
if [[ $input =~ ^file:// ]]; then
# Remove file:// and carrige return (\r) at the end
input="$(sed 's%^file://%%;s/\r$//' <<< "$input")"
fi
# Get URL for...
# Local media
if [[ -e $input ]]; then
type nc &>/dev/null || error "netcat required"
[[ $local_hostname && $local_port ]] || error "Please set local hostname and port in configuration"
# Start netcat in background and kill it when we exit
nc -lp $local_port < "$input" &
trap "kill $!" EXIT
url="tcp://$local_hostname:$local_port"
# youtube.com / youtu.be
elif [[ $input =~ ^https?://(www\.)?youtu(\.be/|be\.com/watch\?v=) ]]; then
url="$ytplugin$(sed 's/.*\(youtu\.be\/\|[&?]v=\)\([a-zA-Z0-9_-]\+\).*/\2/' <<< "$input")"
# 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
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"
fi
[[ $url ]] && send_json "$url"
# Wait for netcat to exit
wait
# Don't kill netcat
trap - EXIT