about summary refs log blame commit diff stats
path: root/home-manager/packages/scripts/apps/deprecated/walldl
blob: 3b6752db026bbcdd24665c842b89a4c92894a50c (plain) (tree)












































































































































































                                                                                                                                
#!/bin/bash
############################################################
# Help                                                     #
############################################################
Help()
{
    # Display Help
    echo "Fetch all images accociated with a given URL."
    echo
    echo "options:"
    echo "-h     Print this Help."
    echo "-H     For Sites with hrefs"
    echo "-L     For Sites without hrefs"
    echo "-C     For generated Code"
    echo
}

############################################################
############################################################
# Main program                                             #
############################################################
############################################################
############################################################
#  Create all needed tmp files and variables.              #
############################################################
Tav() {
    pstr="[=======================================================================]"
    out="$(mktemp)"
    html="$(mktemp)"
    NR="$(mktemp)"
    printf "URL for Website: "
    read url
    dir=$(echo "$url" | awk '{ gsub(":|https",""); print $0 }')
    try=HighRes
}
############################################################
# Define downloader engines                                #
############################################################
HighRes() {
    # For Websites that have hrefs for bigger images
    curl -sS  $url |  awk 'BEGIN { FS="\"";} /href=/ && /img src/ && !/index.html/ { print $2 } ' | awk '!a[$0]++' | sort > $out
}
LowRes() {
    # For Websites that don't have hrefs
    wget --quiet --output-document=${html} $url
    cat $html | tidy --custom-tags pre --show-warnings no -q -output $html
    cat $html | awk 'BEGIN { FS="\"";} /src=/ {print $2}' > $out
}
GenCode() {
    # For Generated Code blocks
    wget --quiet --output-document=${html} $url
    cat $html | tidy --custom-tags pre --show-warnings no -q -output $html

    cat $html | awk ' /<img/ {print NR}' > $NR
    for ((i = 1; i <= $(cat $NR | wc -l); i++))
    do
        lnNums=$(cat $NR | awk -v n=$i ' NR == n {print $0} ')
        let "lnNums++"
        cat $html | awk -v n=$lnNums 'BEGIN { FS="\"";} NR == n { print $2 } ' >> $out
    done
}
############################################################
# Process the input options. Add options as needed.        #
############################################################
# Get the options
while getopts ":hHLGV" option; do
    case $option in
        H)
            proc=HighRes
            ;;
        L)
            proc=LowRes
            ;;
        G)
            proc=GenCode
            ;;
        V)
            ver=true
            ;;
        \?) # Invalid option
            echo "Error: Invalid option"
            ;;
        * | h)
            Help
            exit;;
    esac
done
############################################################
# Download it                                              #
############################################################
Down() {
    total=$(cat $out | wc -l)
    if [ $total -eq 0 ];
    then
        case $try in
            HighRes)
                if [ ver ];
                then
                    echo "trying HighRes engine"
                fi
                HighRes
                try="GenCode"
                Down
                ;;
            LowRes)
                if [ ver ];
                then
                    echo "trying LowRes engine"
                fi
                LowRes
                try=":("
                Down
                ;;
            GenCode)
                if [ ver ];
                then
                    echo "trying GenCode engine"
                fi
                GenCode
                try="LowRes"
                Down
                ;;
            *)
                echo "Nothing found"
                exit
                ;;
        esac

    else
        cat $out
        read -p "Do you want to download this to ${WALLPAPERDIR}/${dir}?(y/n) " -n 1 -r
        echo

        if [[ $REPLY =~ ^[Yy]$ ]];
        then
            mkdir -p ${WALLPAPERDIR}/${dir}
            cd ${WALLPAPERDIR}/${dir}
            url=$(echo $url | awk ' BEGIN {FS="/";} {printf "%s//%s/", $1, $3 } ' )
            # cycle throught all pictures and download them
            for ((i = 0; i <= $total; i++ ))
            do
                awk -v n=$i 'FNR == n ' ${out} | xargs printf "$url%s\n" | xargs curl -OsS
                pd=$(( $i * 73 / $total ))
                printf "\r%3d.%1d%% %.${pd}s" $(( $i * 100 / $total )) $(( ($i * 1000 / $total) % 10 )) $pstr
            done
            printf "\n"
            echo "downloaded to ${WALLPAPERDIR}${dir}"
            rm -f "$out"
            rm -f "$html"
            rm -f "$NR"
        fi
    fi
}
############################################################
# Call the functions                                       #
############################################################

Tav
# case $proc in
#     HighRes)
#         HighRes
#         ;;
#     LowRes)
#         LowRes
#         ;;
#     GenCode)
#         GenCode
#         ;;
#     *)
#         echo "No engine specified"
#         exit
#         ;;
# esac
Down