2019-09-11 04:11:40 +02:00
|
|
|
#!/bin/sh
|
2019-12-08 19:14:46 +01:00
|
|
|
# invidious
|
2019-09-11 04:11:40 +02:00
|
|
|
# Written in 2019 by Leslie
|
2020-05-02 21:39:59 +02:00
|
|
|
# Modified in 2019-2020 by Lucas
|
2019-09-11 04:11:40 +02:00
|
|
|
# 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/>.
|
|
|
|
|
2019-12-08 19:14:46 +01:00
|
|
|
usage()
|
|
|
|
{
|
2020-05-02 21:39:59 +02:00
|
|
|
printf "Usage: [-T] %s url\n" "${0##*/}" >&2
|
2019-09-11 04:11:40 +02:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-12-08 19:14:46 +01:00
|
|
|
err()
|
|
|
|
{
|
|
|
|
printf "%s: %s\n" "${0##*/}" "$*" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:39:59 +02:00
|
|
|
api()
|
|
|
|
{
|
|
|
|
printf "https://%s%s\n" "$INVIDIOUS" "$*"
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:43:38 +02:00
|
|
|
known_site()
|
|
|
|
{
|
|
|
|
printf "%s\n" "$1" | grep -Eq \
|
|
|
|
-e "^https://(www\.)?youtube\.com/" \
|
|
|
|
-e "^https://youtu\.be/" \
|
|
|
|
-e "^https://(www\.)?invidio\.us/" \
|
|
|
|
-e "^https://invidious\.snopyta\.org/" \
|
|
|
|
-e "^https://yewtu\.be/"
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:44:19 +02:00
|
|
|
get_id_path()
|
|
|
|
{
|
|
|
|
printf "%s\n" "${1##*/}"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_id_query()
|
|
|
|
{
|
|
|
|
oldIFS=$IFS
|
|
|
|
IFS="&"
|
|
|
|
set -- ${1##*"?"}
|
|
|
|
IFS=$oldIFS
|
|
|
|
|
|
|
|
id=
|
|
|
|
for param; do
|
|
|
|
case $param in
|
|
|
|
v=*) id=${param#v=}
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
printf "%s\n" "$id"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_id()
|
|
|
|
{
|
|
|
|
case $1 in
|
|
|
|
"https://youtu.be/" | *"/embed/"*)
|
|
|
|
get_id_path "$1"
|
|
|
|
;;
|
|
|
|
*) get_id_query "$1"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2020-05-02 21:39:59 +02:00
|
|
|
: ${INVIDIOUS:=invidious.snopyta.org}
|
2019-09-11 04:11:40 +02:00
|
|
|
|
2020-05-02 21:39:59 +02:00
|
|
|
torsocks=torsocks
|
|
|
|
while getopts T flag; do
|
|
|
|
case $flag in
|
|
|
|
T) torsocks=
|
|
|
|
;;
|
|
|
|
*) usage
|
|
|
|
;;
|
|
|
|
esac
|
2019-09-11 04:11:40 +02:00
|
|
|
done
|
2020-05-02 21:39:59 +02:00
|
|
|
shift $((OPTIND - 1))
|
|
|
|
[ $# -eq 1 ] || usage
|
|
|
|
|
|
|
|
url=$1
|
2020-05-02 22:43:38 +02:00
|
|
|
known_site "$url" || err "Unknown site: $url"
|
2019-09-11 04:11:40 +02:00
|
|
|
|
2020-05-02 22:44:19 +02:00
|
|
|
id=$(get_id "$url")
|
|
|
|
[ ${#id} -eq 11 ] || err "invalid video ID"
|
2019-09-11 04:11:40 +02:00
|
|
|
|
2020-05-02 21:39:59 +02:00
|
|
|
# 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"
|