yuja-dl/yuja-dl
2024-01-29 14:28:23 -06:00

171 lines
3.6 KiB
Bash
Executable File

#!/bin/sh
# --------------------------------------
# name: yuja-dl
# main: Jaidyn Ann <jadedctrl@posteo.at>
# lisc: CC0
# date: 2021
# --------------------------------------
download_url() {
local url="$1"
local sub="$(echo "$url" | subdomain)"
local id="$(get_url_id "$url" "$sub")"
local captions_key="$(get_metadata "$sub" "$id" | caption_file_key)"
local node_pid="$(get_metadata "$sub" "$id" | video_node_pid)"
local flink="$(get_node_metadata "$sub" "$id" "$node_pid" | video_filelink)"
local m3u8="$(get_video_source "$sub" "$id" "$flink" | video_source_m3u8_link)"
if test -z "$OUTPUT"; then
OUTPUT="$(get_metadata "$sub" "$id" | default_video_title)"
fi
curl -b "$COOKIES" -o "${OUTPUT}.srt" "$(caption_url "$sub" "$captions_key")"
ffmpeg -i "$m3u8" -c copy "${OUTPUT}.mkv"
}
#---------------------------------------
get_url_id() {
local url="$1"
local sub="$2"
if echo "$url" | grep "v=" > /dev/null; then
echo "$url" \
| video_vid
elif echo "$url" | grep "u=" > /dev/null; then
video_metadata "$url" "$sub" \
| video_pid
else
>&2 echo "The URL provided is invalid― try pasting it again."
>&2 echo "If you're sure you got it right, please report the URL"
>&2 echo "as a bug (https://github.com/JadedCtrl/yuja-dl/)."
exit 3
fi
}
get_metadata() {
local sub="$1"
local id="$2"
curl -b "$COOKIES" -s \
"https://${sub}.yuja.com/P/Data/GetVideoListNodeInfo?videoPID=${id}"
}
caption_file_key() {
jq -r .video \
| jq -r .captionFileKey.value
}
video_node_pid() {
jq -r .node \
| jq -r .videoListNodePID
}
video_pid() {
jq -r .videoPID
}
default_video_title() {
jq -r .video \
| jq -r .title
}
#---------------------------------------
get_node_metadata() {
local sub="$1"
local video_id="$2"
local video_node_pid="$3"
curl -b "$COOKIES" -s \
"https://${sub}.yuja.com/P/Data/VideoJSON" \
--data-raw "video=${video_id}&node=${video_node_pid}"
}
video_filelink() {
jq -r .video.videoLink
}
#---------------------------------------
get_video_source() {
local sub="$1"
local video_id="$2"
local video_filelink="$3"
curl -b "$COOKIES" -s \
"https://${sub}.yuja.com/P/Data/VideoSource?video=${video_filelink}&videoPID=${video_id}"
}
video_source_m3u8_link() {
jq -r .videoSources[]
}
#---------------------------------------
caption_url() {
local subdomain="$1"
local caption_key="$2"
echo \
"https://${subdomain}.yuja.com/P/DataPage/CaptionFile/${caption_key}"
}
#---------------------------------------
subdomain() {
awk -F '.' '{print $1}' \
| sed 's%.*//%%'
}
video_vid() {
sed 's%.*v=%%' \
| sed 's%&.*%%'
}
video_uid() {
sed 's%.*u=%%' \
| sed 's%&.*%%'
}
video_metadata() {
local url="$1"
local sub="$2"
local uid="$(echo "$url" | video_uid)"
curl -b "$COOKIES" -s \
"https://${sub}.yuja.com/V/VideoDecryptLogicServlet?u=${uid}"
}
# INVOCATION
# --------------------------------------
usage() {
>&2 echo "usage: yuja-dl URL [-o OUTPUT] [-c cookies.txt]"
>&2 echo
>&2 echo " URL is a *.yuja.com URL with a video ID argument"
>&2 echo " OUTPUT is the basename for the mp4 and srt files"
>&2 echo " cookies.txt is an optional cookie-jar, used for authentication"
>&2 echo " with private videos. Should be exported from your web-browser."
exit 2
}
if test "$#" -eq 0; then
usage
fi
OPTIND=2
while getopts ":hc:o:" arg; do
case "$arg" in
h)
usage ;;
c)
COOKIES="$OPTARG" ;;
o)
OUTPUT="$OPTARG" ;;
:)
echo "$0: Must supply an argument to -$OPTARG."
exit 1 ;;
?)
echo "Invalid option: -${OPTARG}"
usage ;;
esac
done
download_url "$1"