Debian Codegraph @

wgetter2.bash

raw symbols SCIP
1 #!/bin/bash
2 # wgetter2.bash
3
4 # Author: Little Monster [[email protected]]
5 # ==> Used in ABS Guide with permission of script author.
6 # ==> This script still needs debugging and fixups (exercise for reader).
7 # ==> It could also use some additional editing in the comments.
8
9
10 # This is wgetter2 --
11 #+ a Bash script to make wget a bit more friendly, and save typing.
12
13 # Carefully crafted by Little Monster.
14 # More or less complete on 02/02/2005.
15 # If you think this script can be improved,
16 #+ email me at: [email protected]
17 # ==> and cc: to the author of the ABS Guide, please.
18 # This script is licenced under the GPL.
19 # You are free to copy, alter and re-use it,
20 #+ but please don't try to claim you wrote it.
21 # Log your changes here instead.
22
23 # =======================================================================
24 # changelog:
25
26 # 07/02/2005. Fixups by Little Monster.
27 # 02/02/2005. Minor additions by Little Monster.
28 # (See after # +++++++++++ )
29 # 29/01/2005. Minor stylistic edits and cleanups by author of ABS Guide.
30 # Added exit error codes.
31 # 22/11/2004. Finished initial version of second version of wgetter:
32 # wgetter2 is born.
33 # 01/12/2004. Changed 'runn' function so it can be run 2 ways --
34 # either ask for a file name or have one input on the CL.
35 # 01/12/2004. Made sensible handling of no URL's given.
36 # 01/12/2004. Made loop of main options, so you don't
37 # have to keep calling wgetter 2 all the time.
38 # Runs as a session instead.
39 # 01/12/2004. Added looping to 'runn' function.
40 # Simplified and improved.
41 # 01/12/2004. Added state to recursion setting.
42 # Enables re-use of previous value.
43 # 05/12/2004. Modified the file detection routine in the 'runn' function
44 # so it's not fooled by empty values, and is cleaner.
45 # 01/02/2004. Added cookie finding routine from later version (which
46 # isn't ready yet), so as not to have hard-coded paths.
47 # =======================================================================
48
49 # Error codes for abnormal exit.
50 E_USAGE=67 # Usage message, then quit.
51 E_NO_OPTS=68 # No command-line args entered.
52 E_NO_URLS=69 # No URLs passed to script.
53 E_NO_SAVEFILE=70 # No save filename passed to script.
54 E_USER_EXIT=71 # User decides to quit.
55
56
57 # Basic default wget command we want to use.
58 # This is the place to change it, if required.
59 # NB: if using a proxy, set http_proxy = yourproxy in .wgetrc.
60 # Otherwise delete --proxy=on, below.
61 # ====================================================================
62 CommandA="wget -nc -c -t 5 --progress=bar --random-wait --proxy=on -r"
63 # ====================================================================
64
65
66
67 # --------------------------------------------------------------------
68 # Set some other variables and explain them.
69
70 pattern=" -A .jpg,.JPG,.jpeg,.JPEG,.gif,.GIF,.htm,.html,.shtml,.php"
71 # wget's option to only get certain types of file.
72 # comment out if not using
73 today=`date +%F` # Used for a filename.
74 home=$HOME # Set HOME to an internal variable.
75 # In case some other path is used, change it here.
76 depthDefault=3 # Set a sensible default recursion.
77 Depth=$depthDefault # Otherwise user feedback doesn't tie in properly.
78 RefA="" # Set blank referring page.
79 Flag="" # Default to not saving anything,
80 #+ or whatever else might be wanted in future.
81 lister="" # Used for passing a list of urls directly to wget.
82 Woptions="" # Used for passing wget some options for itself.
83 inFile="" # Used for the run function.
84 newFile="" # Used for the run function.
85 savePath="$home/w-save"
86 Config="$home/.wgetter2rc"
87 # This is where some variables can be stored,
88 #+ if permanently changed from within the script.
89 Cookie_List="$home/.cookielist"
90 # So we know where the cookies are kept . . .
91 cFlag="" # Part of the cookie file selection routine.
92
93 # Define the options available. Easy to change letters here if needed.
94 # These are the optional options; you don't just wait to be asked.
95
96 save=s # Save command instead of executing it.
97 cook=c # Change cookie file for this session.
98 help=h # Usage guide.
99 list=l # Pass wget the -i option and URL list.
100 runn=r # Run saved commands as an argument to the option.
101 inpu=i # Run saved commands interactively.
102 wopt=w # Allow to enter options to pass directly to wget.
103 # --------------------------------------------------------------------
104
105
106 if [ -z "$1" ]; then # Make sure we get something for wget to eat.
107 echo "You must at least enter a URL or option!"
108 echo "-$help for usage."
109 exit $E_NO_OPTS
110 fi
111
112
113
114 # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115 # added added added added added added added added added added added added
116
117 if [ ! -e "$Config" ]; then # See if configuration file exists.
118 echo "Creating configuration file, $Config"
119 echo "# This is the configuration file for wgetter2" > "$Config"
120 echo "# Your customised settings will be saved in this file" >> "$Config"
121 else
122 source $Config # Import variables we set outside the script.
123 fi
124
125 if [ ! -e "$Cookie_List" ]; then
126 # Set up a list of cookie files, if there isn't one.
127 echo "Hunting for cookies . . ."
128 find -name cookies.txt >> $Cookie_List # Create the list of cookie files.
129 fi # Isolate this in its own 'if' statement,
130 #+ in case we got interrupted while searching.
131
132 if [ -z "$cFlag" ]; then # If we haven't already done this . . .
133 echo # Make a nice space after the command prompt.
134 echo "Looks like you haven't set up your source of cookies yet."
135 n=0 # Make sure the counter
136 #+ doesn't contain random values.
137 while read; do
138 Cookies[$n]=$REPLY # Put the cookie files we found into an array.
139 echo "$n) ${Cookies[$n]}" # Create a menu.
140 n=$(( n + 1 )) # Increment the counter.
141 done < $Cookie_List # Feed the read statement.
142 echo "Enter the number of the cookie file you want to use."
143 echo "If you won't be using cookies, just press RETURN."
144 echo
145 echo "I won't be asking this again. Edit $Config"
146 echo "If you decide to change at a later date"
147 echo "or use the -${cook} option for per session changes."
148 read
149 if [ ! -z $REPLY ]; then # User didn't just press return.
150 Cookie=" --load-cookies ${Cookies[$REPLY]}"
151 # Set the variable here as well as in the config file.
152
153 echo "Cookie=\" --load-cookies ${Cookies[$REPLY]}\"" >> $Config
154 fi
155 echo "cFlag=1" >> $Config # So we know not to ask again.
156 fi
157
158 # end added section end added section end added section end added section
159 # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
160
161
162
163 # Another variable.
164 # This one may or may not be subject to variation.
165 # A bit like the small print.
166 CookiesON=$Cookie
167 # echo "cookie file is $CookiesON" # For debugging.
168 # echo "home is ${home}" # For debugging.
169 # Got caught with this one!
170
171
172 wopts()
173 {
174 echo "Enter options to pass to wget."
175 echo "It is assumed you know what you're doing."
176 echo
177 echo "You can pass their arguments here too."
178 # That is to say, everything passed here is passed to wget.
179
180 read Wopts
181 # Read in the options to be passed to wget.
182
183 Woptions=" $Wopts"
184 # ^ Why the leading space?
185 # Assign to another variable.
186 # Just for fun, or something . . .
187
188 echo "passing options ${Wopts} to wget"
189 # Mainly for debugging.
190 # Is cute.
191
192 return
193 }
194
195
196 save_func()
197 {
198 echo "Settings will be saved."
199 if [ ! -d $savePath ]; then # See if directory exists.
200 mkdir $savePath # Create the directory to save things in
201 #+ if it isn't already there.
202 fi
203
204 Flag=S
205 # Tell the final bit of code what to do.
206 # Set a flag since stuff is done in main.
207
208 return
209 }
210
211
212 usage() # Tell them how it works.
213 {
214 echo "Welcome to wgetter. This is a front end to wget."
215 echo "It will always run wget with these options:"
216 echo "$CommandA"
217 echo "and the pattern to match: $pattern \
218 (which you can change at the top of this script)."
219 echo "It will also ask you for recursion depth, \
220 and if you want to use a referring page."
221 echo "Wgetter accepts the following options:"
222 echo ""
223 echo "-$help : Display this help."
224 echo "-$save : Save the command to a file $savePath/wget-($today) \
225 instead of running it."
226 echo "-$runn : Run saved wget commands instead of starting a new one -"
227 echo "Enter filename as argument to this option."
228 echo "-$inpu : Run saved wget commands interactively --"
229 echo "The script will ask you for the filename."
230 echo "-$cook : Change the cookies file for this session."
231 echo "-$list : Tell wget to use URL's from a list instead of \
232 from the command-line."
233 echo "-$wopt : Pass any other options direct to wget."
234 echo ""
235 echo "See the wget man page for additional options \
236 you can pass to wget."
237 echo ""
238
239 exit $E_USAGE # End here. Don't process anything else.
240 }
241
242
243
244 list_func() # Gives the user the option to use the -i option to wget,
245 #+ and a list of URLs.
246 {
247 while [ 1 ]; do
248 echo "Enter the name of the file containing URL's (press q to change
249 your mind)."
250 read urlfile
251 if [ ! -e "$urlfile" ] && [ "$urlfile" != q ]; then
252 # Look for a file, or the quit option.
253 echo "That file does not exist!"
254 elif [ "$urlfile" = q ]; then # Check quit option.
255 echo "Not using a url list."
256 return
257 else
258 echo "using $urlfile."
259 echo "If you gave url's on the command-line, I'll use those first."
260 # Report wget standard behaviour to the user.
261 lister=" -i $urlfile" # This is what we want to pass to wget.
262 return
263 fi
264 done
265 }
266
267
268 cookie_func() # Give the user the option to use a different cookie file.
269 {
270 while [ 1 ]; do
271 echo "Change the cookies file. Press return if you don't want to change
272 it."
273 read Cookies
274 # NB: this is not the same as Cookie, earlier.
275 # There is an 's' on the end.
276 # Bit like chocolate chips.
277 if [ -z "$Cookies" ]; then # Escape clause for wusses.
278 return
279 elif [ ! -e "$Cookies" ]; then
280 echo "File does not exist. Try again." # Keep em going . . .
281 else
282 CookiesON=" --load-cookies $Cookies" # File is good -- use it!
283 return
284 fi
285 done
286 }
287
288
289
290 run_func()
291 {
292 if [ -z "$OPTARG" ]; then
293 # Test to see if we used the in-line option or the query one.
294 if [ ! -d "$savePath" ]; then # If directory doesn't exist . . .
295 echo "$savePath does not appear to exist."
296 echo "Please supply path and filename of saved wget commands:"
297 read newFile
298 until [ -f "$newFile" ]; do # Keep going till we get something.
299 echo "Sorry, that file does not exist. Please try again."
300 # Try really hard to get something.
301 read newFile
302 done
303
304
305 # -----------------------------------------------------------------------
306 # if [ -z ( grep wget ${newfile} ) ]; then
307 # Assume they haven't got the right file and bail out.
308 # echo "Sorry, that file does not contain wget commands. Aborting."
309 # exit
310 # fi
311 #
312 # This is bogus code.
313 # It doesn't actually work.
314 # If anyone wants to fix it, feel free!
315 # -----------------------------------------------------------------------
316
317
318 filePath="${newFile}"
319 else
320 echo "Save path is $savePath"
321 echo "Please enter name of the file which you want to use."
322 echo "You have a choice of:"
323 ls $savePath # Give them a choice.
324 read inFile
325 until [ -f "$savePath/$inFile" ]; do # Keep going till
326 #+ we get something.
327 if [ ! -f "${savePath}/${inFile}" ]; then # If file doesn't exist.
328 echo "Sorry, that file does not exist. Please choose from:"
329 ls $savePath # If a mistake is made.
330 read inFile
331 fi
332 done
333 filePath="${savePath}/${inFile}" # Make one variable . . .
334 fi
335 else filePath="${savePath}/${OPTARG}" # Which can be many things . . .
336 fi
337
338 if [ ! -f "$filePath" ]; then # If a bogus file got through.
339 echo "You did not specify a suitable file."
340 echo "Run this script with the -${save} option first."
341 echo "Aborting."
342 exit $E_NO_SAVEFILE
343 fi
344 echo "Using: $filePath"
345 while read; do
346 eval $REPLY
347 echo "Completed: $REPLY"
348 done < $filePath # Feed the actual file we are using into a 'while' loop.
349
350 exit
351 }
352
353
354
355 # Fish out any options we are using for the script.
356 # This is based on the demo in "Learning The Bash Shell" (O'Reilly).
357 while getopts ":$save$cook$help$list$runn:$inpu$wopt" opt
358 do
359 case $opt in
360 $save) save_func;; # Save some wgetter sessions for later.
361 $cook) cookie_func;; # Change cookie file.
362 $help) usage;; # Get help.
363 $list) list_func;; # Allow wget to use a list of URLs.
364 $runn) run_func;; # Useful if you are calling wgetter from,
365 #+ for example, a cron script.
366 $inpu) run_func;; # When you don't know what your files are named.
367 $wopt) wopts;; # Pass options directly to wget.
368 \?) echo "Not a valid option."
369 echo "Use -${wopt} to pass options directly to wget,"
370 echo "or -${help} for help";; # Catch anything else.
371 esac
372 done
373 shift $((OPTIND - 1)) # Do funky magic stuff with $#.
374
375
376 if [ -z "$1" ] && [ -z "$lister" ]; then
377 # We should be left with at least one URL
378 #+ on the command-line, unless a list is
379 #+ being used -- catch empty CL's.
380 echo "No URL's given! You must enter them on the same line as wgetter2."
381 echo "E.g., wgetter2 http://somesite http://anothersite."
382 echo "Use $help option for more information."
383 exit $E_NO_URLS # Bail out, with appropriate error code.
384 fi
385
386 URLS=" $@"
387 # Use this so that URL list can be changed if we stay in the option loop.
388
389 while [ 1 ]; do
390 # This is where we ask for the most used options.
391 # (Mostly unchanged from version 1 of wgetter)
392 if [ -z $curDepth ]; then
393 Current=""
394 else Current=" Current value is $curDepth"
395 fi
396 echo "How deep should I go? \
397 (integer: Default is $depthDefault.$Current)"
398 read Depth # Recursion -- how far should we go?
399 inputB="" # Reset this to blank on each pass of the loop.
400 echo "Enter the name of the referring page (default is none)."
401 read inputB # Need this for some sites.
402
403 echo "Do you want to have the output logged to the terminal"
404 echo "(y/n, default is yes)?"
405 read noHide # Otherwise wget will just log it to a file.
406
407 case $noHide in # Now you see me, now you don't.
408 y|Y ) hide="";;
409 n|N ) hide=" -b";;
410 * ) hide="";;
411 esac
412
413 if [ -z ${Depth} ]; then
414 # User accepted either default or current depth,
415 #+ in which case Depth is now empty.
416 if [ -z ${curDepth} ]; then
417 # See if a depth was set on a previous iteration.
418 Depth="$depthDefault"
419 # Set the default recursion depth if nothing
420 #+ else to use.
421 else Depth="$curDepth" # Otherwise, set the one we used before.
422 fi
423 fi
424 Recurse=" -l $Depth" # Set how deep we want to go.
425 curDepth=$Depth # Remember setting for next time.
426
427 if [ ! -z $inputB ]; then
428 RefA=" --referer=$inputB" # Option to use referring page.
429 fi
430
431 WGETTER="${CommandA}${pattern}${hide}${RefA}${Recurse}\
432 ${CookiesON}${lister}${Woptions}${URLS}"
433 # Just string the whole lot together . . .
434 # NB: no embedded spaces.
435 # They are in the individual elements so that if any are empty,
436 #+ we don't get an extra space.
437
438 if [ -z "${CookiesON}" ] && [ "$cFlag" = "1" ] ; then
439 echo "Warning -- can't find cookie file"
440 # This should be changed,
441 #+ in case the user has opted to not use cookies.
442 fi
443
444 if [ "$Flag" = "S" ]; then
445 echo "$WGETTER" >> $savePath/wget-${today}
446 # Create a unique filename for today, or append to it if it exists.
447 echo "$inputB" >> $savePath/site-list-${today}
448 # Make a list, so it's easy to refer back to,
449 #+ since the whole command is a bit confusing to look at.
450 echo "Command saved to the file $savePath/wget-${today}"
451 # Tell the user.
452 echo "Referring page URL saved to the file$ \
453 savePath/site-list-${today}"
454 # Tell the user.
455 Saver=" with save option"
456 # Stick this somewhere, so it appears in the loop if set.
457 else
458 echo "*****************"
459 echo "*****Getting*****"
460 echo "*****************"
461 echo ""
462 echo "$WGETTER"
463 echo ""
464 echo "*****************"
465 eval "$WGETTER"
466 fi
467
468 echo ""
469 echo "Starting over$Saver."
470 echo "If you want to stop, press q."
471 echo "Otherwise, enter some URL's:"
472 # Let them go again. Tell about save option being set.
473
474 read
475 case $REPLY in
476 # Need to change this to a 'trap' clause.
477 q|Q ) exit $E_USER_EXIT;; # Exercise for the reader?
478 * ) URLS=" $REPLY";;
479 esac
480
481 echo ""
482 done
483
484
485 exit 0