Skip to content

Instantly share code, notes, and snippets.

@roktas
Created May 23, 2010 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save roktas/410669 to your computer and use it in GitHub Desktop.
Save roktas/410669 to your computer and use it in GitHub Desktop.
#!/bin/sh -e
# Recai Oktaş <roktas@debian.org>
# This file is in public domain.
PROGRAM=${0##*/}
BASEURI='labs.google.com'
THESMALL="Small+Set"
THELARGE="Large+Set"
# Portable which(1).
pathfind () {
ifs_save="$IFS"
IFS=:
for _p in $PATH; do
if [ -x "$_p/$*" ] && [ -f "$_p/$*" ]; then
IFS="$OLDIFS"
return 0
fi
done
IFS="$ifs_save"
return 1
}
dump () {
for p in wget lynx w3m curl links w3c; do
if pathfind $p; then
dumper=$p
break
fi
done
# Setup proper options.
case "$dumper" in
wget) opt="-O-" ;;
lynx) opt="-source" ;;
w3m) opt="-dump_source" ;;
curl) opt="" ;;
links) opt="-source" ;;
w3c) opt="-n -get" ;;
"") printf "Couldn't locate a program to fetch files from net " >&2
printf "(e.g. wget, w3m, lynx, w3c, or curl).\n" >&2
exit 1 ;;
esac
$dumper $opt "$@"
}
for term; do
i=$(($i + 1))
term=$(echo $term | tr [[:blank:]] '+')
query_string="${query_string}q${i}=${term}&"
done
size="$THESMALL"
case "$PROGRAM" in
gibigibi) size="$THELARGE" ;;
esac
QUERY="http://${BASEURI}/sets?${query_string}btn=${size}"
echo "Querying for the terms: $@..." >&2
echo >&2
dump "$QUERY" 2>/dev/null |
awk '
/<tr>.*<a href=.+\.google\.com\/search.+<\/a>/ {
term = $0
sub(/<\/a>.+$/, "", term)
sub(/^.+>/, "", term)
re = "/q=" term "/"
if (re) print term
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment