Skip to content

Commit

Permalink
feat: m3u8 quality parsing (#638)
Browse files Browse the repository at this point in the history
* feat: quality selection with m3u8 links(currently best and worst)

* Let wildcard do its thing

* feat:m3u8 parsing, auto-update base-url, optimizations

* pass shellcheck

* QA: change if to one-liners

* docs: matrix server

* fix: typos

* fix: new keys, deduplicacy

* docs: update readme

* chore: version bump 2.0.4 -> 2.0.5

* Needs more discussion

* formatting: removed whitespace

* formatting: removed whitespace

* formatting: line optimizations

* formatting: empty lines

* chore: version bump

* remove version indicator

Co-authored-by: RaynardGerraldo <[email protected]>
Co-authored-by: CoolnsX <[email protected]>
Co-authored-by: zenith71 <[email protected]>
Co-authored-by: ivan <[email protected]>
Co-authored-by: Derisis13 <[email protected]>
Co-authored-by: port19 <[email protected]>
  • Loading branch information
7 people authored Apr 17, 2022
1 parent 506ad35 commit 6a61169
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 41 deletions.
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 ;;
*)
idx=5 ;;
esac
printf '%s' "$video_url" | grep -qE "gogocdn.*m3u.*" && idx=$((idx-1))
}


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"
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

0 comments on commit 6a61169

Please sign in to comment.