mirror of
https://github.com/CoolnsX/dra-cla.git
synced 2025-12-20 07:15:25 +05:30
Refactor and bump version to 3.0.0
This commit is contained in:
11
README.md
11
README.md
@@ -1,5 +1,5 @@
|
||||
|
||||
<h1> Refactoring the script and using fzf for UI. WIP </h1>
|
||||
### This script is refactored and now using fzf (make sure it is installed as script now checks for it) for showing menus, if you don't like fzf then you can continue to use old one. I would suggest to try fzf based one.
|
||||
|
||||
<p align=center>
|
||||
<br>
|
||||
@@ -50,18 +50,14 @@ Install termux [(Guide)](https://termux.com/)
|
||||
|
||||
```sh
|
||||
pkg update
|
||||
pkg install git termux-tools ncurses-utils openssl-tool ffmpeg -y
|
||||
pkg install git termux-tools ncurses-utils openssl-tool ffmpeg fzf aria2 -y
|
||||
git clone https://github.com/CoolnsX/dra-cla && cd dra-cla
|
||||
cp dra-cla $PREFIX/bin/dra-cla
|
||||
echo 'am start -n is.xyz.mpv/.MPVActivity "$1"' > $PREFIX/bin/mpv
|
||||
chmod +x $PREFIX/bin/mpv
|
||||
```
|
||||
|
||||
Install mpv-android [(Link)](https://play.google.com/store/apps/details?id=is.xyz.mpv)
|
||||
|
||||
*Add ```referrer="https://asianembed.io"``` to mpv.conf (Open mpv app, goto three dots top right->Settings->Advanced-->Edit mpv.conf)*
|
||||
|
||||
*Note: VLC android doesn't support referrer option. So it will not work*
|
||||
For VLC: pass ``` dra-cla -v ```
|
||||
|
||||
## Uninstall
|
||||
|
||||
@@ -70,6 +66,7 @@ Install mpv-android [(Link)](https://play.google.com/store/apps/details?id=is.xy
|
||||
|
||||
## Dependencies
|
||||
|
||||
- fzf (new)
|
||||
- grep
|
||||
- sed
|
||||
- curl
|
||||
|
||||
650
dra-cla
650
dra-cla
@@ -1,152 +1,157 @@
|
||||
#!/bin/sh
|
||||
|
||||
VERSION="2.2.2"
|
||||
#shellcheck disable=SC2154
|
||||
|
||||
#######################
|
||||
# AUXILIARY FUNCTIONS #
|
||||
#######################
|
||||
# most important variables.
|
||||
VERSION="3.0.0"
|
||||
CHANNEL="main"
|
||||
base_url="https://pladrac.net"
|
||||
logdir="${XDG_CACHE_HOME:-$HOME/.cache}"
|
||||
logfile="$logdir/dra-hsts"
|
||||
quality=best
|
||||
scrape=query
|
||||
agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/12$(head -5 /dev/urandom | tr -dc '0-2' | cut -c1).0.0.0 Safari/537.36"
|
||||
trap 'rm -f '"$logfile"'.new' INT HUP
|
||||
|
||||
help_text () {
|
||||
############
|
||||
# help #
|
||||
############
|
||||
|
||||
help_text() {
|
||||
while IFS= read -r line; do
|
||||
printf "%s\n" "$line"
|
||||
done <<-EOF
|
||||
|
||||
Usage:
|
||||
${0##*/} [-v] [-q <quality>] [-a <episode>] [-d | -p <download_dir>] [<query>]
|
||||
${0##*/} [-v] [-q <quality>] -c
|
||||
${0##*/} -h | -D | -U | -V
|
||||
Usage:
|
||||
${0##*/} [-v] [-q <quality>] [-a <episode>] [-d | -p <download_dir>] [<query>]
|
||||
${0##*/} [-v] [-q <quality>] -c
|
||||
${0##*/} -h | -D | -U | -V
|
||||
|
||||
Options:
|
||||
-c continue watching drama from history
|
||||
-a specify episode to watch
|
||||
-h show helptext
|
||||
-d download episode
|
||||
-p download episode to specified directory
|
||||
-q set video quality (best|worst|360|480|720|1080)
|
||||
-v use VLC as the media player
|
||||
-D delete history
|
||||
-U fetch update from github
|
||||
-V print version number and exit
|
||||
Options:
|
||||
-c continue watching drama from history
|
||||
-a specify episode to watch
|
||||
-h show helptext
|
||||
-d download episode
|
||||
-p download episode to specified directory
|
||||
-q set video quality (best|worst|360|480|720|1080)
|
||||
-v use VLC as the media player
|
||||
-D delete Entire history
|
||||
-U fetch update from github
|
||||
-V print version number and exit
|
||||
|
||||
Episode selection:
|
||||
Multiple episodes can be chosen given a range
|
||||
Choose episode [1-13]: 1 6
|
||||
This would choose episodes 1 2 3 4 5 6
|
||||
To select the last episode use -1
|
||||
Episode selection:
|
||||
Multiple episodes can be chosen given a range
|
||||
Choose episode [1-13]: 1 6
|
||||
This would choose episodes 1 2 3 4 5 6
|
||||
|
||||
When selecting non-interactively, the first result will be
|
||||
selected, if drama is passed
|
||||
When selecting non-interactively (eg: -a <ep_no>), the first result will be
|
||||
selected, if drama is passed as Args
|
||||
EOF
|
||||
}
|
||||
|
||||
version_text () {
|
||||
inf "Version: $VERSION" >&2
|
||||
}
|
||||
############
|
||||
# Update #
|
||||
############
|
||||
|
||||
die () {
|
||||
err "$*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# get the newest version of this script from github and replace it
|
||||
update_script () {
|
||||
update="$(curl -s "https://raw.githubusercontent.com/CoolnsX/dra-cla/main/dra-cla" | diff -u "$0" -)"
|
||||
update_script() {
|
||||
log "Checking For Updates.." >&2
|
||||
update="$(curl -A "$agent" -s "https://raw.githubusercontent.com/CoolnsX/dra-cla/$CHANNEL/dra-cla" | diff -u "$0" -)"
|
||||
if [ -z "$update" ]; then
|
||||
inf "Script is up to date :)"
|
||||
log "Script is up to date :)\n" "32" >&2
|
||||
exit 0
|
||||
fi
|
||||
#dry run
|
||||
if [ "$1" -eq 0 ]; then
|
||||
log "Update Available!!\n" "32" >&2
|
||||
log "To Update, type: ${sudo}${0##*/} -U" "33" >&2
|
||||
exit 0
|
||||
fi
|
||||
#warning: this updates the codebase of script.
|
||||
if printf '%s\n' "$update" | patch "$0" -; then
|
||||
log "Script has been updated\n" "32" >&2
|
||||
exit 0
|
||||
else
|
||||
if printf '%s\n' "$update" | patch "$0" - ; then
|
||||
inf "Script has been updated"
|
||||
else
|
||||
die "Can't update for some reason!"
|
||||
fi
|
||||
log "Can't update for some reason!\n" "31" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# checks if dependencies are present
|
||||
dep_ch () {
|
||||
for dep; do
|
||||
if ! command -v "$dep" >/dev/null ; then
|
||||
err "Program \"$dep\" not found. Please install it."
|
||||
#aria2c is in the package aria2
|
||||
[ "$dep" = "aria2c" ] && err "To install aria2c, Type <your_package_manager> aria2"
|
||||
die
|
||||
fi
|
||||
############
|
||||
# UI #
|
||||
############
|
||||
|
||||
log() {
|
||||
#shellcheck disable=SC2059
|
||||
printf "\033[2K\r\033[1;${2:-36}m${1}\033[0m"
|
||||
}
|
||||
|
||||
menu() {
|
||||
fzf --prompt="$1 >> " --layout=reverse --border --height=20 -0 -1 -m
|
||||
}
|
||||
|
||||
ask() {
|
||||
log "$1" "33"
|
||||
}
|
||||
|
||||
#############
|
||||
# Logic #
|
||||
#############
|
||||
|
||||
dep_ch() {
|
||||
for dep in $(printf "%s" "$1" | tr ' ' '\n'); do
|
||||
command -v "$dep" >/dev/null || {
|
||||
log "Program $dep not found. Please install it\n" "31"
|
||||
[ "$dep" = "aria2c" ] && log "To install $dep, Type <your_package_manager> aria2" "32"
|
||||
exit 1
|
||||
}
|
||||
done
|
||||
}
|
||||
|
||||
download () {
|
||||
case $2 in
|
||||
*m3u8*)
|
||||
ffmpeg -loglevel error -stats -referer "$1" -i "$2" -c copy "$download_dir/${3}${4}.mp4" ;;
|
||||
*)
|
||||
aria2c --summary-interval=0 -x 16 -s 16 --referer="$1" "$2" --dir="$download_dir" -o "${3}${4}.mp4" --download-result=hide ;;
|
||||
esac
|
||||
download() {
|
||||
case "$1" in
|
||||
*m3u8*) ;;
|
||||
*) aria2c --enable-rpc=false --check-certificate=false --continue --summary-interval=0 -x16 -s16 "$1" --dir="$download_dir" -o "$2.mp4" --download-result=hide ;;
|
||||
esac
|
||||
}
|
||||
|
||||
#############
|
||||
# SEARCHING #
|
||||
#############
|
||||
|
||||
# gets drama names along with its id for search term
|
||||
search_drama () {
|
||||
search=$(printf '%s' "$1" | tr ' ' '-' )
|
||||
curl -s "$base_url/search.html?keyword=$search" |
|
||||
sed -nE 's_^[[:space:]]*<a href="/videos/([^"]*)">_\1_p'
|
||||
}
|
||||
|
||||
check_episode () {
|
||||
curl -s "$base_url/videos/$1" | sed '/Latest Episodes/,$d' | sed -nE "s_^[[:space:]]*<a href.*videos/${2}(.*)\">_\1_p"
|
||||
}
|
||||
|
||||
process_hist_entry () {
|
||||
temp_drama_id=$(printf "%s" "$drama_id" | sed 's/[0-9]*.$//')
|
||||
latest_ep=$(printf "%s" "$drama_id" | sed "s/$temp_drama_id//g")
|
||||
current_ep=$(check_episode "$drama_id" "$temp_drama_id" | head -n 1)
|
||||
if [ -n "$current_ep" ] && [ "$current_ep" -ge "$latest_ep" ]; then
|
||||
printf "%s\n" "$drama_id"
|
||||
fi
|
||||
}
|
||||
|
||||
# compares history with asianembed, only shows unfinished drama
|
||||
search_history () {
|
||||
open_episode() {
|
||||
tput clear
|
||||
[ ! -s "$logfile" ] && die "History is empty"
|
||||
search_results=$(while read -r drama_id; do process_hist_entry & done < "$logfile"; wait)
|
||||
[ -z "$search_results" ] && die "No unwatched episodes"
|
||||
one_hist=$(printf '%s\n' "$search_results" | grep -e "$" -c)
|
||||
[ "$one_hist" = 1 ] && select_first=1
|
||||
drama_selection "$search_results"
|
||||
ep_choice_start=$(sed -n -E "s/${selection_id}(.*)/\1/p" "$logfile")
|
||||
}
|
||||
log "Fetching $title Episode $episode.."
|
||||
[ -z "$dpage_link" ] && dpage_link=$(curl -s -A "$agent" "$base_url/videos/${id}${episode}" | sed -nE 's_^[[:space:]]*<iframe src="([^"]*)".*_https:\1_p')
|
||||
get_video_url
|
||||
if grep -q "$id" "$logfile"; then
|
||||
sed -E "s/^${id}.*/${id}$((episode + 1))/" "$logfile" >"${logfile}.new"
|
||||
else
|
||||
printf "%s%s\n" "$id" "$((episode + 1))" >>"$logfile"
|
||||
fi
|
||||
[ -z "$video_link" ] && log "Video URL not found" "31"
|
||||
[ "$player_function" = "download" ] && display="Downloading $title Episode $episode/$total_eps on $player_function\n" || display="Streaming $title Episode $episode/$total_eps\n"
|
||||
log "$display" "32"
|
||||
[ -z "$nohup" ] && ! printf "%s" "$player_function" | grep -q "android" && log "To play next Episode, quit the player."
|
||||
|
||||
##################
|
||||
# URL PROCESSING #
|
||||
##################
|
||||
#media Player
|
||||
case "$player_function" in
|
||||
mpv*) $nohup $player_function --force-media-title="${title} Episode ${episode}" "$video_link" >/dev/null 2>&1 ;;
|
||||
android_mpv) am start --user 0 -a android.intent.action.VIEW -d "$video_link" -n is.xyz.mpv/.MPVActivity >/dev/null 2>&1 ;;
|
||||
vlc*) $nohup $player_function --play-and-exit --meta-title="${title} Episode ${episode}" "$video_link" >/dev/null 2>&1 ;;
|
||||
android_vlc) am start --user 0 -a android.intent.action.VIEW -d "$video_link" -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -e "title" "${title} Episode ${episode}" >/dev/null 2>&1 ;;
|
||||
download) "$player_function" "$video_link" "${title} Episode ${episode}" ;;
|
||||
esac
|
||||
|
||||
# get the download page url
|
||||
get_dpage_link() {
|
||||
drama_id="$1"
|
||||
ep_no="$2"
|
||||
|
||||
curl -s "$base_url/videos/${drama_id}${ep_no}" | sed -nE 's_^[[:space:]]*<iframe src="([^"]*)".*_\1_p' |
|
||||
sed 's/^/https:/g'
|
||||
dpage_link=""
|
||||
[ -f "$logfile.new" ] && mv "${logfile}.new" "$logfile"
|
||||
}
|
||||
|
||||
decrypt_link() {
|
||||
fembed_id=$(curl -A "uwu" -s "$1" | sed -nE 's_.*fembed.*/v/([^#]*).*_\1_p')
|
||||
[ -z "$fembed_id" ] || video_links=$(curl -A "uwu" -s -X POST "https://fembed9hd.com/api/source/$fembed_id" -H "x-requested-with:XMLHttpRequest" | sed -e 's/\\//g' -e 's/.*data"://' | tr "}" "\n" | sed -nE 's/.*file":"(.*)","label":"([^"]*)".*/\2>\1/p')
|
||||
[ -z "$video_links" ] || return 0
|
||||
secret_key='3933343232313932343333393532343839373532333432393038353835373532'
|
||||
iv='39323632383539323332343335383235'
|
||||
ajax_url="$base_url/encrypt-ajax.php"
|
||||
ajax=$(printf "%s" "$1" | sed -nE 's/.*id=([^&]*)&.*/\1/p' | openssl enc -e -aes256 -K "$secret_key" -iv "$iv" -a)
|
||||
video_links=$(curl -s -H "X-Requested-With:XMLHttpRequest" "$ajax_url" -d "id=$ajax" | sed -e 's/{"data":"//' -e 's/"}/\n/' -e 's/\\//g' | base64 -d | openssl enc -d -aes256 -K "$secret_key" -iv "$iv" | sed -e 's/\].*/\]/' -e 's/\\//g' | tr '{|}' '\n' | sed -nE 's/\"file\":"([^"]*)".*label.*P.*/\1/p')
|
||||
video_links=$(curl -A "$agent" -s -H "X-Requested-With:XMLHttpRequest" "$ajax_url" -d "id=$ajax" | sed -e 's/{"data":"//' -e 's/"}/\n/' -e 's/\\//g' | base64 -d | openssl enc -d -aes256 -K "$secret_key" -iv "$iv" | sed -e 's/\].*/\]/' -e 's/\\//g' | tr '{|}' '\n' | sed -nE 's/\"file\":"([^"]*)".*label.*P.*/\1/p')
|
||||
}
|
||||
|
||||
# chooses the link for the set quality
|
||||
get_video_quality() {
|
||||
dpage_url="$1"
|
||||
decrypt_link "$dpage_url"
|
||||
get_video_url() {
|
||||
decrypt_link "$dpage_link"
|
||||
case $quality in
|
||||
best)
|
||||
video_link=$(printf '%s' "$video_links" | head -n 4 | tail -n 1 | cut -d'>' -f2)
|
||||
@@ -157,275 +162,51 @@ get_video_quality() {
|
||||
*)
|
||||
video_link=$(printf '%s' "$video_links" | grep -i "${quality}p" | head -n 1 | cut -d'>' -f2)
|
||||
if [ -z "$video_link" ]; then
|
||||
err "Current video quality is not available (defaulting to best quality)"
|
||||
log "Current video quality is not available (defaulting to best quality)\n" "31"
|
||||
quality=best
|
||||
video_link=$(printf '%s' "$video_links" | head -n 4 | tail -n 1 | cut -d'>' -f2)
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
printf '%s' "$video_link"
|
||||
}
|
||||
|
||||
|
||||
###############
|
||||
# TEXT OUTPUT #
|
||||
###############
|
||||
|
||||
# display an error message to stderr (in red)
|
||||
err () {
|
||||
printf "\033[1;31m%s\033[0m\n" "$*" >&2
|
||||
}
|
||||
|
||||
# display an informational message (first argument in green, second in magenta)
|
||||
inf () {
|
||||
printf "\033[1;35m%s \033[1;35m%s\033[0m\n" "$1" "$2"
|
||||
}
|
||||
|
||||
# prompts the user with message in $1-2 ($1 in blue, $2 in magenta) and saves the input to the variables in $REPLY and $REPLY2
|
||||
prompt () {
|
||||
printf "\033[1;35m%s\033[1;35m%s\033[1;34m\033[0m" "$1" "$2"
|
||||
read -r REPLY REPLY2
|
||||
}
|
||||
|
||||
# displays an even (cyan) line of a menu line with $2 as an indicator in () and $1 as the option
|
||||
menu_line_even () {
|
||||
printf "\033[1;36m(\033[1;36m%s\033[1;36m) \033[1;36m%s\033[0m\n" "$2" "$1"
|
||||
}
|
||||
|
||||
# displays an odd (yellow) line of a menu line with $2 as an indicator in () and $1 as the option
|
||||
menu_line_odd() {
|
||||
printf "\033[1;33m(\033[1;33m%s\033[1;33m) \033[1;33m%s\033[0m\n" "$2" "$1"
|
||||
}
|
||||
|
||||
# display alternating menu lines (even and odd)
|
||||
menu_line_alternate() {
|
||||
menu_line_parity=${menu_line_parity:-0}
|
||||
if [ "$menu_line_parity" -eq 0 ]; then
|
||||
menu_line_odd "$1" "$2"
|
||||
menu_line_parity=1
|
||||
else
|
||||
menu_line_even "$1" "$2"
|
||||
menu_line_parity=0
|
||||
fi
|
||||
}
|
||||
|
||||
# displays a warning (red) line of a menu line with $2 as an indicator in [] and $1 as the option
|
||||
menu_line_strong() {
|
||||
printf "\033[1;31m(\033[1;31m%s\033[1;31m) \033[1;31m%s\033[0m\n" "$2" "$1"
|
||||
}
|
||||
|
||||
|
||||
#################
|
||||
# INPUT PARSING #
|
||||
#################
|
||||
|
||||
# only lets the user pass in case of a valid search
|
||||
process_search () {
|
||||
search_results=$(search_drama "$query")
|
||||
while [ -z "$search_results" ]; do
|
||||
err 'No search results found'
|
||||
prompt 'Search Drama: '
|
||||
query="$REPLY $REPLY2"
|
||||
search_results=$(search_drama "$query")
|
||||
done
|
||||
drama_selection "$search_results"
|
||||
episode_selection
|
||||
}
|
||||
|
||||
# drama-selection menu handling function
|
||||
drama_selection () {
|
||||
count=1
|
||||
while read -r drama_id; do
|
||||
menu_line_alternate "$drama_id" "$count"
|
||||
: $((count+=1))
|
||||
done <<-EOF
|
||||
$search_results
|
||||
EOF
|
||||
if [ -n "$select_first" ]; then
|
||||
tput clear
|
||||
choice=1
|
||||
elif [ -z "$ep_choice_to_start" ] || { [ -n "$ep_choice_to_start" ] && [ -z "$select_first" ]; }; then
|
||||
menu_line_strong "exit" "q"
|
||||
prompt "> "
|
||||
choice="$REPLY"
|
||||
while ! [ "$choice" -eq "$choice" ] 2>/dev/null || [ "$choice" -lt 1 ] || [ "$choice" -ge "$count" ] || [ "$choice" = " " ]; do
|
||||
[ "$choice" = "q" ] && exit 0
|
||||
err "Invalid choice entered"
|
||||
prompt "> "
|
||||
choice="$REPLY"
|
||||
done
|
||||
fi
|
||||
# Select respective drama_id
|
||||
selection_id="$(printf "%s" "$search_results" | sed -n "${choice}p")"
|
||||
temp_drama_id=$(printf "%s" "$selection_id" | sed 's/[0-9]*.$//')
|
||||
select_ep_result=$(check_episode "$selection_id" "$temp_drama_id")
|
||||
last_ep_number=$(printf "%s" "$select_ep_result" | head -n 1)
|
||||
first_ep_number=$(printf "%s" "$select_ep_result" | tail -n 1)
|
||||
selection_id=$temp_drama_id
|
||||
}
|
||||
|
||||
# gets episode number from user, makes sure it's in range, skips input if only one episode exists
|
||||
episode_selection () {
|
||||
if [ "$last_ep_number" -gt "$first_ep_number" ]; then
|
||||
if [ -z "$ep_choice_to_start" ]; then
|
||||
# if branches, because order matters this time
|
||||
while : ; do
|
||||
inf "To specify a range, use: start_number end_number"
|
||||
inf "Episodes:" "($first_ep_number-$last_ep_number)"
|
||||
prompt "> "
|
||||
ep_choice_start="$REPLY"
|
||||
ep_choice_end="$REPLY2"
|
||||
if [ "$REPLY" = q ]; then
|
||||
exit 0
|
||||
fi
|
||||
[ "$ep_choice_end" = "-1" ] && ep_choice_end="$last_ep_number"
|
||||
if ! [ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || { [ -n "$ep_choice_end" ] && ! [ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null; }; then
|
||||
err "Invalid number(s)"
|
||||
continue
|
||||
fi
|
||||
if [ "$ep_choice_start" -gt "$last_ep_number" ] 2>/dev/null || [ "$ep_choice_end" -gt "$last_ep_number" ] 2>/dev/null || [ "$ep_choice_start" -lt "$first_ep_number" ] 2>/dev/null; then
|
||||
err "Episode out of range"
|
||||
continue
|
||||
fi
|
||||
if [ "$ep_choice_end" -le "$ep_choice_start" ]; then
|
||||
err "Invalid range"
|
||||
continue
|
||||
fi
|
||||
break
|
||||
done
|
||||
else
|
||||
ep_choice_start="$ep_choice_to_start" && unset ep_choice_to_start
|
||||
fi
|
||||
else
|
||||
# In case the drama contains only a single episode
|
||||
ep_choice_start=1
|
||||
fi
|
||||
if [ -z "$ep_choice_end" ]; then
|
||||
auto_play=0
|
||||
else
|
||||
auto_play=1
|
||||
fi
|
||||
}
|
||||
|
||||
# creates $episodes from $ep_choice_start and $ep_choice_end
|
||||
generate_ep_list() {
|
||||
episodes=$ep_choice_start
|
||||
[ -n "$ep_choice_end" ] && episodes=$(seq "$ep_choice_start" "$ep_choice_end")
|
||||
}
|
||||
|
||||
|
||||
##################
|
||||
# VIDEO PLAYBACK #
|
||||
##################
|
||||
|
||||
append_history () { # todo: unite this with the temporary histfile writing
|
||||
grep -q "${selection_id}" "$logfile" || printf "%s%s\n" "$selection_id" $((episode+1)) >> "$logfile"
|
||||
}
|
||||
|
||||
# opens selected episodes one-by-one
|
||||
open_selection() {
|
||||
for ep in $episodes; do
|
||||
open_episode "$selection_id" "$ep"
|
||||
done
|
||||
episode=${ep_choice_end:-$ep_choice_start}
|
||||
}
|
||||
|
||||
open_episode () {
|
||||
drama_id="$1"
|
||||
episode="$2"
|
||||
|
||||
tput clear
|
||||
inf "Loading episode $episode..."
|
||||
# decrypting url
|
||||
dpage_link=$(get_dpage_link "$drama_id" "$episode")
|
||||
echo "$dpage_link"
|
||||
video_url=$(get_video_quality "$dpage_link")
|
||||
echo "$video_url"
|
||||
if [ "$is_download" -eq 0 ]; then
|
||||
# write drama and episode number and save to temporary history
|
||||
sed -E "
|
||||
s/^${selection_id}[0-9]*/${selection_id}$((episode+1))/
|
||||
" "$logfile" > "${logfile}.new"
|
||||
[ ! "$PID" = "0" ] && kill "$PID" >/dev/null 2>&1
|
||||
[ -z "$video_url" ] && die "Video URL not found"
|
||||
play_episode
|
||||
# overwrite history with temporary history
|
||||
mv "${logfile}.new" "$logfile"
|
||||
else
|
||||
mkdir -p "$download_dir"
|
||||
inf "Downloading episode $episode ..."
|
||||
episode=$(printf "%03d" "$episode")
|
||||
{
|
||||
if download "$dpage_link" "$video_url" "$drama_id" "$episode" ; then
|
||||
inf "Downloaded episode: $episode"
|
||||
else
|
||||
err "Download failed episode: $episode , please retry or check your internet connection"
|
||||
fi
|
||||
}
|
||||
fi
|
||||
}
|
||||
|
||||
play_episode () {
|
||||
# Build command
|
||||
set -- "$player_fn" "$video_url"
|
||||
case "$player_fn" in
|
||||
vlc)
|
||||
[ ! "$auto_play" -eq 0 ] && set -- "$@" "--play-and-exit"
|
||||
set -- "$@" --http-referrer="$dpage_link"
|
||||
;;
|
||||
*)
|
||||
set -- "$@" --referrer="$dpage_link" --force-media-title="${drama_id}${episode}"
|
||||
;;
|
||||
esac
|
||||
# Run Command
|
||||
if [ "$auto_play" -eq 0 ]; then
|
||||
nohup "$@" > /dev/null 2>&1 &
|
||||
else
|
||||
inf "Currently playing $display_name episode" "$episode/$last_ep_number, Range: $ep_choice_start-$ep_choice_end"
|
||||
"$@" > /dev/null 2>&1
|
||||
sleep 2
|
||||
fi
|
||||
PID=$!
|
||||
}
|
||||
|
||||
############
|
||||
# START UP #
|
||||
# Main #
|
||||
############
|
||||
|
||||
# clears the colors and deletes temporary logfile when exited using SIGINT
|
||||
trap 'printf "\033[0m";[ -f "$logfile".new ] && rm "$logfile".new;exit 1' INT HUP
|
||||
|
||||
# default options
|
||||
player_fn="mpv" #video player needs to be able to play urls
|
||||
is_download=0
|
||||
PID=0
|
||||
quality=best
|
||||
scrape=query
|
||||
download_dir="."
|
||||
choice=
|
||||
auto_play=0
|
||||
# history file path
|
||||
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/dra-hsts"
|
||||
logdir="${XDG_CACHE_HOME:-$HOME/.cache}"
|
||||
|
||||
# create history file and history dir if none found
|
||||
#create history file
|
||||
[ -d "$logdir" ] || mkdir "$logdir"
|
||||
[ -f "$logfile" ] || : > "$logfile"
|
||||
[ -f "$logfile" ] || : >"$logfile"
|
||||
|
||||
# OS detection for player selection and default download location.
|
||||
case "$(uname -o)" in
|
||||
*ndroid*)
|
||||
player_function="android_mpv"
|
||||
download_dir="/sdcard"
|
||||
alt_player="android_vlc"
|
||||
;;
|
||||
*)
|
||||
player_function="mpv"
|
||||
download_dir="."
|
||||
alt_player="vlc"
|
||||
command -v "doas" >/dev/null && sudo="doas " || sudo="sudo "
|
||||
;;
|
||||
esac
|
||||
|
||||
while getopts 'vq:dp:chDUVa:' OPT; do
|
||||
case $OPT in
|
||||
d)
|
||||
is_download=1
|
||||
player_function="download"
|
||||
;;
|
||||
a)
|
||||
ep_choice_to_start=$OPTARG
|
||||
ep_start=$OPTARG
|
||||
;;
|
||||
D)
|
||||
: > "$logfile"
|
||||
: >"$logfile"
|
||||
exit 0
|
||||
;;
|
||||
p)
|
||||
is_download=1
|
||||
player_function="download"
|
||||
download_dir=$OPTARG
|
||||
;;
|
||||
q)
|
||||
@@ -435,15 +216,15 @@ while getopts 'vq:dp:chDUVa:' OPT; do
|
||||
scrape=history
|
||||
;;
|
||||
v)
|
||||
player_fn="vlc"
|
||||
player_function=$alt_player
|
||||
;;
|
||||
U)
|
||||
update_script
|
||||
exit 0
|
||||
update_script 1
|
||||
;;
|
||||
V)
|
||||
version_text
|
||||
exit 0
|
||||
log "Version: $VERSION\n" >&2
|
||||
log "Channel: $CHANNEL\n" >&2
|
||||
update_script 0
|
||||
;;
|
||||
*)
|
||||
help_text
|
||||
@@ -453,92 +234,71 @@ while getopts 'vq:dp:chDUVa:' OPT; do
|
||||
done
|
||||
shift $((OPTIND - 1))
|
||||
|
||||
dep_ch "curl" "sed" "grep" "openssl"
|
||||
if [ "$is_download" -eq 0 ]; then
|
||||
dep_ch "$player_fn"
|
||||
else
|
||||
dep_ch "aria2c" "ffmpeg"
|
||||
#dependency checking
|
||||
log "Checking Dependencies.."
|
||||
dep_ch "curl sed grep openssl fzf"
|
||||
if [ "$player_function" = "download" ]; then
|
||||
dep_ch "aria2c ffmpeg"
|
||||
elif ! printf "%s" "$player_function" | grep -q "android";then
|
||||
dep_ch "$player_function"
|
||||
fi
|
||||
|
||||
# Get the current working URL
|
||||
base_url=$(curl -Ls -o /dev/null -w %{url_effective} https://asianembed.io)
|
||||
if [ "$scrape" = "query" ]; then
|
||||
[ -z "$*" ] && ask "Search Drama: " && read -r query || query=$*
|
||||
log "Searching Drama.."
|
||||
response=$(curl -A "$agent" -s "$base_url/search.html" -G --data-urlencode "keyword=$query" | sed -nE 's_^[[:space:]]*<a href="/videos/([^"]*)-episode-([0-9]*)">_\1 (\2 Episodes)_p')
|
||||
[ -z "$response" ] && log "No Results Found" "31" && exit 0
|
||||
log ""
|
||||
id=$(printf "%s" "$response" | tr '-' ' ' | menu "Select Drama")
|
||||
[ -z "$id" ] && log "No Drama Selected" "31" && exit 0
|
||||
# sets $title and $total_eps variable
|
||||
eval "$(printf %s "$id" | tr -s ' ' | sed -nE 's|(.*) \(([0-9]*) Episodes\)$|title="\1";total_eps="\2"|p')"
|
||||
id=$(printf "%s" "$id" | sed 's| ([0-9]* Episodes)|-episode-|g' | tr ' ' '-')
|
||||
|
||||
case $scrape in
|
||||
query)
|
||||
if [ -z "$*" ]; then
|
||||
prompt "Search Drama: "
|
||||
query="$REPLY $REPLY2"
|
||||
else
|
||||
if [ -n "$ep_choice_to_start" ]; then
|
||||
REPLY=1
|
||||
select_first=1
|
||||
fi
|
||||
query="$*"
|
||||
fi
|
||||
process_search
|
||||
;;
|
||||
history)
|
||||
search_history
|
||||
[ "$REPLY" = "q" ] && exit 0
|
||||
first_ep_number=$(check_episode "${selection_id}1" "$selection_id" | tail -1)
|
||||
;;
|
||||
*)
|
||||
die "Unexpected scrape type"
|
||||
esac
|
||||
else
|
||||
log ""
|
||||
[ ! -s "$logfile" ] && log "History is empty" "31" && exit 0
|
||||
id=$(tr '-' ' ' <"$logfile" | menu "Continue" | tr ' ' '-')
|
||||
[ -z "$id" ] && log "No Drama Selected" "31" && exit 0
|
||||
ep_start=$(printf "%s" "$id" | sed -nE 's|.*-episode-([0-9]*)|\1|p')
|
||||
log "Checking Episode $ep_start"
|
||||
response=$(curl -s "$base_url/videos/$id" -A "$agent" | sed '/Latest Episodes/,$d' | sed -nE 's_^[[:space:]]*<a href.*/videos/.*-episode-([0-9]*)">.*_\1_p;s_^[[:space:]]*<iframe src="([^"]*)".*_https:\1_p')
|
||||
[ -z "$response" ] && log "Episode Not Released" "31" && exit 0
|
||||
id=$(printf "%s" "$id" | sed 's|-episode-.*|-episode-|g')
|
||||
total_eps=$(printf "%s" "$response" | sed -n '2p')
|
||||
title=$(printf "%s" "$id" | sed 's|-episode-.*||g' | tr '-' ' ' | tr -s ' ')
|
||||
dpage_link=$(printf "%s" "$response" | head -1)
|
||||
fi
|
||||
|
||||
generate_ep_list
|
||||
append_history
|
||||
open_selection
|
||||
#shows prompt if select episode is not specified via args
|
||||
if [ -z "$ep_start" ]; then
|
||||
log "Selected: $title\n" "32"
|
||||
printf "%s" "$player_function" | grep -q "android" && display="Range is broken for android." || display="To specify a range, use: start_number end_number"
|
||||
log "$display\n"
|
||||
ask "Episodes [1-$total_eps]: "
|
||||
read -r ep_start ep_end
|
||||
fi
|
||||
|
||||
########
|
||||
# LOOP #
|
||||
########
|
||||
[ -z "$ep_start" ] && log "No Episode Selected" "31" && exit 1
|
||||
if [ -n "$ep_end" ]; then
|
||||
for episode in $(seq "$ep_start" "$ep_end"); do
|
||||
open_episode
|
||||
done
|
||||
else
|
||||
nohup="setsid -f"
|
||||
episode="${ep_start}"
|
||||
open_episode
|
||||
fi
|
||||
episode="${ep_end:-$ep_start}"
|
||||
|
||||
while :; do
|
||||
if [ -z "$select_first" ]; then
|
||||
if [ "$auto_play" -eq 0 ]; then
|
||||
display_name=$(printf '%s' "$selection_id" | sed 's/-episode-//')
|
||||
inf "Currently playing $display_name episode" "$episode/$last_ep_number"
|
||||
else
|
||||
auto_play=0
|
||||
fi
|
||||
[ "$episode" -ne "$last_ep_number" ] && menu_line_alternate 'next' 'n'
|
||||
[ "$episode" -ne "$first_ep_number" ] && menu_line_alternate 'previous' 'p'
|
||||
menu_line_alternate "replay" "r"
|
||||
[ "$last_ep_number" -ne "$first_ep_number" ] && menu_line_alternate 'select' 's'
|
||||
menu_line_strong "exit" "q"
|
||||
prompt "> "
|
||||
choice="$REPLY"
|
||||
case $choice in
|
||||
n)
|
||||
ep_choice_start=$((episode + 1))
|
||||
unset ep_choice_end
|
||||
;;
|
||||
p)
|
||||
ep_choice_start=$((episode - 1))
|
||||
unset ep_choice_end
|
||||
;;
|
||||
r)
|
||||
ep_choice_start="$episode"
|
||||
unset ep_choice_end
|
||||
;;
|
||||
s)
|
||||
episode_selection
|
||||
;;
|
||||
q)
|
||||
break
|
||||
;;
|
||||
*)
|
||||
tput clear
|
||||
err "Invalid choice"
|
||||
continue
|
||||
;;
|
||||
while cmd=$(printf "next\nreplay\nprevious\nselect\nchange_quality\nquit" | menu "Select Option"); do
|
||||
case "$cmd" in
|
||||
next) [ "$episode" -lt "$total_eps" ] && : $((episode+=1)) || flag=1 ;;
|
||||
replay) ;;
|
||||
previous) [ "$episode" -gt 1 ] && : $((episode-=1)) || flag=1 ;;
|
||||
select) ask "Episodes [1-$total_eps]: " && read -r episode ;;
|
||||
*) exit 0 ;;
|
||||
esac
|
||||
generate_ep_list
|
||||
append_history
|
||||
open_selection
|
||||
else
|
||||
wait $!
|
||||
exit
|
||||
fi
|
||||
[ -n "$flag" ] && log "Out of Range" "31" && flag="" && continue
|
||||
open_episode
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user