env/bin/invidious.sh
Lucas 0a7dd126e8 invidious: rewrite
- Use API cleverly
- Allow specifying a provider different than invidio.us
- Use torsocks by default
2020-05-02 19:39:59 +00:00

68 lines
1.3 KiB
Bash

#!/bin/sh
# invidious
# Written in 2019 by Leslie
# 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
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
usage()
{
printf "Usage: [-T] %s url\n" "${0##*/}" >&2
exit 1
}
err()
{
printf "%s: %s\n" "${0##*/}" "$*" >&2
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
# XXX check URL for known sites
url=$1
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 "$id" ] || err "no video ID in URL"
# 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"