Skip to content

Commit

Permalink
fix(ytdl-mpv): improve alpine linux compatibility
Browse files Browse the repository at this point in the history
best command test availability and universal usage
for both `iconv` and `gnu-iconv` shipped by alpine
  • Loading branch information
andros21 committed Dec 10, 2023
1 parent 26aeb66 commit 6f1ebb9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 8 additions & 4 deletions bin/mpvctl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ _checkDep() {
for dep in "${deps[@]}"; do
type "$dep" > /dev/null 2>&1 || _die "Cannot find ${dep} in your \$PATH"
done
(echo "D̲eep̲ ̲P̲u̲r̲ple" | iconv -f utf-8 -t ascii//translit &>/dev/null) \
|| _die "Unsupported iconv flavour, install gnu-libiconv"
ICONV=$(which iconv)
type "gnu-iconv" >/dev/null 2>&1 && ICONV=$(which gnu-iconv)
local test
test=$(echo "D̲eep̲ ̲P̲u̲r̲ple" | $ICONV -f utf-8 -s -c -t ascii)
[[ "$test" == "Deep Purple" ]] ||
_die "Unsupported iconv flavour, install gnu-iconv"
SOCKCMD="socat - $SOCKET"
}

Expand Down Expand Up @@ -131,7 +135,7 @@ _getPlaylist() {
csv=$(yt-dlp --dump-json "https://youtube.com/watch?v=${trid}" \
| sed 's/\xE2\x80\x9D//g;s/\xE2\x80\x9C//g' \
| jq -r '. | {query:"NULL",id:.id,title:.title,nchapters:(.chapters|length),chapters:(.chapters|if .==null then [] else [.[].title] end)} | map(tostring) | @csv' \
| iconv -f utf-8 -t ascii//translit
| ($ICONV -f utf-8 -c -t ascii || true)
)
# cache this single search as relative to a NULL query
sqlite3 "${db}" "insert into main (query,id,title,nchapters,chapters) values (${csv})"
Expand All @@ -141,7 +145,7 @@ _getPlaylist() {
# searching track title, using ytdl
local trtitle
trtitle=$(yt-dlp --get-title "https://youtube.com/watch?v=${trid}" \
| iconv -f utf-8 -t ascii//translit)
| ($ICONV -f utf-8 -c -t ascii || true))
fi
if [ -z "$trtitle" ]; then
printf '[Warning] yt-dlp title search failed for %s\n' "$trid" >&2
Expand Down
10 changes: 7 additions & 3 deletions bin/ytdl-mpv
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ _checkDep() {
fi
}
done
(echo "D̲eep̲ ̲P̲u̲r̲ple" | iconv -f utf-8 -t ascii//translit &>/dev/null) \
|| _die "Unsupported iconv flavour, install gnu-libiconv"
ICONV=$(which iconv)
type "gnu-iconv" >/dev/null 2>&1 && ICONV=$(which gnu-iconv)
local test
test=$(echo "D̲eep̲ ̲P̲u̲r̲ple" | $ICONV -f utf-8 -s -c -t ascii)
[[ "$test" == "Deep Purple" ]] ||
_die "Unsupported iconv flavour, install gnu-iconv"
}

# Ensure internet connection is on
Expand Down Expand Up @@ -518,7 +522,7 @@ _startPlay() {
ytsearch"$NUMBER" "$search" --dump-single-json \
| query=$query jq -r '.entries[] | {query:env.query,id:.id,title:.title,nchapters:(.chapters|length),chapters:(.chapters|if .==null then [] else [.[].title] end)} | map(tostring) | @csv' \
| sed 's/\xE2\x80\x9D//g;s/\xE2\x80\x9C//g' \
| iconv -f utf-8 -t ascii//translit \
| ($ICONV -f utf-8 -c -t ascii || true) \
> "$TMPDIR/$query" &
wait "$!"; yt_dlp_exit="$?"
[[ "$yt_dlp_exit" -eq 0 ]] || _die "yt-dlp search fail, exit code ${yt_dlp_exit}"
Expand Down

0 comments on commit 6f1ebb9

Please sign in to comment.