invidious: rewrite

- Use API cleverly
- Allow specifying a provider different than invidio.us
- Use torsocks by default
This commit is contained in:
Lucas 2020-05-02 19:39:59 +00:00
parent aa86bb5c6e
commit 0a7dd126e8

View File

@ -1,7 +1,7 @@
#!/bin/sh
# invidious
# Written in 2019 by Leslie
# Modified in 2019 by Lucas
# Modified in 2019-2020 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
@ -13,7 +13,7 @@
usage()
{
printf "Usage: %s url\n" "${0##*/}" >&2
printf "Usage: [-T] %s url\n" "${0##*/}" >&2
exit 1
}
@ -23,33 +23,45 @@ err()
exit 1
}
[ $# -eq 1 ] || usage
api()
{
printf "https://%s%s\n" "$INVIDIOUS" "$*"
}
case $1 in
"https://invidio.us/watch?v="*)
: ${INVIDIOUS:=invidious.snopyta.org}
torsocks=torsocks
while getopts T flag; do
case $flag in
T) torsocks=
;;
*) err "Not an invidio.us url."
*) usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || usage
video_id=
for param in $(printf "%s\n" "${1##*"?"}" | tr "&" "\n"); do
k=${param%%=*}
[ "$k" = v ] || continue
# XXX check URL for known sites
url=$1
v=${param#*=}
if [ ${#v} -ne 11 ]; then
printf "%s: \"%s\": not a video ID." "${0##*/}" "$v" >&2
continue
fi
video_id=$v
break
oldIFS=$IFS
IFS="&"
set -- ${url##*"?"}
IFS=$oldIFS
id=
for param; do
case $param in
v=*) id=${param#v=}
[ ${#id} -eq 11 ] || err "invalid video ID"
;;
esac
done
[ -n "$video_id" ] || err "no video ID in URL."
[ -n "$id" ] || err "no video ID in URL"
dl_option=$(ftp -o - "$1" 2>/dev/null | grep -F "<option value='{" |
head -n 1 | cut -d "\"" -f 8)
printf "https://invidio.us/latest_version?id=%s&itag=%s\n" \
"$video_id" "$dl_option"
# XXX choose in some clever way
itag=$(torsocks ftp -MVo - "$(api "/api/v1/videos/$id")" |
jq -r ".formatStreams[].itag" | head -n 1)
api "/latest_version?id=$id&itag=$itag"