Files
repos_scripts/fap-cli
2024-06-19 10:37:53 +05:30

191 lines
4.8 KiB
Bash
Executable File

#!/bin/sh
help_text() {
while IFS= read -r line; do
printf "%s\n" "$line"
done <<-EOF
Usage:
${0##*/} [-d | -p <download_dir>] [<query>]
${0##*/} [-v] [<query>]
${0##*/} -h
Options:
-h show helptext
-d download video
-p download video to specified directory
-v use VLC as the media player
EOF
}
info() {
#shellcheck disable=SC2059
printf "\033[2K\r\033[1;${2:-32}m${1}\033[0m"
}
ask() {
info "$1" "33"
}
err() {
info "$1\n" "31"
}
input() {
ask " Episode??(1-$1)> "
read -r x
while [ -z "$x" ] || ! [ "$x" -eq "$x" ] 2>/dev/null || [ "$x" -lt "1" ] 2>/dev/null || [ "$x" -gt "$1" ] 2>/dev/null; do
err "Invalid choice"
ask " Episode??(1-$1)> "
read -r x
done
ep_no=$x
unset x
}
stream() {
case $player_fn in
vlc) setsid -f "$player_fn" --http-referrer="$base_url" "$1" >/dev/null 2>&1 ;;
mpv) setsid -f "$player_fn" "$1" --referrer="$base_url" --force-media-title="$2" >/dev/null 2>&1 ;;
mpv_android) am start --user 0 -a android.intent.action.VIEW -d "$1" -n is.xyz.mpv/.MPVActivity >/dev/null 2>&1 ;;
vlc_android) am start --user 0 -a android.intent.action.VIEW -d "$1" -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -e "title" "$2" >/dev/null 2>&1 ;;
esac
}
download() {
info "Downloading $1" "34"
case $1 in
*m3u8) $terminal hls -n 300 -ro "$download_dir/$2" "$1" ;;
*) $terminal aria2c --summary-interval=0 -x 16 -s 16 --referer="$base_url" "$1" -d "$download_dir" -o "$2.mp4" --download-result=hide ;;
esac
}
get_show() {
info "Searching query.." "34"
results=$(search | fzf --bind="change:reload:$run_file {q} 1" --prompt="Search: " )
[ -z "$results" ] && err "No search results found" && exit 0
info ""
result=$(printf "%s" "$results" | fzf --layout="reverse" --border --height=10 -1)
[ -z "$result" ] && err "No hentai selected" && exit 0
info "selected $result\n" "35"
info "Fetching Episodes List.." "34"
ep_list=$(curl -sA "$agent" "$base_url/tvshows/$result/" | sed -nE 's_^[[:space:]]*<a href="https://hentaimama.io/(.*)/">.$_\1_p' | tac)
noofeps=$(printf "%s\n" "$ep_list" | wc -l)
ep_no=1
[ "$noofeps" -gt 1 ] && input "$noofeps"
get_ep_link
}
get_ep_link() {
tput clear
info "Loading Episode $ep_no" "34"
ep_id=$(printf "%s" "$ep_list" | sed -n "${ep_no}p")
id=$(curl -sA "$agent" "$base_url/$ep_id/" | sed -nE "s/.*?p=(.*)'.*/\1/p")
display=$(printf "%s" "$ep_id" | cut -d'/' -f2- | tr "-" " ")
[ -z "$id" ] && err "Episode doesn't exist on this site" && return 1
play_link
}
play_link() {
info "Fetching Video Link" "34"
data="$(curl -sA "$agent" "$(curl -sA "$agent" "$base_url/wp-admin/admin-ajax.php" -d "action=get_player_contents&a=$id" -H "X-Requested-With:XMLHttpRequest" | sed 's/\\//g' | sed -nE 's/.*src="(.*)" width.*,.*/\1/p')")"
video_link="$(printf "%s" "$data" | sed -nE 's/[[:space:]]*<source src="(.*)" typ.*/\1/p')"
# trying again
[ -z "$video_link" ] && video_link="$(printf "%s" "$data" | sed -nE 's/[[:space:]]*file: "(.*)".$/\1/p')"
[ -z "$video_link" ] && err "Video Url not found" && return 1
info "\n$video_link\n"
if [ "$is_download" -eq "0" ]; then
stream "$video_link" "$display"
else
download "$video_link" "$display"
fi
[ "$noofeps" -eq 1 ] && exit 0
}
trap "exit 0" INT HUP
base_url="https://hentaimama.io"
player_fn="mpv"
is_download=0
download_dir='.'
run_file="${TMPDIR:-/tmp}/${0##*/}_run"
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/12$(head /dev/urandom | tr -dc '3-6' | cut -c1).0.0.0 Safari/537.36"
case $(uname -o) in
*ndroid*)
download_dir="/sdcard"
player_fn="mpv_android"
alt_fn="vlc_android"
;;
*)
download_dir="$HOME/Downloads"
player_fn="mpv"
alt_fn="vlc"
[ -t 1 ] || terminal="${TERMINAL:-foot} -e"
;;
esac
\cat << EOF > "$run_file"
search() { curl -s -A "$agent" "$base_url/?s=\$1" | sed -nE 's_^[[:space:]]*<a href="https://hentaimama.io/tvshows/(.*)/">.\$_\1_p'; }; [ -n "\$2" ] && search "\$1"
EOF
chmod +x "$run_file"
# shellcheck disable=SC1091,SC1090
. "$run_file"
while getopts 'dhp:v' OPT; do
case $OPT in
d)
is_download=1
;;
p)
is_download=1
download_dir=$OPTARG
;;
v)
player_fn=$alt_fn
;;
*)
help_text
exit 0
;;
esac
done
shift $((OPTIND - 1))
get_show
while :; do
info "\nCurrently playing $display/$noofeps\n"
[ "$ep_no" != "$noofeps" ] && info "(n) next\n" "33"
[ "$ep_no" != "1" ] && info "(p) previous\n" "36"
info "(e) select episode\n" "34"
info "(q) exit\n" "31"
ask "> "
read -r choice
case $choice in
n)
[ "$((ep_no + 1))" -gt "$noofeps" ] && err "Episode out of range" && continue
: $((ep_no += 1))
;;
p)
[ "$((ep_no - 1))" -lt "1" ] && err "Episode out of range" && continue
: $((ep_no -= 1))
;;
e)
input "$noofeps"
;;
q)
break
;;
*)
err "invalid choice"
;;
esac
get_ep_link
done