chore:v2, history not backwords compatible

This commit is contained in:
CoolnsX
2022-03-11 00:39:52 +05:30
parent 2becd94995
commit e3d75681ab

145
dra-cla
View File

@@ -18,7 +18,7 @@
# Project repository: https://github.com/CoolnsX/dra-cla
# Version number
VERSION="1.1.0"
VERSION="2.0.0"
#######################
@@ -68,8 +68,8 @@ die () {
exit 1
}
update_script () {
# 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" -)"
if [ -z "$update" ]; then
inf "Script is up to date :)"
@@ -82,8 +82,8 @@ update_script () {
fi
}
dep_ch () {
# 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."
@@ -99,78 +99,73 @@ dep_ch () {
# SEARCHING #
#############
# gets drama names along with its id for search term
search_drama () {
# get drama name along with its id for search term
search=$(printf '%s' "$1" | tr ' ' '+' )
curl -s "$base_url/search?type=movies&keyword=$search" |
sed -n -E 's_^[[:space:]]*<a href="/drama-detail/([^"]*)".*_\1_p'
search=$(printf '%s' "$1" | tr ' ' '-' )
curl -s "$base_url/search.html" -G -d "keyword=$search" |
sed -nE 's_^[[:space:]]*<a href="/videos/([^"]*)">_\1_p'
}
search_for_unwatched () {
# compares history with dramacool, only shows unfinished drama
check_episode () {
tmp=$(printf "%s" "$1" | sed 's/[0-9]*.$//')
curl -s "$base_url/videos/$1" | sed -nE "s_^[[:space:]]*<a href.*videos/${tmp}(.*)\">_\1_p" | head -n 1
}
search_for_unwatched () { # todo: merge into search_history
search_results="$*"
while read -r drama_id; do
temp=$(curl -s "$base_url/drama-detail/$drama_id")
current_ep_number=$(printf "%s" "$temp" | sed -nE "s/.*href.*episode-(.*).html.*class.*img.*/\1/p" | head -1)
drama_id=$(printf "%s" "$temp" | sed -nE "s/.*href=\"\/(.*)-episode-${current_ep_number}.html.*class.*img.*/\1/p" | head -1)
history_ep_number=$(sed -n -E "s/${drama_id}\t//p" "$logfile")
[ "$current_ep_number" -ge "$history_ep_number" ] && printf '%s\n' "$drama_id"
current_ep_number=$(check_episode "$drama_id")
history_ep_number=$(printf "%s" "$drama_id" | grep -Po '\K\d+' | tail -n 1)
[ -n "$current_ep_number" ] && [ "$current_ep_number" -ge "$history_ep_number" ] && printf '%s\n' "$drama_id"
done <<-EOF
$search_results
EOF
}
search_another_drama () {
tput clear
prompt "Search Drama"
query="$REPLY $REPLY2"
process_search
}
# compares history with gogoplay, only shows unfinished drama
search_history () {
tput clear
search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile")
search_results=$(cat "$logfile")
[ -z "$search_results" ] && die "History is empty"
search_results=$(search_for_unwatched "$search_results")
[ -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}\t//p" "$logfile")
ep_choice_start=$(sed -n -E "s/${selection_id}(.*)/\1/p" "$logfile")
}
##################
# URL PROCESSING #
##################
get_dpage_link() {
# get the download page url
get_dpage_link() {
drama_id="$1"
ep_no="$2"
for params in "-episode-$ep_no.html" "-$ep_no.html" "-episode-$ep_no-1.html" "-camrip-episode-$ep_no.html"; do
drama_page=$(curl -s "$base_url/$drama_id$params")
printf '%s' "$drama_page" | grep -q '<h1 class="entry-title">404</h1>' || break
done
printf '%s' "$drama_page" |
sed -n -E 's/.*class="Standard Server selected" rel="1" data-video="([^"]*)".*/\1/p' | sed 's/^/https:/g'
curl -s "$base_url/videos/${drama_id}${ep_no}" | sed -nE 's_^[[:space:]]*<iframe src="([^"]*)".*_\1_p' |
sed 's/^/https:/g'
}
decrypt_link() {
secret_key='3933343232313932343333393532343839373532333432393038353835373532'
iv='39323632383539323332343335383235'
ajax_url="https://asianembed.io/encrypt-ajax.php"
crypto_data=$(curl -s "$1" | sed -nE 's/.*data-value="([^"]*)".*/\1/p')
id=$(printf '%s' "$crypto_data" | base64 -d | openssl enc -d -aes256 -K "$secret_key" -iv "$iv" | cut -d '&' -f1)
ajax_url="$base_url/encrypt-ajax.php"
id=$(printf "%s" "$1" | sed -nE 's/.*id=(.*)&title.*/\1/p')
#encrypt and create the final ajax
# encrypt new_id using iv and secret_key
ajax=$(printf "%s\010\016\003\010\t\003\004\t" "$id" | openssl enc -aes256 -K "$secret_key" -iv "$iv" -a)
#send request and get the data(most lamest way)
data=$(curl -s -H "X-Requested-With:XMLHttpRequest" "$ajax_url" -d "id=$ajax" | sed -e 's/{"data":"//' -e 's/"}/\n/' -e 's/\\//g')
#decrypt the data to get final links
printf '%s' "$data" | base64 -d | openssl enc -d -aes256 -K "$secret_key" -iv "$iv" | sed -e 's/\].*/\]/' -e 's/\\//g' |
grep -Eo 'https:\/\/[-a-zA-Z0-9@:%._\+~#=][a-zA-Z0-9][-a-zA-Z0-9@:%_\+.~#?&\/\/=]*'
}
get_video_quality() {
# chooses the link for the set quality
get_video_quality() {
dpage_url="$1"
video_links=$(decrypt_link "$dpage_url")
case $quality in
@@ -197,32 +192,33 @@ get_video_quality() {
# TEXT OUTPUT #
###############
err () {
# display an error message to stderr (in red)
err () {
printf "\033[1;31m%s\033[0m\n" "$*" >&2
}
inf () {
# display an informational message (first argument in green, second in magenta)
inf () {
printf "\033[1;32m%s \033[1;35m%s\033[0m\n" "$1" "$2"
}
prompt () {
# 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;34m%s\033[1;35m%s\033[1;34m: \033[0m" "$1" "$2"
read -r REPLY REPLY2
}
menu_line_even () {
# 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;34m[\033[1;36m%s\033[1;34m] \033[1;36m%s\033[0m\n" "$2" "$1"
}
menu_line_odd() {
# 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;34m[\033[1;33m%s\033[1;34m] \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
@@ -234,8 +230,8 @@ menu_line_alternate() {
fi
}
menu_line_strong() {
# 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;34m[\033[1;31m%s\033[1;34m] \033[1;31m%s\033[0m\n" "$2" "$1"
}
@@ -244,10 +240,11 @@ menu_line_strong() {
# INPUT PARSING #
#################
is_number () {
is_number () { # todo make this better, or make this inline
[ "$1" -eq "$1" ] 2>/dev/null || die 'Invalid number entered'
}
# only lets the user pass in case of a valid search
process_search () {
search_results=$(search_drama "$query")
while [ -z "$search_results" ]; do
@@ -260,6 +257,7 @@ process_search () {
episode_selection
}
# drama-selection menu handling function
drama_selection () {
count=1
while read -r drama_id; do
@@ -268,7 +266,7 @@ drama_selection () {
done <<-EOF
$search_results
EOF
if [ -n "$ep_choice_to_start" ] && [ -n "$select_first" ]; then
if [ -n "$select_first" ]; then
tput clear
choice=1
elif [ -z "$ep_choice_to_start" ] || { [ -n "$ep_choice_to_start" ] && [ -z "$select_first" ]; }; then
@@ -293,18 +291,26 @@ drama_selection () {
done <<-EOF
$search_results
EOF
temp=$(curl -s "$base_url/drama-detail/$selection_id")
search_ep_result=$(printf "%s" "$temp" | sed -nE "s/.*href.*episode-(.*).html.*class.*img.*/\1/p" | head -1)
selection_id=$(printf "%s" "$temp" | sed -nE "s/.*href=\"\/(.*)-episode-${search_ep_result}.html.*class.*img.*/\1/p" | head -1)
case $scrape in
query)
select_ep_result=$(printf "%s" "$selection_id" | grep -Po '\K\d+' | tail -1);;
history)
select_ep_result=$(check_episode "$selection_id");;
*)
die "Unexpected scrape type";;
esac
read -r last_ep_number <<-EOF
$search_ep_result
$select_ep_result
EOF
selection_id=$(printf "%s" "$selection_id" | sed 's/[0-9]*.$//')
}
# gets episode number from user, makes sure it's in range, skips input if only one episode exists
episode_selection () {
# using get_dpage_link to get confirmation from episode 0 if it exists,else first_ep_number becomes "1"
first_ep_number=0
result=$(get_dpage_link "$drama_id" "$first_ep_number")
result=$(get_dpage_link "$selection_id" "$first_ep_number")
[ -z "$result" ] && first_ep_number=1
if [ "$last_ep_number" -gt "$first_ep_number" ]; then
inf "Range of episodes can be specified: start_number end_number"
@@ -340,8 +346,8 @@ episode_selection () {
fi
}
check_input() {
# checks if input is number, creates $episodes from $ep_choice_start and $ep_choice_end
check_input() {
is_number "$ep_choice_start"
episodes=$ep_choice_start
if [ -n "$ep_choice_end" ]; then
@@ -356,23 +362,23 @@ check_input() {
# VIDEO PLAYBACK #
##################
append_history () {
grep -q -w "${selection_id}" "$logfile" || printf "%s\t%s\n" "$selection_id" $((episode+1)) >> "$logfile"
append_history () { # todo: unite this with the temporary histfile writing
grep -q "${selection_id}" "$logfile" || printf "%s%s\n" "$selection_id" $((episode+1)) >> "$logfile"
}
open_selection() {
# 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 () {
open_episode () { # todo move input checking
drama_id="$1"
episode="$2"
# cool way of clearing screen
tput clear
#tput clear
# checking if episode is in range
while [ "$episode" -gt "$last_ep_number" ] || [ -z "$episode" ]; do
is_number "$ep_choice_start"
@@ -384,7 +390,6 @@ open_episode () {
prompt "Choose episode" "[$first_ep_number-$last_ep_number]"
episode="$REPLY $REPLY2"
done
#processing half episodes
if [ "$half_ep" -eq 1 ]; then
temp_ep="$episode"
episode="${episode}-5"
@@ -392,18 +397,17 @@ open_episode () {
inf "Getting data for episode $episode"
# decrypting url
dpage_link=$(get_dpage_link "$drama_id" "$episode")
printf "%s" "$dpage_link"
printf "%s\n" "$dpage_link"
video_url=$(get_video_quality "$dpage_link")
printf "%s" "$video_url"
printf "%s\n" "$video_url"
if [ "$half_ep" -eq 1 ]; then
episode="$temp_ep"
half_ep=0
fi
# Download or play episodes
if [ "$is_download" -eq 0 ]; then
# write drama and episode number and save to temporary history
sed -E "
s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+1))/
s/^${selection_id}[0-9]*/${selection_id}$((episode+1))/
" "$logfile" > "${logfile}.new"
[ "$PID" -ne 0 ] && kill "$PID" >/dev/null 2>&1
[ -z "$video_url" ] && die "Video URL not found"
@@ -428,11 +432,11 @@ open_episode () {
;;
esac
PID=$!
# overwrite history with temporary history
mv "${logfile}.new" "$logfile"
else
mkdir -p "$download_dir"
inf "Downloading episode $episode ..."
# add 0 padding to the episode name
episode=$(printf "%03d" "$episode")
{
if aria2c -x 16 -s 16 --referer="$dpage_link" "$video_url" --dir="$download_dir" -o "${drama_id}-${episode}.mp4" --download-result=hide ; then
@@ -448,7 +452,7 @@ open_episode () {
# START UP #
############
# to clear the colors when exited using SIGINT
# 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
@@ -516,17 +520,14 @@ while getopts 'vq:dp:chDUVa:' OPT; do
done
shift $((OPTIND - 1))
# check for main dependencies
dep_ch "curl" "sed" "grep" "git" "openssl"
# check for optional dependencies
if [ "$is_download" -eq 0 ]; then
dep_ch "$player_fn"
else
dep_ch "aria2c"
fi
# dramacool likes to change domains but keep the olds as redirects
base_url="https://dramacool.fo"
base_url="https://asianembed.io"
case $scrape in
query)
if [ -z "$*" ]; then
@@ -545,7 +546,7 @@ case $scrape in
search_history
[ "$REPLY" = "q" ] && exit 0
first_ep_number=0
result=$(get_dpage_link "$drama_id" "$first_ep_number")
result=$(get_dpage_link "$selection_id" "$first_ep_number")
[ -z "$result" ] && first_ep_number=1
;;
*)
@@ -576,7 +577,6 @@ if [ -z "$select_first" ]; then
menu_line_alternate "select quality (current: $quality)" "b"
menu_line_strong "exit" "q"
prompt "Enter choice"
# process user choice
choice="$REPLY"
case $choice in
n)
@@ -599,7 +599,10 @@ if [ -z "$select_first" ]; then
ep_choice_end=
;;
a)
search_another_drama
tput clear
prompt "Search Drama"
query="$REPLY $REPLY2"
process_search
;;
h)
search_history