#!/bin/bash

baseurl="https://www.sexart.com"
webhookurl="$1"

parameters="-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8' -H 'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2' -H 'Upgrade-Insecure-Requests: 1' -H 'Sec-Fetch-Dest: document' -H 'Sec-Fetch-Mode: navigate' -H 'Sec-Fetch-Site: same-origin' -H 'Sec-Fetch-User: ?1' -H 'Connection: keep-alive' -H 'Cookie: msite_prod_session=s%3AdfyNB2GAxlNviiRxPeV1DRtOgStQfL3C.dD8%2FN0UVIl5jiVLD%2FSj32757tXitgl0h%2BZXjikvkGaY; mam=65.SA.13.793.5.0.0; mamident=549A1A6D34804C9AB35D073958DE043F.4FE9E486B9D64F09B6637C66FAF61B83; _csrf=Z3fQYWAia9x9eGLIlY-NYiaO; history=%5B%7B%22text%22%3A%22Updates%20Stream%22%2C%22url%22%3A%22%2Fupdates%22%7D%5D; _csrfToken=YsfR9G9y-X9e_8iIBR4XSztU40oznyrcWgs0; auuid=eb6d8820-b20a-4676-bdcb-3051d8d169a8' -H 'If-None-Match: W/"11a2e-ZLRQ2FHtbPnu2o76MyLhehVE+O4"'"

function migration() {
    cat /dev/null > maindb.txt
    for line in `cat finished list | uniq`
    do
        title="${line##*/}"
        metdate="${line%/*}"
        metdate="${metdate##*/}"
        grepresult=`grep "$title.$metdate" results.txt`
        if [ "$grepresult" ]
        then
            guildedlink=`echo "$grepresult" | cut -f3 -d'|'`
            guildedlinksize=`echo "$grepresult" | cut -f2 -d'|'`
            guildedlinkchecksum=`echo "$grepresult" | cut -f4 -d'|'`
            echo "$line|1|$guildedlink|$guildedlinksize|$guildedlinkchecksum" >> maindb.txt
        else
            echo "$line|0" >> maindb.txt
        fi
    done
}

function sexart_videolist() {
    OLD_IFS=$IFS
    IFS=$'\n'
    
    totalvideos=`eval "curl -k '$baseurl/api/movies?galleryType=MOVIE&first=60&page=1&staffSelectionHead=false&tabId=0&order=DATE&direction=ASC&type=MOVIE' $parameters" | sed 's/,/\n/g' | grep "total" | sed 's/:/\n/g;s/}//g' | grep -Eo [0-9]*`
    
    for page in `seq 1 $((totalvideos/60+1))`
    do
        for line in `eval "curl -k '$baseurl/api/movies?galleryType=MOVIE&first=60&page=$page&staffSelectionHead=false&tabId=0&order=DATE&direction=ASC&type=MOVIE' $parameters" | sed 's/},{/\n/g'`
        do
            if [ `echo $line | grep -c '"isStaffSelection":true'` -eq 0 ] # not staff selection
            then
                selectedurl=`echo $line | sed 's/,/\n/g' | grep path | sed 's/"/\n/g' | grep movie`
                [ "$selectedurl" ] && echo "$selectedurl|0" >> maindb.txt
            fi
        done
    done
    
    IFS=$OLD_IFS
}

function sexart_piclist() {
    OLD_IFS=$IFS
    IFS=$'\n'
    
    totalpics=`eval "curl -k '$baseurl/api/galleries?first=60&galleryType=GALLERY&page=1&order=DATE&direction=ASC' $parameters" | sed 's/,/\n/g' | grep "total" | sed 's/:/\n/g;s/}//g' | grep -Eo [0-9]*`
    
    for page in `seq 1 $((totalpics/60+1))`
    do
        eval "curl -k '$baseurl/api/galleries?first=60&galleryType=GALLERY&page=$page&order=DATE&direction=ASC' $parameters" | sed 's/},{/\n/g' | sed 's/,/\n/g' | grep path | sed 's/"/\n/g' | grep gallery
    done
    IFS=$OLD_IFS
}

function sexart_pics() { # $1 = title, $2 = date
    OLD_IFS=$IFS
    IFS=$'\n'
    title="$1"
    metdate="$2"
    jsontemp=`eval "curl -k --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 '$baseurl/api/gallery?name=$title&date=$metdate' $parameters"`
    echo "$jsontemp"
    uuid=`echo "$jsontemp" | grep -Po '"originalUUID".*?,' | sed 's/^.*:"//g;s/".*//g'`
    commentsdump=`eval "curl -k --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 '$baseurl/api/comments?objectUUID=$uuid&order=TIMESTAMP&direction=DESC' $parameters"`
    echo "$commentsdump"
    for avaliablequality in `echo "$jsontemp" | sed 's/\\\\\\//\\//g;' | grep -Po '"zips":.*' | sed 's/,/\n/g' | grep quality | sed 's/^.*://g;s/"//g'`
    do
        eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -kI '$baseurl/api/download-media/$uuid/photos/$avaliablequality' $parameters" 2> /dev/null | grep -E "(l|L)ocation:" | sed 's/location: //g;s/Location: //g;s/location://g;s/Location://g' | sed 's/\r//g'
    done
    IFS=$OLD_IFS
}

function sexart_movie() { # $1 = title, $2 = date
    OLD_IFS=$IFS
    IFS=$'\n'
    title="$1"
    metdate="$2"
    jsontemp=`eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -k '$baseurl/api/movie?name=$title&date=$metdate' $parameters"`
    echo "$jsontemp"
    uuid=`echo "$jsontemp" | grep -Po '"crew":.*?"UUID".*?"name' | sed 's/^.*UUID//g;s/":"//g;s/".*//g'`
    commentsdump=`eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -k '$baseurl/api/comments?objectUUID=$uuid&order=TIMESTAMP&direction=DESC' $parameters"`
    echo "$commentsdump"
    for avaliablequality in `echo "$jsontemp" | sed 's/\\\\\\//\\//g;' | grep -Po '"videos":.*?,"zip' | sed 's/"videos":"//g;s/","zip//g' | sed 's/,/\n/g' | grep id | sed 's/^.*://g;s/"//g'`
    do
        eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -kI '$baseurl/api/download-media/$uuid/film/$avaliablequality' $parameters" 2> /dev/null | grep -E "(l|L)ocation:" | sed 's/location: //g;s/Location: //g;s/location://g;s/Location://g' | sed 's/\r//g'
    done
    for avaliablequality in `echo "$jsontemp" | sed 's/\\\\\\//\\//g;' | grep -Po '"zips":.*' | sed 's/,/\n/g' | grep quality | sed 's/^.*://g;s/"//g'` # some sexart movies also contain images zip
    do
        eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -kI '$baseurl/api/download-media/$uuid/photos/$avaliablequality' $parameters" 2> /dev/null | grep -E "(l|L)ocation:" | sed 's/location: //g;s/Location: //g;s/location://g;s/Location://g' | sed 's/\r//g'
    done
    IFS=$OLD_IFS
}

# function sexart() { # $1 = url
    # url="$1"

    # title="${url##*/}"
    # metdate="${url%/*}"
    # metdate="${metdate##*/}"
    
    # [ `echo "$url" | grep -c "movie"` -gt 0 ] && sexart_movie "$title" "$metdate"
    # [ `echo "$url" | grep -c "gallery"` -gt 0 ] && sexart_pics "$title" "$metdate"
# }

# echo "Content-type: text/plain"
# echo ""

# sexart "$QUERY_STRING"
# exit 0

# sexart "$1"

function antics_movie() { # $1 = title, $2 = date
    OLD_IFS=$IFS
    IFS=$'\n'
    mkdir "$title.$metdate"
    cd "$title.$metdate"
    
    jsontemp=`eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -k '$baseurl/api/movie?name=$title&date=$metdate' $parameters"`
    echo "$jsontemp" > "$title.$metdate.metadata.json"
    uuid=`echo "$jsontemp" | grep -Po '"crew":.*?"UUID".*?"name' | sed 's/^.*UUID//g;s/":"//g;s/".*//g'`
    commentsdump=`eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -k '$baseurl/api/comments?objectUUID=$uuid&order=TIMESTAMP&direction=DESC' $parameters"`
    echo "$commentsdump" > "$title.$metdate.comments.json"
    for avaliablequality in `echo "$jsontemp" | sed 's/\\\\\\//\\//g;' | grep -Po '"videos":.*?,"zip' | sed 's/"videos":"//g;s/","zip//g' | sed 's/,/\n/g' | grep id | sed 's/^.*://g;s/"//g'`
    do
        eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -kI '$baseurl/api/download-media/$uuid/film/$avaliablequality' $parameters" 2> /dev/null | grep -E "(l|L)ocation:" | sed 's/location: //g;s/Location: //g;s/location://g;s/Location://g' | sed 's/\r//g'
    done >> list
    for avaliablequality in `echo "$jsontemp" | sed 's/\\\\\\//\\//g;' | grep -Po '"zips":.*' | sed 's/,/\n/g' | grep quality | sed 's/^.*://g;s/"//g'` # some sexart movies also contain images zip
    do
        eval "curl --connect-timeout 2 --retry 20 --retry-delay 0 --retry-max-time 40 -kI '$baseurl/api/download-media/$uuid/photos/$avaliablequality' $parameters" 2> /dev/null | grep -E "(l|L)ocation:" | sed 's/location: //g;s/Location: //g;s/location://g;s/Location://g' | sed 's/\r//g'
    done >> list
    IFS=$OLD_IFS
    
    aria2c --check-certificate=false -k 1M -x 128 -s 128 -j 64 -R -c --auto-file-renaming=false -i list
    
    cd ..
    
    time ~/guilded_dir.v3.1.sh "$title.$metdate" "$webhookurl"
    rm "$title.$metdate" -rf
}

function downloadantics() {
    OLD_IFS=$IFS
    IFS=$'\n'

    totallines=`cat list | wc -l`
    currentlines=0

    while [ -f "list" ]
    do
        currentline=`head -1 list`
        title="${currentline##*/}"
        metdate="${currentline%/*}"
        metdate="${metdate##*/}"
        antics_movie >> log 2>&1

        head -1 list >> finished
        tail -n +2 list > list2
        [ -s "list2" ] && mv list2 list
        
        let currentlines++
        echo -e "\e[32m$title.$metdate\e[36m finished in \e[32m`date`, \e[36m$currentlines\e[32m/$totallines\e[0m"
    done

    IFS=$OLD_IFS
}

function downloadantics_v2() {
    OLD_IFS=$IFS
    IFS=$'\n'

    for line in `cat maindb.txt`
    do
        url=`echo "$line" | cut -f1 -d'|'`
        if [ `echo "$line" | cut -f2 -d'|'` -eq 0 ]
        then
            echo -e "\e[36mprocessin' \e[32m$url\e[0m"
            title="${url##*/}"
            metdate="${url%/*}"
            metdate="${metdate##*/}"
            
            antics_movie >> log 2>&1
            
            guildedlink=`grep "$postid" results.txt | tail -1 | cut -f3 -d'|'`
            guildedlinksize=`grep "$postid" results.txt | tail -1 | cut -f2 -d'|'`
            guildedlinkchecksum=`grep "$postid" results.txt | tail -1 | cut -f4 -d'|'`
            
            echo -e "\e[36mpost \e[32m$url\e[36m finished processin', guilded link: \e[32m$guildedlink\e[36m, change written into maindb\e[0m"
            
            source="$(echo "$url|0" | sed 's:[]\[^$.*/&]:\\&:g')"
            dest="$(echo "$url|1|$guildedlink|$guildedlinksize|$guildedlinkchecksum" | sed 's:[]\[^$.*/&]:\\&:g')"
            sed -i "s/$source/$dest/g" maindb.txt
        else
            echo -e "\e[31m$url was either processed, or didn't need to process\e[0m"
        fi
    done

    IFS=$OLD_IFS
}

downloadantics_v2
