Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: m3u8 parsing for quality selection,auto-update base-url, optimizations #638

Merged
merged 21 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
</p>

<h3 align="center">
A cli to browse and watch anime. This tool scrapes the site <a href="https://gogoplay4.com">gogoplay.</a>

Latest stable version: 2.0.5
A cli to browse and watch anime. This tool scrapes the site <a href="https://gogoplay5.com">gogoplay.</a>
</h3>

<h1 align="center">
Expand Down Expand Up @@ -51,8 +49,10 @@ if you encounter "Video url not found" or any breaking issue, then make sure you
`sudo ani-cli -U` to update on linux, mac and android. On windows, run gitbash as administrator then there type `ani-cli -U`.
If after this the issue persists then open an issue.
<br>
If you see sed warnings or your history entries have disappeared after updating, then update your history file with the history
transition script (history_transition.sh). Download it: `git clone https://github.com/pystardust/ani-cli` then run: `./ani-cli/hist-transition.sh`
If you see sed warnings or your history entries have disappeared after updating, then update your history file with the history transition script.
```sh
curl -s "https://raw.githubusercontent.com/pystardust/ani-cli/master/hist_transition.sh" | sh
```
It doesn't work for all anime, but the ones it can't find will print out alongside their episode numbers. In the end clean up: `rm -rf ./ani-cli`

## Install
Expand Down
95 changes: 59 additions & 36 deletions ani-cli
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Project repository: https://github.com/pystardust/ani-cli

# Version number
VERSION="2.0.6"
VERSION="2.1.0"



Expand Down Expand Up @@ -99,7 +99,7 @@ case $2 in
*mp4*)
aria2c --summary-interval=0 -x 16 -s 16 --referer="$1" "$2" --dir="$download_dir" -o "${3}-${4}.mp4" --download-result=hide ;;
*)
ffmpeg -loglevel error -stats -referer "$1" -i "$2" -c copy "$download_dir/${3}-${4}.mp4" ;;
ffmpeg -loglevel error -stats -referer "$1" -i "$2" -map "0:p:$((idx-1))?" -c copy "$download_dir/${3}-${4}.mp4" ;;
esac
}

Expand All @@ -122,13 +122,11 @@ extended_search () {
sed -n -E 's_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p'
}

#
#check no of episodes in an anime
check_episode () {
data=$(curl -s "$base_url/videos/$1")
if [ "$data" != "404" ]; then
del=$(printf "%s" "$data" | grep -n "Latest Episodes" | cut -d ":" -f1)
printf "%s" "$data" | sed "$del,$ d" | sed -nE "s_^[[:space:]]*<a href.*videos/${2}(.*)\">_\1_p"
fi
del=$(printf "%s" "$data" | grep -n "Latest Episodes" | cut -d ":" -f1)
[ -n "$del" ] && printf "%s" "$data" | sed "$del,$ d" | sed -nE "s_^[[:space:]]*<a href.*videos/${2}(.*)\">_\1_p"
}

process_hist_entry () {
Expand Down Expand Up @@ -156,6 +154,13 @@ search_history () {
# URL PROCESSING #
##################

#update main url to latest one
updateurl () {
prev_url=$(printf "%s" "$base_url" | cut -d"/" -f3)
new_url=$(printf "%s" "$dpage_link" | cut -d"/" -f3)
[ "$prev_url" != "$new_url" ] && printf "%s" "$new_url" > "$urlfile"
}

# get the download page url
get_dpage_link() {
anime_id="$1"
Expand All @@ -179,26 +184,50 @@ decrypt_link() {
}

# chooses the link for the set quality
get_video_quality() {
get_video_link() {
dpage_url="$1"
video_links=$(decrypt_link "$dpage_url")
if printf '%s' "$video_links" | grep -q "mp4"; then
video_url=$(get_video_quality_mp4 "$video_links")
idx=1
else
video_url="$video_links"
get_video_quality_m3u8
fi
}

get_video_quality_mp4() {
case $quality in
best)
video_link=$(printf '%s' "$video_links" | head -n 4 | tail -n 1)
;;
video_url=$(printf '%s' "$1" | head -n 4 | tail -n 1) ;;
worst)
video_link=$(printf '%s' "$video_links" | head -n 1)
;;
video_url=$(printf '%s' "$1" | head -n 1) ;;
*)
video_link=$(printf '%s' "$video_links" | grep -i "${quality}p" | head -n 1)
if [ -z "$video_link" ]; then
video_url=$(printf '%s' "$1" | grep -i "${quality}p" | head -n 1)
if [ -z "$video_url" ]; then
err "Current video quality is not available (defaulting to best quality)"
quality=best
video_link=$(printf '%s' "$video_links" | head -n 4 | tail -n 1)
video_url=$(printf '%s' "$1" | head -n 4 | tail -n 1)
fi
;;
esac
printf '%s' "$video_link"
printf '%s' "$video_url"
}

get_video_quality_m3u8() {
case $quality in
worst|360)
idx=2 ;;
480)
idx=3 ;;
720)
idx=4 ;;
1080|best)
idx=5 ;;
*)
Derisis13 marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +217 to +227
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know there was an earlier commit by @CoolnsX that aimed to deduplicate, but it still seems like this is redundant, am I missing sth?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is just because we have people put in worst, 360, 480, 720, 1080, best as the optarg to -q.
Doesn't seem like there is an easy way to deduplicate it further, but maybe someone has an idea?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems unlikely

idx=5 ;;
esac
printf '%s' "$video_url" | grep -qE "gogocdn.*m3u.*" && idx=$((idx-1))
CoolnsX marked this conversation as resolved.
Show resolved Hide resolved
}


Expand Down Expand Up @@ -389,7 +418,7 @@ open_episode () {
if [ -z "$dpage_link" ];then
err "Episode doesn't exist!!"
else
video_url=$(get_video_quality "$dpage_link")
get_video_link "$dpage_link"
fi
if [ "$is_download" -eq 0 ]; then
# write anime and episode number and save to temporary history
Expand All @@ -406,7 +435,7 @@ open_episode () {
inf "Downloading episode $episode ..."
episode=$(printf "%03d" "$episode")
{
if download "$dpage_link" "$video_url" "$anime_id" "$episode" ; then
if download "$dpage_link" "$video_url" "$anime_id" "$episode"; then
inf "Downloaded episode: $episode"
else
err "Download failed episode: $episode , please retry or check your internet connection"
Expand All @@ -425,7 +454,7 @@ play_episode () {
;;
*)
trackma_title="$(printf '%s' "$anime_id" | tr '-' ' ' | awk '{for(i=1;i<=NF;i++){ $i=toupper(substr($i,1,1)) substr($i,2) }}1')"
set -- "$@" --referrer="$dpage_link" --force-media-title="$trackma_title $episode"
set -- "$@" --vid="$idx" --referrer="$dpage_link" --force-media-title="$trackma_title $episode"
;;
esac
# Run Command
Expand All @@ -439,6 +468,7 @@ play_episode () {
PID=$!
}


############
# START UP #
############
Expand All @@ -457,6 +487,7 @@ choice=
auto_play=0
# history file path
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts"
urlfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-url"
port19x marked this conversation as resolved.
Show resolved Hide resolved
logdir="${XDG_CACHE_HOME:-$HOME/.cache}"

# create history file and history dir if none found
Expand All @@ -466,11 +497,9 @@ logdir="${XDG_CACHE_HOME:-$HOME/.cache}"
while getopts 'vq:dp:chDUVa:' OPT; do
case $OPT in
d)
is_download=1
;;
is_download=1 ;;
a)
ep_choice_to_start=$OPTARG
;;
ep_choice_to_start=$OPTARG ;;
D)
: > "$logfile"
exit 0
Expand All @@ -480,14 +509,11 @@ while getopts 'vq:dp:chDUVa:' OPT; do
download_dir=$OPTARG
;;
q)
quality=$OPTARG
;;
quality=$OPTARG ;;
c)
scrape=history
;;
scrape=history ;;
v)
player_fn="vlc"
;;
player_fn="vlc" ;;
U)
update_script
exit 0
Expand Down Expand Up @@ -529,9 +555,6 @@ case $scrape in
history)
search_history
[ "$REPLY" = "q" ] && exit 0
first_ep_number=0
result=$(get_dpage_link "$selection_id" "$first_ep_number")
[ -z "$result" ] && first_ep_number=1
;;
*)
die "Unexpected scrape type"
Expand All @@ -540,6 +563,8 @@ esac
generate_ep_list
append_history
open_selection
updateurl &


########
# LOOP #
Expand Down Expand Up @@ -574,11 +599,9 @@ if [ -z "$select_first" ]; then
unset ep_choice_end
;;
s)
episode_selection
;;
episode_selection ;;
q)
break
;;
break ;;
*)
tput clear
err "Invalid choice"
Expand Down