packages feed

sindre-0.1: examples/suggest

#!/bin/sh
# Interface to Google Suggest - not very useful, but a nice demo of
# how to combine Sindre with other programs through shell script.

code=$(cat <<EOF
stdin->lines(lines) { if (clear) { list.clear(); clear = 0; } }
input->changed(from, to) { clear=1; print to; next; }
function quit() { print; exit }
EOF
)
dir=$(mktemp -p "${TMPDIR:-/tmp}" -d dir-XXXX) || exit 1
fifo=$dir/fifo
mkfifo "$fifo" || { rmdir "$dir"; exit 1; }

googlesuggest() {
    python - "$@" <<EOF
import sys, string
import urllib

URI = "http://www.google.com/complete/search?hl=en&js=true&qu="

def suggest(term):
    # Pull the results as Javascript, then munge them a bit and
    # evaluate them as Python, yielding a dictionary.
    text = urllib.urlopen(URI + urllib.quote(term)).read() \
           .replace("window.google.ac.h(","").replace(",{}])", "]")
    for res in (eval(text)[1]):
        print res[0]

suggest(string.join(sys.argv[1:], ' '))
EOF
}

cat $fifo | $(dirname "$0")/dmenu -l 10 -e "$code" -p Search |\
while read line; do
    echo $line 1>&3
        # Due to Python nonsense, we have to reencode the output.
    googlesuggest "$line" | iconv -f latin1 -t utf8 &
done 3>&1 1>$fifo | tail -n 1 | sed '/^$/d'
rm $fifo
rmdir $dir