# get urls from duckduckgo, then parse them to wget to download
args=("$@")
-search=${args[0]}
-filetype=${args[1]}
+site=${args[0]}
+extension=${args[1]}
+outdir=${args[2]}
+filetype=""
-set -f
-urls=`curl -silent https://duckduckgo.com/html/\?q\=site:www.mash.dept.shef.ac.uk/Resources |
-grep 'pdf$' | tr -d ' \t\r' | grep -v '^[0-9]' | awk '{print "http://" $0}'`
+if (($# == 0 ))
+ then
+ printf "\n\x1b[31mWrong number of arguments\x1b[0m\n\n"
+ printf "Usage: duckduckget SITE [FILETYPE] [DESTINATION] \n where SITE is the domain to search for files\n [FILETYPE] is extension without preceding dot\n [DESTINATION] is the output directory relative to working directory"
+ exit 1
+fi
+
+if [[ $outdir == "" ]]
+ then
+ outdir="`pwd`/"
+fi
+
+if ! [[ $outdir =~ /$ ]]
+ then
+ outdir=$outdir/
+fi
+
+if [[ $extension != "" ]]
+ then
+ filetype="filetype:$extension"
+fi
+
+urls=`curl -silent https://duckduckgo.com/html/\?q\=site:$site%20$filetype | grep "$extension$" | tr -d ' \t\r' | grep -v '^[0-9]' | awk '{print "http://" $0}'`
for url in $urls; do
- wget --cut-dirs=100 -P /mnt/andrew/literature/mathcentre/ $url
+ wget --cut-dirs=100 -P $outdir $url
done