fix ani-cli script for streaming and downloading

implemented fixes from @RaynardGerraldo on the main script
https://github.com/pystardust/ani-cli
This commit is contained in:
ksyasuda 2021-12-30 11:08:50 -08:00
parent 89fb51c5bf
commit 9dcb67f172

137
ani-cli
View File

@ -7,7 +7,8 @@ fi
VERBOSE=0 VERBOSE=0
BASE_URL="https://www2.gogoanime.cm/" # BASE_URL="https://www2.gogoanime.cm/"
BASE_URL=$(curl -s -L -o /dev/null -w "%{url_effective}\n" https://gogoanime.cm)
CFG_DIR="$XDG_CONFIG_HOME/aniwrapper" CFG_DIR="$XDG_CONFIG_HOME/aniwrapper"
ROFI_CFG="meh.rasi" ROFI_CFG="meh.rasi"
@ -55,11 +56,15 @@ log() {
search_anime() { search_anime() {
# get anime name along with its id # get anime name along with its id
search=$(printf '%s' "$1" | tr ' ' '-') search=${1// /-}
log "Search Query: $search"
titlepattern='<a href="/category/'
curl -s "$BASE_URL//search.html" \ curl -s "$BASE_URL//search.html" \
-G \ -G \
-d "keyword=$search" | -d "keyword=$search" |
sed -n -E 's_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p' sed -n -E '
s_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p
'
} }
search_eps() { search_eps() {
@ -75,17 +80,14 @@ search_eps() {
' '
} }
# get the download page url - no longer used (use get_embedded_video_link) check_input() {
get_dpage_link() { [ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
anime_id=$1 episodes=$ep_choice_start
ep_no=$2 if [ -n "$ep_choice_end" ]; then
[ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null || die "Invalid number entered"
curl -s "$BASE_URL/$anime_id-episode-$ep_no" | # create list of episodes to download/watch
sed -n -E ' episodes=$(seq $ep_choice_start $ep_choice_end)
/^[[:space:]]*<li class="dowloads">/{ fi
s/.*href="([^"]*)".*/\1/p
q
}'
} }
get_embedded_video_link() { get_embedded_video_link() {
@ -103,50 +105,32 @@ get_embedded_video_link() {
}' }'
} }
get_video_quality() { get_links() {
embedded_video_url=$1 embedded_video_url=$(get_embedded_video_link "$anime_id" "$episode")
video_url=$2 episode_id=$(echo "$embedded_video_url" | grep -oE "id.+?&")
video_url="https://gogoplay1.com/download?${episode_id}"
video_file=$(curl -s --referer "$embedded_video_url" "$video_url")
available_qualities=$(printf '%s' "$video_file" | sed -n -E 's/.*NAME="([^p]*)p"/\1/p')
case $quality in
best)
printf '%s' "$available_qualities" | tail -n 1
;;
worst)
printf '%s' "$available_qualities" | head -n 1
;;
*)
is_quality_avail=$(printf '%s' "$available_qualities" | grep "$quality")
video_quality="$quality"
if [ -z "$is_quality_avail" ]; then
printf "$c_red%s$c_reset\n" "Current video quality is not available (defaulting to highest quality)" >&2
quality=best
video_quality=$(printf '%s' "$available_qualities" | tail -n 1)
fi
printf '%s' "$video_quality"
;;
esac
} }
get_links() { get_video_quality() {
embedded_video_url="$1" get_links
video_url=$(curl -s "$embedded_video_url" | video_quality=$(curl -s get_links "$video_url" | grep -oE "(http|https):\/\/.*com\/cdn.*expiry=[0-9]*"| sort -V | sed 's/amp;//')
sed -n -E ' case $quality in
/^[[:space:]]*sources:/{ best)
s/.*(https[^'\'']*).*/\1/p play_link=$(echo "$video_quality" | sort -V | tail -n 1)
q ;;
} worst)
') play_link=$(echo "$video_quality" | sort -V | head -n 1)
;;
tmp_url=$(printf '%s' "$video_url" | sed -n -E 's/(.*)\.([0-9]+\.[0-9]+)\.[0-9]+\.m3u8/\1.\2.m3u8/p') *)
[ -z "$tmp_url" ] || video_url="$tmp_url" play_link=$(echo "$video_quality" | grep -oE "(http|https):\/\/.*com\/cdn.*"${quality}".*expiry=[0-9]*")
video_quality=$(get_video_quality "$embedded_video_url" "$video_url") if [ -z "$play_link" ]; then
# Replace the video with highest quality video printf "$c_red%s$c_reset\n" "Current video quality is not available (defaulting to highest quality)" >&2
printf '%s' "$video_url" | sed -n -E "s/(.*)\.m3u8/\1.$video_quality.m3u8/p" quality=best
play_link=$(echo "$video_quality" | sort -V | tail -n 1)
fi
printf '%s' "$play_link"
;;
esac
} }
dep_ch() { dep_ch() {
@ -491,31 +475,19 @@ open_episode() {
insert_history "$anime_id" "$episode" insert_history "$anime_id" "$episode"
# dpage_url=$(get_dpage_link "$anime_id" "$episode") # embedded_video_url=$(get_embedded_video_link "$anime_id" "$episode")
# video_url=$(get_links "$dpage_url") # video_url=$(get_links "$embedded_video_url")
embedded_video_url=$(get_embedded_video_link "$anime_id" "$episode") get_video_quality
video_url=$(get_links "$embedded_video_url") status_code=$(curl -s -I get_video_quality "$play_link" | head -n 1|cut -d ' ' -f2)
log "Status code: $status_code"
case $video_url in
*streamtape*)
# If direct download not available then scrape streamtape.com
BROWSER=${BROWSER:-firefox}
printf "scraping streamtape.com\n"
video_url=$(curl -s "$video_url" | sed -n -E '
/^<script>document/{
s/^[^"]*"([^"]*)" \+ '\''([^'\'']*).*/https:\1\2\&dl=1/p
q
}
')
;;
esac
if [ "$is_download" -eq 0 ]; then if [ "$is_download" -eq 0 ]; then
log "embedded video url: $embedded_video_url" if echo "$status_code" | grep -vE "^2.*"; then
log "video url: $video_url" printf "${c_red}\nCannot reach servers!"
# setsid -f $player_fn --http-header-fields="Referer: $dpage_url" "$video_url" >/dev/null 2>&1 else
# nohup $player_fn --http-header-fields="Referer: $dpage_url" "$video_url" >/dev/null 2>&1 & setsid -f $player_fn get_video_quality "$play_link" > /dev/null 2>&1
setsid -f $player_fn --http-header-fields="Referer: $embedded_video_url" "$video_url" >/dev/null 2>&1 printf "${c_green}\nVideo playing"
fi
else else
log "Downloading episode $episode ..." log "Downloading episode $episode ..."
log "$video_url" log "$video_url"
@ -525,10 +497,11 @@ open_episode() {
cd "${ddir/ //}" || die "Could not enter directory $ddir" cd "${ddir/ //}" || die "Could not enter directory $ddir"
mkdir -p "$anime_id" || die "Could not create directory" mkdir -p "$anime_id" || die "Could not create directory"
cd "$anime_id" || die "Could not enter subdirectory $ddir/$anime_id" cd "$anime_id" || die "Could not enter subdirectory $ddir/$anime_id"
ffmpeg -headers "Referer: $embedded_video_url" -i "$video_url" \ ffmpeg -i "$play_link" -c copy "${anime_id}-${episode}.mkv" >/dev/null 2>&1 &&
-codec copy "${anime_id}-${episode}.mkv" >/dev/null 2>&1 &&
notify-send "Downloaded episode: $episode" || notify-send "Downloaded episode: $episode" ||
notify-send "Download failed episode: $episode" notify-send "Download failed episode: $episode, please retry or check your internet connection"
# printf "${c_green}Downloaded episode: %s${c_reset}\n" "$episode" ||
# printf "${c_red}Download failed episode: %s , please retry or check your internet connection${c_reset}\n" "$episode"
} }
fi fi
} }