about summary refs log tree commit diff stats
path: root/home-manager/packages/scripts/apps/deprecated/walldl
blob: 3b6752db026bbcdd24665c842b89a4c92894a50c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/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