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 #!/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
} }
api()
{
printf "https://%s%s\n" "$INVIDIOUS" "$*"
}
: ${INVIDIOUS:=invidious.snopyta.org}
torsocks=torsocks
while getopts T flag; do
case $flag in
T) torsocks=
;;
*) usage
;;
esac
done
shift $((OPTIND - 1))
[ $# -eq 1 ] || usage [ $# -eq 1 ] || usage
case $1 in # XXX check URL for known sites
"https://invidio.us/watch?v="*) url=$1
;;
*) err "Not an invidio.us url."
;;
esac
video_id= oldIFS=$IFS
for param in $(printf "%s\n" "${1##*"?"}" | tr "&" "\n"); do IFS="&"
k=${param%%=*} set -- ${url##*"?"}
[ "$k" = v ] || continue IFS=$oldIFS
v=${param#*=} id=
if [ ${#v} -ne 11 ]; then for param; do
printf "%s: \"%s\": not a video ID." "${0##*/}" "$v" >&2 case $param in
continue v=*) id=${param#v=}
fi [ ${#id} -eq 11 ] || err "invalid video ID"
video_id=$v ;;
break 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"