commit 02c88e3
uint
·
2026-03-15 17:15:38 +0000 UTC
parent 26018fe
shrados: fix seeking by plaing direct stream urls
1 files changed,
+64,
-14
+64,
-14
1@@ -5,11 +5,6 @@
2
3 set -u
4
5-# Options:
6-# CACHE_LOGIN: 1 to cache auth in ~/.cache/shrados.auth, 0 to disable.
7-# VIDEO_PLAYER: player command used by "watch n".
8-# VIDEO_PLAYER_ARGS: extra args appended before "-" stdin input.
9-
10 CACHE_LOGIN=${CACHE_LOGIN:-1}
11 VIDEO_PLAYER=${VIDEO_PLAYER:-mpv}
12 VIDEO_PLAYER_ARGS=${VIDEO_PLAYER_ARGS:-}
13@@ -84,13 +79,31 @@ is_video_path()
14 esac
15 }
16
17+# percent encode credentials to be safely embedded in the URL
18+urlencode_userinfo()
19+{
20+ LC_ALL=C awk -v s="$1" '
21+ BEGIN {
22+ safe = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~"
23+ for (i = 1; i < 256; i++)
24+ ord[sprintf("%c", i)] = i
25+ for (i = 1; i <= length(s); i++) {
26+ c = substr(s, i, 1)
27+ if (index(safe, c) > 0)
28+ printf "%s", c
29+ else
30+ printf "%%%02X", ord[c]
31+ }
32+ }'
33+}
34+
35 # do a HTTP GET request, save body to file, return HTTP code
36 http_get()
37 {
38 if [ -n "$AUTH" ]; then
39- "$PARADOS_CURL" -sS -u "$AUTH" -o "$2" -w '%{http_code}' "${PARADOS_URL}$1"
40+ "$PARADOS_CURL" -q -sS -u "$AUTH" -o "$2" -w '%{http_code}' "${PARADOS_URL}$1"
41 else
42- "$PARADOS_CURL" -sS -o "$2" -w '%{http_code}' "${PARADOS_URL}$1"
43+ "$PARADOS_CURL" -q -sS -o "$2" -w '%{http_code}' "${PARADOS_URL}$1"
44 fi
45 }
46
47@@ -282,7 +295,47 @@ cmd_cd()
48 CUR_DIR=$ENTRY_FULL
49 }
50
51-# given ls index, get video id, call "/stream/id", pipe to VIDEO_PLAYER, print errors
52+# build URL for "/stream/id", embedding auth if needed for seeking
53+build_stream_url()
54+{
55+ base=${PARADOS_URL%/}
56+ path="/stream/$1"
57+
58+ [ -n "$AUTH" ] || {
59+ printf '%s%s' "$base" "$path"
60+ return 0
61+ }
62+
63+ case "$AUTH" in
64+ *:)
65+ user=${AUTH%:}
66+ pass=""
67+ ;;
68+ *:*)
69+ user=${AUTH%%:*}
70+ pass=${AUTH#*:}
71+ ;;
72+ *)
73+ user=$AUTH
74+ pass=""
75+ ;;
76+ esac
77+
78+ case "$base" in
79+ *://*)
80+ user_enc=$(urlencode_userinfo "$user") || return 1
81+ pass_enc=$(urlencode_userinfo "$pass") || return 1
82+ scheme=${base%%://*}
83+ rest=${base#*://}
84+ printf '%s://%s:%s@%s%s' "$scheme" "$user_enc" "$pass_enc" "$rest" "$path"
85+ ;;
86+ *)
87+ printf '%s%s' "$base" "$path"
88+ ;;
89+ esac
90+}
91+
92+# given ls index, get video id, launch VIDEO_PLAYER on streams URL
93 cmd_watch()
94 {
95 [ $# -eq 1 ] || { printf '%s\n' "usage: watch <n>" >&2; return 1; }
96@@ -293,14 +346,11 @@ cmd_watch()
97 [ "$ENTRY_TYP" = "F" ] || { printf '%s\n' "index is not a video: $1" >&2; return 1; }
98 validate_id "$ENTRY_ID" || { printf '%s\n' "invalid id in index: $1" >&2; return 1; }
99 cmd_exists "$VIDEO_PLAYER"
100+ stream_url=$(build_stream_url "$ENTRY_ID") || { printf '%s\n' "failed building stream URL" >&2; return 1; }
101 printf 'watching: %s\n' "$ENTRY_NAME"
102 # shellcheck disable=SC2086
103- set -- "$VIDEO_PLAYER" $VIDEO_PLAYER_ARGS -
104- if [ -n "$AUTH" ]; then
105- "$PARADOS_CURL" -sS -u "$AUTH" "${PARADOS_URL}/stream/$ENTRY_ID" | "$@"
106- else
107- "$PARADOS_CURL" -sS "${PARADOS_URL}/stream/$ENTRY_ID" | "$@"
108- fi
109+ set -- "$VIDEO_PLAYER" $VIDEO_PLAYER_ARGS "$stream_url"
110+ "$@"
111 }
112
113 # call "/rescan", print response or error