summaryrefslogtreecommitdiff
path: root/misc/userscripts/kodi
blob: 63fcc81fed51d89a43671c8ee361f9a1bb2af9af (plain)
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
#!/usr/bin/env bash
#
# Behavior:
#   A qutebrowser userscript that plays Twitch, YouTube or Vimeo videos in Kodi via its
#   API.
#
#   Requirements:
#     awk
#     bash
#     curl
#
#  Kodi setup:
#    Settings -> Services -> Control
#      enable 'Allow remote control via HTTP'
#      set Username and Password
#      enable 'Allow remote control from applications on this system'
#     Optional yet recommended, setup SSL within Kodi over via a proxy webserver
#
#  userscript setup:
#    create ~/.config/qutebrowser/kodi_rc with host and authentication information like:
#
#      HOST="http://127.0.0.1:8080"
#      or
#      HOST="https://kodi.example.com"
#
#      AUTH="user:password"
#      or
#      AUTH="bas64authenticationinformation"
#
#      The base64 authentication is the output of
#      `echo -ne "user:password" |base64 --wrap 0`
#      reminder base64 is not encryption
#
#      For vim users you might want to add '# vim: set nospell filetype=bash' to the
#      kodi_rc file.
#
#  qutebrowser setup:
#    in ~/.config/qutebrowser/config.py add something like
#
#      to send video link via hints:
#        config.bind('X', 'hint links userscript kodi')
#      to send current URL:
#        config.bind('X', 'spawn --userscript kodi')
#
#   troubleshooting:
#     Errors detected within this userscript with have an exit of 231.  All other exit
#     codes will come from curl or awk.  To test that the kodi_rc file is set up
#     correctly, run the following command. It will display a 'It works!' notification within Kodi.
#
#     source ~/.config/qutebrowser/kodi_rc ; curl --request POST "$HOST"/jsonrpc --header "Authorization: Basic $AUTH" --header "Content-Type: application/json" --data '{"id":1,"jsonrpc":"2.0","method":"GUI.ShowNotification","params":{"title":"It works!","message":"both HOST and AUTH are correct"}}'
#
#     In case you miss the notification in Kodi the successful response is:
#
#     {"id":1,"jsonrpc":"2.0","result":"OK"}
#
#     Note, curl will display errors for some problems, but not all.

if [[ -z "$QUTE_FIFO" ]] ; then
	echo "This script is designed to run as a qutebrowser userscript, not as a standalone script."
	exit 231
fi

# configuration loading adapted from the password_fill userscript
QUTE_CONFIG_DIR=${QUTE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/qutebrowser/}
KODI_CONFIG=${PWFILL_CONFIG:-${QUTE_CONFIG_DIR}/kodi_rc}
if [[ -f "$KODI_CONFIG" ]] ; then
	# shellcheck source=/dev/null
	source "$KODI_CONFIG"
	if [[ -z "$HOST" || -z "$AUTH" ]] ; then
		echo "message-error 'HOST and/or AUTH not set in $KODI_CONFIG'" > "$QUTE_FIFO"
		exit 231
	fi
else
	echo "message-error '$KODI_CONFIG not found'" > "$QUTE_FIFO"
	exit 231
fi

# get real URL from twitter links
if [[ "$QUTE_URL" =~ ^https:\/\/t\.co ]] ; then
	QUTE_URL=$(curl -o /dev/null --silent --head --write-out '%{redirect_url}' "$QUTE_URL" )
fi

# regex from https://github.com/dirkjanm/firefox-send-to-xbmc/blob/master/webextension/main.js
if [[ "$QUTE_URL" =~  ^.*twitch.tv\/([a-zA-Z0-9_]+)$ ]] ; then
	NAME="${BASH_REMATCH[1]}"
	JSON='{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"plugin://plugin.video.twitch/?mode=play&channel_name='$NAME'"}},"id":"2"}'

elif [[ "$QUTE_URL" =~ ^.*twitch.tv\/videos\/([0-9]+)$ ]] ; then
	NAME="${BASH_REMATCH[1]}"
	JSON='{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"plugin://plugin.video.twitch/?mode=play&video_id='$NAME'"}},"id":"2"}'

elif [[ "$QUTE_URL" =~ ^.*vimeo.com\/([0-9]+) ]] ; then
	NAME="${BASH_REMATCH[1]}"
	JSON='{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"plugin://plugin.video.vimeo/play/?video_id='$NAME'"}},"id":"2"}'

elif [[ "$QUTE_URL" =~ ^.*youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=([^#\&\?]*).*  ]] ; then
	NAME="${BASH_REMATCH[1]}"
	JSON='{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"plugin://plugin.video.youtube/play/?video_id='$NAME'"}},"id":"2"}'
fi

if [[ "$JSON" ]] ; then
	curl \
	--request POST "$HOST"/jsonrpc \
	--header "Authorization: Basic $AUTH" \
	--header "Content-Type: application/json" \
	--data "$JSON" \
	--silent > /dev/null
else
	URL=$(echo "$QUTE_URL" |awk -F/ '{print $3}')
	echo "message-warning 'kodi userscript does not support this $URL'" > "$QUTE_FIFO"
fi