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
1 changed files with 37 additions and 25 deletions

View File

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