-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgoogliser.sh
executable file
·3575 lines (2837 loc) · 100 KB
/
googliser.sh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
####################################################################################
# googliser.sh
#
# (C)opyright 2016-2020 Teracow Software
#
# If you find this script useful, please send me an email to let me know. :)
# teracow@gmail.com
#
# The latest copy can be found here [https://github.com/teracow/googliser]
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see http://www.gnu.org/licenses/.
####################################################################################
# * Style Guide *
# function names: CamelCase
# forked function names: TrailingUnderscore_
# sub-function names: :LeadingColon
# variable names: lowercase_with_underscores (except for 'returncode' & 'errorcode')
# constants: UPPERCASE_WITH_UNDERSCORES
# indents: 4 x spaces
####################################################################################
# return values ($?):
# 0 completed successfully
# 1 required/alternative program unavailable (wget, curl, montage, convert, identify, brew, etc...)
# 2 required parameter unspecified or wrong
# 3 could not create output directory for 'phrase'
# 4 could not get a list of search results from Google
# 5 URL links list has been exhausted
# 6 thumbnail gallery building failed
# 7 unable to create a temporary build directory
# 8 Internet inaccessible
# debug log first characters notation:
# >> child process forked
# << child process ended
# \\ function entry
# // function exit
# VV variable value
# ?? other value
# == execution
# ~~ variable was reset within bounds
# $$ success
# xx warning
# !! failure
# TT elapsed time
# ## comment
readonly ORIGIN=$_
InitOK()
{
# check and log runtime environment
# $? = 0 if OK, 1 if not
# script constants
local -r SCRIPT_VERSION=200212
readonly DEBUG_FILE=debug.log
readonly IMAGE_FILE_PREFIX=image
readonly SCRIPT_FILE=googliser.sh
readonly SCRIPT_STARTSECONDS=$(date +%s)
readonly SCRIPT_VERSION_PID="v:$SCRIPT_VERSION PID:$$"
readonly USERAGENT='--user-agent "Mozilla/5.0 (X11; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0"'
# parameter default constants
readonly GALLERY_BORDER_PIXELS_DEFAULT=30
readonly GALLERY_THUMBNAIL_DIMENSIONS_DEFAULT=400x400
readonly IMAGES_REQUESTED_DEFAULT=36
readonly LOWER_SIZE_BYTES_DEFAULT=2000
readonly PARALLEL_LIMIT_DEFAULT=64
readonly RETRIES_DEFAULT=3
readonly TIMEOUT_SECONDS_DEFAULT=30
readonly UPPER_SIZE_BYTES_DEFAULT=200000
# limits
readonly BING_RESULTS_MAX=1000
readonly GOOGLE_RESULTS_MAX=1000
readonly PARALLEL_MAX=512
readonly RETRIES_MAX=100
readonly TIMEOUT_SECONDS_MAX=600
# script-variables
current_path=$PWD
errorcode=0
gallery_images_required=$IMAGES_REQUESTED_DEFAULT # number of images to build gallery with. This is ideally same as $user_images_requested except when performing random (single) image download.
image_links_file=image.links.list
# script-variable flags
target_path_created=false
# user-variable parameters
gallery_border_pixels=$GALLERY_BORDER_PIXELS_DEFAULT
gallery_thumbnail_dimensions=$GALLERY_THUMBNAIL_DIMENSIONS_DEFAULT
lower_size_bytes=$LOWER_SIZE_BYTES_DEFAULT
parallel_limit=$PARALLEL_LIMIT_DEFAULT
retries=$RETRIES_DEFAULT
timeout_seconds=$TIMEOUT_SECONDS_DEFAULT
upper_size_bytes=$UPPER_SIZE_BYTES_DEFAULT
user_images_requested=$IMAGES_REQUESTED_DEFAULT
# user-variable options
debug=false
exact_search=false
gallery=false
gallery_background_trans=false
gallery_compact_thumbs=false
gallery_delete_images=false
lightning_mode=false
links_only=false
output_colour=true
output_verbose=true
random_image=false
reindex_rename=false
safesearch_on=true
save_links=false
show_help=false
skip_no_size=false
# user-variable strings
exclude_links_pathfile=''
exclude_words=''
gallery_user_title=''
input_links_pathfile=''
input_phrases_pathfile=''
output_path=''
search_phrase=''
sites=''
user_phrase=''
# user-variable presets
aspect_ratio=''
image_colour=''
image_format=''
image_type=''
min_pixels=''
recent=''
usage_rights=''
BuildWorkPaths || return 1
DebugScriptEntry
DebugScriptNow
DebugScriptVal version "$SCRIPT_VERSION"
DebugScriptVal PID "$$"
FindLauncher
FindPackageManager || return 1
FindGNUUtils || return 1
user_parameters=$($GETOPT_BIN -o d,E,G,h,L,q,s,S,z,a:,b:,i:,l:,m:,n:,o:,p:,P:,r:,R:,t:,T:,u: -l debug,exact-search,help,lightning,links-only,no-colour,no-color,safesearch-off,quiet,random,reindex-rename,save-links,skip-no-size,aspect-ratio:,border-pixels:,colour:,color:,exclude-links:,exclude-words:,format:,gallery:,input-links:,input-phrases:,lower-size:,minimum-pixels:,number:,output:,parallel:,phrase:,recent:,retries:,sites:,thumbnails:,timeout:,title:,type:,upper-size:,usage-rights: -n "$LAUNCHER" -- "$@")
user_parameters_result=$?
# shellcheck disable=SC2034
user_parameters_raw=$*
# shellcheck disable=SC2119
WhatAreMyArgs
ShowHelp || return 1
ShowTitle
[[ $output_verbose = true ]] && echo
ValidateScriptParameters || return 1
if [[ $errorcode -eq 0 ]]; then
DebugFuncComment 'runtime parameters after validation and adjustment'
DebugFuncVar aspect_ratio
DebugFuncVar debug
DebugFuncVar exact_search
DebugFuncVar exclude_links_pathfile
DebugFuncVar exclude_words
DebugFuncVar image_colour
DebugFuncVar image_format
DebugFuncVar image_type
DebugFuncVar input_links_pathfile
DebugFuncVar input_phrases_pathfile
DebugFuncVar gallery
DebugFuncVar gallery_background_trans
DebugFuncVar gallery_border_pixels
DebugFuncVar gallery_compact_thumbs
DebugFuncVar gallery_delete_images
DebugFuncVar gallery_images_required
DebugFuncVar gallery_thumbnail_dimensions
DebugFuncVar gallery_user_title
DebugFuncVar lightning_mode
DebugFuncVar links_only
DebugFuncVar lower_size_bytes
DebugFuncVar min_pixels
DebugFuncVar output_colour
DebugFuncVar output_path
DebugFuncVar output_verbose
DebugFuncVar parallel_limit
DebugFuncVar random_image
DebugFuncVar recent
DebugFuncVar reindex_rename
DebugFuncVar retries
DebugFuncVar safesearch_on
DebugFuncVar save_links
DebugFuncVar sites
DebugFuncVar skip_no_size
DebugFuncVar timeout_seconds
DebugFuncVar upper_size_bytes
DebugFuncVar user_phrase
DebugFuncVar usage_rights
DebugFuncVar user_images_requested
DebugFuncComment 'internal parameters'
DebugFuncVar BING_RESULTS_MAX
DebugFuncVar GOOGLE_RESULTS_MAX
DebugFuncVar ORIGIN
DebugFuncVar OSTYPE
DebugFuncVar TEMP_PATH
FindDownloader || return 1
FindImageMagick || return 1
trap CTRL_C_Captured INT
fi
return 0
}
BuildWorkPaths()
{
# $? = 0 if OK, 1 if not
local OK=false
while true; do # yes, it's a single-run loop - easier to abort when things go wrong
TEMP_PATH=$(mktemp -d "/tmp/${SCRIPT_FILE%.*}.$$.XXX") || break
page_run_count_path=$TEMP_PATH/pages.running.count
mkdir -p "$page_run_count_path" || break
page_success_count_path=$TEMP_PATH/pages.success.count
mkdir -p "$page_success_count_path" || break
page_fail_count_path=$TEMP_PATH/pages.fail.count
mkdir -p "$page_fail_count_path" || break
page_abort_count_path=$TEMP_PATH/pages.abort.count
mkdir -p "$page_abort_count_path" || break
image_run_count_path=$TEMP_PATH/images.running.count
mkdir -p "$image_run_count_path" || break
image_success_count_path=$TEMP_PATH/images.success.count
mkdir -p "$image_success_count_path" || break
image_fail_count_path=$TEMP_PATH/images.fail.count
mkdir -p "$image_fail_count_path" || break
image_abort_count_path=$TEMP_PATH/images.abort.count
mkdir -p "$image_abort_count_path" || break
image_sizetest_pathfile=$TEMP_PATH/test-image-size
pages_pathfile=$TEMP_PATH/page.html
gallery_title_pathfile=$TEMP_PATH/gallery.title.png
gallery_thumbnails_pathfile=$TEMP_PATH/gallery.thumbnails.png
gallery_background_pathfile=$TEMP_PATH/gallery.background.png
image_links_pathfile=$TEMP_PATH/$image_links_file
debug_pathfile=$TEMP_PATH/$DEBUG_FILE
OK=true
break
done
if [[ $OK = false ]]; then
ShowFail 'Unable to create a temporary build directory'
errorcode=7
return 1
fi
return 0
}
WhatAreMyArgs()
{
DebugFuncVar user_parameters_raw
eval set -- "$user_parameters"
while true; do
case $1 in
--aspect-ratio|-a)
aspect_ratio=$2
shift 2
;;
--border-pixels|-b)
gallery_border_pixels=$2
gallery=true
shift 2
;;
--colour|--color)
image_colour=$2
shift 2
;;
--debug|-d)
debug=true
shift
;;
--exact-search|-E)
exact_search=true
shift
;;
--exclude-links)
exclude_links_pathfile=$2
shift 2
;;
--exclude-words)
exclude_words=$2
shift 2
;;
--format)
image_format=$2
shift 2
;;
-G)
gallery=true
shift
;;
--gallery)
case $2 in
background-trans)
gallery_background_trans=true
;;
compact)
gallery_compact_thumbs=true
;;
delete-after)
gallery_delete_images=true
;;
esac
gallery=true
shift 2
;;
--help|-h)
show_help=true
errorcode=2
return 1
;;
--input-links)
input_links_pathfile=$2
shift 2
;;
--input-phrases|-i)
input_phrases_pathfile=$2
shift 2
;;
--lightning|-z)
lightning_mode=true
shift
;;
--links-only|-L)
links_only=true
shift
;;
--lower-size|-l)
lower_size_bytes=$2
shift 2
;;
--minimum-pixels|-m)
min_pixels=$2
shift 2
;;
--no-colour|--no-color)
output_colour=false
shift
;;
--number|-n)
user_images_requested=$2
shift 2
;;
--output|-o)
output_path=$2
shift 2
;;
--parallel|-P)
parallel_limit=$2
shift 2
;;
--phrase|-p)
user_phrase=$2
shift 2
;;
--quiet|-q)
output_verbose=false
shift
;;
--random)
random_image=true
shift
;;
--recent|-R)
recent=$2
shift 2
;;
--reindex-rename)
reindex_rename=true
shift
;;
--retries|-r)
retries=$2
shift 2
;;
--safesearch-off)
safesearch_on=false
shift
;;
--save-links|-s)
save_links=true
shift
;;
--sites)
sites=$2
shift 2
;;
--skip-no-size|-S)
skip_no_size=true
shift
;;
--thumbnails)
gallery_thumbnail_dimensions=$2
shift 2
;;
--timeout|-t)
timeout_seconds=$2
shift 2
;;
--title|-T)
if [[ $(Lowercase "$2") = none ]]; then
gallery_user_title=none
else
gallery_user_title=$2
fi
shift 2
;;
--type)
image_type=$2
shift 2
;;
--upper-size|-u)
upper_size_bytes=$2
shift 2
;;
--usage-rights)
usage_rights=$2
shift 2
;;
--)
shift # shift to next parameter in $1
break
;;
*)
break # there are no more matching parameters
;;
esac
done
return 0
}
ShowHelp()
{
# $? = 0 if OK, 1 if not
if [[ $user_parameters_result -ne 0 || $user_parameters = ' --' ]]; then
ShowBasicHelp
errorcode=2
return 1
fi
if [[ $show_help = true ]]; then
if (command -v less >/dev/null); then
ShowExtendedHelp | LESSSECURE=1 less -rMK -PM' use arrow-keys to scroll up-down left-right, press Q to quit'
elif (command -v more >/dev/null); then
ShowExtendedHelp | more -d
else
ShowExtendedHelp
fi
return 1
fi
return 0
}
ShowBasicHelp()
{
ShowTitle
echo
echo " Search '$(ShowGoogle) $(ColourTextBrightBlue images)' then download a number of images matching a phrase"
echo
echo " Usage: $(ColourTextBold "$LAUNCHER") -p [TEXT] -dEGhLqsSz [PARAMETERS] FILE,PATH,TEXT,INTEGER,PRESET ..."
}
ShowExtendedHelp()
{
local SAMPLE_USER_PHRASE=cows
ShowBasicHelp
echo
echo " External requirements: Wget or cURL"
echo " and optionally: identify, montage & convert (from ImageMagick)"
echo
echo " Questions or comments? teracow@gmail.com"
echo
echo " Mandatory arguments for long options are mandatory for short options too."
echo
FormatHelpSection Required
FormatHelpLine p phrase string "Search for images Google identifies with this phrase. Enclose whitespace in quotes. A sub-directory will be created with this name, unless '--output' is specified."
echo
FormatHelpSection Optional
FormatHelpLine a aspect-ratio preset 'Search for images with this aspect-ratio.'
FormatHelpLine example '--aspect-ratio square'
FormatHelpLine 'presets:'
FormatHelpLine preset tall
FormatHelpLine preset square
FormatHelpLine preset wide
FormatHelpLine preset panoramic
FormatHelpLine b border-pixels integer 'Thickness of border surrounding gallery image in pixels.'
FormatHelpLine default "$GALLERY_BORDER_PIXELS_DEFAULT"
FormatHelpLine disable 0
FormatHelpLine 'colour|color' preset 'The dominant image colour.'
FormatHelpLine example '--colour green'
FormatHelpLine 'presets:'
FormatHelpLine preset any
FormatHelpLine preset 'full (colour images only)'
FormatHelpLine preset black-white
FormatHelpLine preset bw
FormatHelpLine preset transparent
FormatHelpLine preset clear
FormatHelpLine preset red
FormatHelpLine preset orange
FormatHelpLine preset yellow
FormatHelpLine preset green
FormatHelpLine preset teal
FormatHelpLine preset cyan
FormatHelpLine preset blue
FormatHelpLine preset purple
FormatHelpLine preset magenta
FormatHelpLine preset pink
FormatHelpLine preset white
FormatHelpLine preset gray
FormatHelpLine preset grey
FormatHelpLine preset black
FormatHelpLine preset brown
FormatHelpLine d debug option "Save the runtime debug log [$DEBUG_FILE] into output directory."
FormatHelpLine E exact-search option 'Perform an exact-phrase search only. Disregard Google suggestions and loose matches.'
FormatHelpLine default 'loose search'
FormatHelpLine exclude-links file "The URLs for images successfully downloaded will be appended to this file (if specified). Specify this file again to ensure these URLs are not reused."
FormatHelpLine exclude-words string 'A comma-separated list (without spaces) of words that you want to exclude from the search.'
FormatHelpLine format preset 'Only download images encoded in this file format.'
FormatHelpLine example '--format svg'
FormatHelpLine 'presets:'
FormatHelpLine preset jpg
FormatHelpLine preset png
FormatHelpLine preset gif
FormatHelpLine preset bmp
FormatHelpLine preset svg
FormatHelpLine preset webp
FormatHelpLine preset ico
FormatHelpLine preset craw
FormatHelpLine G gallery option 'Download images, then create a thumbnail gallery.'
FormatHelpLine gallery= preset 'As above, and apply one of the following modifiers:'
FormatHelpLine '--gallery=background-trans (use a transparent background in the gallery image)'
FormatHelpLine '--gallery=compact (create a condensed thumbnail gallery - no tile-padding between thumbnails)'
FormatHelpLine '--gallery=delete-after (remove all downloaded images after building thumbnail gallery)'
FormatHelpLine h help option 'Display this help.'
FormatHelpLine input-links file 'Download each URL as listed in this text-file, one URL per line. A Google search will not be performed.'
FormatHelpLine i input-phrases file 'A text file containing a list of phrases to download, one phrase per line.'
FormatHelpLine l lower-size integer 'Only download images that are larger than this many bytes.'
FormatHelpLine default "$LOWER_SIZE_BYTES_DEFAULT"
FormatHelpLine L links-only option "Compile a list of image URLs, but don't download any images."
FormatHelpLine m minimum-pixels preset 'Images must contain at least this many pixels.'
FormatHelpLine example '-m 8mp'
FormatHelpLine 'presets:'
FormatHelpLine preset icon
FormatHelpLine preset medium
FormatHelpLine preset large
FormatHelpLine preset 'qsvga (400 x 300)'
FormatHelpLine preset 'vga (640 x 480)'
FormatHelpLine preset 'svga (800 x 600)'
FormatHelpLine preset 'xga (1024 x 768)'
FormatHelpLine preset '2mp (1600 x 1200)'
FormatHelpLine preset '4mp (2272 x 1704)'
FormatHelpLine preset '6mp (2816 x 2112)'
FormatHelpLine preset '8mp (3264 x 2448)'
FormatHelpLine preset '10mp (3648 x 2736)'
FormatHelpLine preset '12mp (4096 x 3072)'
FormatHelpLine preset '15mp (4480 x 3360)'
FormatHelpLine preset '20mp (5120 x 3840)'
FormatHelpLine preset '40mp (7216 x 5412)'
FormatHelpLine preset '70mp (9600 x 7200)'
FormatHelpLine n number integer 'Number of images to download.'
FormatHelpLine default "$IMAGES_REQUESTED_DEFAULT"
FormatHelpLine maximum "$GOOGLE_RESULTS_MAX"
FormatHelpLine no-colour option 'Runtime display will be in boring, uncoloured text. :('
FormatHelpLine safesearch-off option "Disable Google's SafeSearch content-filtering."
FormatHelpLine default on
FormatHelpLine o output path "The image output directory. Enclose whitespace in quotes."
FormatHelpLine default phrase
FormatHelpLine P parallel integer 'How many parallel image downloads?'
FormatHelpLine default "$PARALLEL_LIMIT_DEFAULT"
FormatHelpLine maximum "$PARALLEL_MAX"
FormatHelpLine q quiet option 'Suppress stdout. stderr is still shown.'
FormatHelpLine random option 'Download a single, random image.'
FormatHelpLine R recent preset 'Only get images published this far back in time.'
FormatHelpLine example '--recent month'
FormatHelpLine 'presets:'
FormatHelpLine preset any
FormatHelpLine preset hour
FormatHelpLine preset day
FormatHelpLine preset week
FormatHelpLine preset month
FormatHelpLine preset year
FormatHelpLine reindex-rename option 'Reindex and rename downloaded image files into a contiguous block.'
FormatHelpLine r retries integer 'Retry each image download this many times.'
FormatHelpLine default "$RETRIES_DEFAULT"
FormatHelpLine maximum "$RETRIES_MAX"
FormatHelpLine s save-links option "Save image URL list to file [$image_links_file] into the output directory."
FormatHelpLine sites string 'A comma separated list (without spaces) of sites or domains from which you want to search the images.'
FormatHelpLine S skip-no-size option "Don't download any image if its size cannot be determined before fetching from server."
FormatHelpLine thumbnails string 'Ensure each gallery thumbnail is not larger than: width x height.'
FormatHelpLine example '--thumbnails 200x100'
FormatHelpLine default "$GALLERY_THUMBNAIL_DIMENSIONS_DEFAULT"
FormatHelpLine t timeout integer 'Number of seconds before aborting each image download.'
FormatHelpLine default "$TIMEOUT_SECONDS_DEFAULT"
FormatHelpLine maximum "$TIMEOUT_SECONDS_MAX"
FormatHelpLine T title string 'Title for thumbnail gallery image. Enclose whitespace in quotes.'
FormatHelpLine default phrase
FormatHelpLine disable none
FormatHelpLine type preset 'Image category type.'
FormatHelpLine example '--type clipart'
FormatHelpLine 'presets:'
FormatHelpLine preset face
FormatHelpLine preset photo
FormatHelpLine preset clipart
FormatHelpLine preset lineart
FormatHelpLine preset animated
FormatHelpLine u upper-size integer 'Only download images that are smaller than this many bytes.'
FormatHelpLine default "$UPPER_SIZE_BYTES_DEFAULT"
FormatHelpLine unlimited 0
FormatHelpLine usage-rights preset 'Original image usage-rights.'
FormatHelpLine example '--usage-rights reuse'
FormatHelpLine 'presets:'
FormatHelpLine preset reuse
FormatHelpLine preset reuse-with-mod
FormatHelpLine preset noncomm-reuse
FormatHelpLine preset noncomm-reuse-with-mod
FormatHelpLine z lightning option "Download images even faster by using an optimised set of parameters. For when you really can't wait!"
echo
echo " example:"
echo
ColourTextBold " $ $LAUNCHER -p '$SAMPLE_USER_PHRASE'"; echo
echo
echo " This will download the first $IMAGES_REQUESTED_DEFAULT images available for the phrase '$SAMPLE_USER_PHRASE'."
}
ValidateScriptParameters()
{
# $? = 0 if OK, 1 if not
local OK=false
if [[ $links_only = true ]]; then
gallery=false
save_links=true
fi
if [[ $gallery_compact_thumbs = true || $gallery_background_trans = true || $gallery_delete_images = true ]]; then
gallery=true
fi
if [[ $lightning_mode = true ]]; then
# Yeah!
timeout_seconds=1
retries=0
skip_no_size=true
parallel_limit=$PARALLEL_MAX
links_only=false
gallery=false
fi
while true; do
if [[ -n $input_links_pathfile && $links_only = true && $save_links = true ]]; then
echo " Let's review. Your chosen options will:"
echo " 1. use an input file with a list of URL links,"
echo " 2. don't download any images,"
echo " 3. save the URL links list to file."
echo " So... I've nothing to do. Might be time to (R)ead-(T)he-(M)anual. ;)"
break
fi
case ${user_images_requested#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $user_images_requested is invalid'
ShowFailInvalidInteger '-n, --number'
break
;;
*)
if [[ $user_images_requested -lt 1 ]]; then
user_images_requested=1
DebugFuncVarAdjust '$user_images_requested TOO LOW so set to a sensible minimum' "$user_images_requested"
fi
if [[ $user_images_requested -gt $GOOGLE_RESULTS_MAX ]]; then
user_images_requested=$GOOGLE_RESULTS_MAX
DebugFuncVarAdjust '$user_images_requested TOO HIGH so set as $GOOGLE_RESULTS_MAX' "$user_images_requested"
fi
;;
esac
if [[ $random_image = true ]]; then
gallery_images_required=1
else
gallery_images_required=$user_images_requested
fi
if [[ -n $input_links_pathfile ]]; then
if [[ ! -e $input_links_pathfile ]]; then
DebugScriptFail '$input_links_pathfile was not found'
ShowFailMissingFile '--input-links'
break
fi
fi
if [[ -n $input_phrases_pathfile ]]; then
if [[ ! -e $input_phrases_pathfile ]]; then
DebugScriptFail '$input_phrases_pathfile was not found'
ShowFailMissingFile '-i, --input-phrases'
break
fi
fi
if [[ -n $exclude_links_pathfile ]]; then
[[ ! -e $exclude_links_pathfile ]] && touch "$exclude_links_pathfile"
fi
case ${parallel_limit#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $parallel_limit is invalid'
ShowFailInvalidInteger '-P, --parallel'
break
;;
*)
if [[ $parallel_limit -lt 1 ]]; then
parallel_limit=$PARALLEL_MAX
DebugFuncVarAdjust '$parallel_limit SET TO MAX' "$parallel_limit"
fi
if [[ $parallel_limit -gt $PARALLEL_MAX ]]; then
parallel_limit=$PARALLEL_MAX
DebugFuncVarAdjust '$parallel_limit TOO HIGH so set as' "$parallel_limit"
fi
;;
esac
case ${timeout_seconds#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $timeout_seconds is invalid'
ShowFailInvalidInteger '-t, --timeout'
break
;;
*)
if [[ $timeout_seconds -lt 1 ]]; then
timeout_seconds=1
DebugFuncVarAdjust '$timeout_seconds TOO LOW so set as' "$timeout_seconds"
fi
if [[ $timeout_seconds -gt $TIMEOUT_SECONDS_MAX ]]; then
timeout_seconds=$TIMEOUT_SECONDS_MAX
DebugFuncVarAdjust '$timeout_seconds TOO HIGH so set as' "$timeout_seconds"
fi
;;
esac
case ${retries#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $retries is invalid'
ShowFailInvalidInteger '-r, --retries'
break
;;
*)
if [[ $retries -lt 0 ]]; then
retries=0
DebugFuncVarAdjust '$retries TOO LOW so set as' "$retries"
fi
if [[ $retries -gt $RETRIES_MAX ]]; then
retries=$RETRIES_MAX
DebugFuncVarAdjust '$retries TOO HIGH so set as' "$retries"
fi
;;
esac
case ${upper_size_bytes#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $upper_size_bytes is invalid'
ShowFailInvalidInteger '-u, --upper-size'
break
;;
*)
if [[ $upper_size_bytes -lt 0 ]]; then
upper_size_bytes=0
DebugFuncVarAdjust '$upper_size_bytes TOO LOW so set as' "$upper_size_bytes (unlimited)"
fi
;;
esac
case ${lower_size_bytes#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $lower_size_bytes is invalid'
ShowFailInvalidInteger '-l, --lower-size'
break
;;
*)
if [[ $lower_size_bytes -lt 0 ]]; then
lower_size_bytes=0
DebugFuncVarAdjust '$lower_size_bytes TOO LOW so set as' "$lower_size_bytes"
fi
if [[ $upper_size_bytes -gt 0 && $lower_size_bytes -gt $upper_size_bytes ]]; then
lower_size_bytes=$((upper_size_bytes-1))
DebugFuncVarAdjust "\$lower_size_bytes larger than \$upper_size_bytes ($upper_size_bytes) so set as" "$lower_size_bytes"
fi
;;
esac
case ${gallery_border_pixels#[-+]} in
*[!0-9]*)
DebugScriptFail 'specified $gallery_border_pixels is invalid'
ShowFailInvalidInteger '-b, --border-pixels'
break
;;
*)
if [[ $gallery_border_pixels -lt 0 ]]; then
gallery_border_pixels=0
DebugFuncVarAdjust '$gallery_border_pixels TOO LOW so set as' "$gallery_border_pixels"
fi
;;
esac
OK=true
break
done
if [[ $OK = false ]]; then
errorcode=2
return 1
fi
return 0
}
ValidateGoogleParameters()
{
# all elements of Google's URL syntax should be validated and calculated here, except for 'start page' and 'result index'. These will be added later.
# $? = 0 if OK, 1 if not
compiled_query_parameters='' # query string without 'start page' and 'result index'
local -r SERVER='https://www.google.com'
local -r SAFE_SEARCH_QUERY="&q=$safe_search_phrase"
local -r SEARCH_TYPE='&tbm=isch' # search for images
local -r SEARCH_LANGUAGE='&hl=en' # language
local -r SEARCH_STYLE='&site=imghp' # result layout style
local -r SEARCH_SIMILAR='&filter=0' # don't omit similar results
local aspect_ratio_type=''
local aspect_ratio_search=''
local image_colour_type=''
local image_colour_search=''
local image_type_search=''
local image_format_search=''
local min_pixels_type=''
local min_pixels_search=''
local recent_type=''
local recent_search=''
local usage_rights_type=''
local usage_rights_search=''
local search_match_type='&nfpr=' # exact or loose (suggested) search
local OK=false
if [[ $exact_search = true ]]; then
search_match_type+=1
else
search_match_type+=0
fi
local safesearch_flag='&safe=' # Google's SafeSearch content filter
if [[ $safesearch_on = true ]]; then
safesearch_flag+=active
else
safesearch_flag+=inactive
fi
while true; do
if [[ -n $min_pixels ]]; then
case "$min_pixels" in
qsvga|vga|svga|xga|2mp|4mp|6mp|8mp|10mp|12mp|15mp|20mp|40mp|70mp)
min_pixels_type=lt,islt:$min_pixels
;;
large)
min_pixels_type=l
;;
medium)
min_pixels_type=m
;;
icon)
min_pixels_type=i
;;
*)
DebugScriptFail 'specified $min_pixels is invalid'
ShowFailInvalidPreset '-m, --minimum-pixels'
break
;;
esac
[[ -n $min_pixels_type ]] && min_pixels_search=isz:$min_pixels_type
fi
if [[ -n $aspect_ratio ]]; then
case "$aspect_ratio" in
tall)
aspect_ratio_type=t
;;
square)
aspect_ratio_type=s
;;
wide)
aspect_ratio_type=w
;;
panoramic)
aspect_ratio_type=xw
;;
*)
DebugScriptFail 'specified $aspect_ratio is invalid'
ShowFailInvalidPreset '-a, --aspect-ratio'
break
;;
esac
[[ -n $aspect_ratio_type ]] && aspect_ratio_search=iar:$aspect_ratio_type
fi
if [[ -n $image_type ]]; then
case "$image_type" in
face|photo|clipart|lineart|animated)
image_type_search=itp:$image_type
;;
*)
DebugScriptFail 'specified $image_type is invalid'
ShowFailInvalidPreset '--type'
break
;;
esac
fi
if [[ -n $image_format ]]; then
case "$image_format" in
png|jpg|gif|bmp|svg|ico|webp|craw)
image_format_search=ift:$image_format
;;
*)
DebugScriptFail 'specified $image_format is invalid'
ShowFailInvalidPreset '--format'
break
;;
esac
fi
if [[ -n $usage_rights ]]; then
case "$usage_rights" in
reuse-with-mod)
usage_rights_type=fmc