#!/bin/sh

#stolen from pystardust/ani-cli
_decrypt_link() {
	ajax_url='https://gogoplay.io/encrypt-ajax.php'

	#get the id from the url
	video_id=$(echo "$1" | cut -d\? -f2 | cut -d\& -f1 | sed 's/id=//g')

	#construct ajax parameters
	secret_key='3235373436353338353932393338333936373634363632383739383333323838'
	iv='34323036393133333738303038313335'
	ajax=$(echo "$video_id" | openssl enc -aes256  -K "$secret_key" -iv "$iv" -a)

	#send the request to the ajax url
	curl -s -H 'x-requested-with:XMLHttpRequest' "$ajax_url" -d "id=$ajax" -d "time=69420691337800813569" |
	sed -e 's/\].*/\]/' -e 's/\\//g' |
	grep -Eo 'https:\/\/[-a-zA-Z0-9@:%._\+~#=][a-zA-Z0-9][-a-zA-Z0-9@:%_\+.~#?&\/\/=]*'
}

#stolen from pystardust/ani-cli
_ani_category_get_episodes () {
    sed -n -E '
		/^[[:space:]]*<a href="#" class="active" ep_start/{
		s/.* '\''([0-9]*)'\'' ep_end = '\''([0-9]*)'\''.*/\2/p
		q
		}' "$1"
}

scrape_ani_category () {
    search="$1"
    output_json_file="$2"
    #stolen from pystardust/ani-cli
    base_url=$(curl -s -L -o /dev/null -w "%{url_effective}\n" https://gogoanime.cm)
    _tmp_html="${session_temp_dir}/ani-category.html"
    _get_request "$base_url/category/$search" > "$_tmp_html"
    episode_count="$(_ani_category_get_episodes "$_tmp_html")"
    command_exists "openssl" || die 3 "openssl is a required dependency for ani, please install it\n"
    i=1
    _start_series_of_threads
    while [ $i -le "$episode_count" ]; do
	{
	    print_info "Scraping anime episode $i\n"
	    _tmp_json="${session_temp_dir}/ani-category-$i.json.final"
	    #stolen from pystardust/ani-cli
	    dpage_link=$(curl -s "$base_url/$search-episode-$i" | sed -n -E 's/^[[:space:]]*<a href="#" rel="100" data-video="([^"]*)".*/\1/p' | sed 's/^/https:/g')
	    url="$(_decrypt_link "$dpage_link" | head -n 4 | tail -n 1)"
	    echo "[]" | jq --arg url "$url" --arg title "$search episode $i" '[{"url": $url, "title": $title, "ID": $title}]' > "$_tmp_json"
	} &
	: $((i+=1))
	_thread_started "$!"
    done
    wait
    _concatinate_json_file "${session_temp_dir}/ani-category-" "$episode_count" "$output_json_file"
}
