search.dcgi - gophercgis - Collection of gopher CGI/DCGI for geomyidae
 (HTM) hg clone https://bitbucket.org/iamleot/gophercgis
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       search.dcgi
       ---
            1 #!/bin/sh
            2 
            3 . ../common/config.sh
            4 
            5 SEARCH_CGI="${CGI_BASEDIR}/duckduckgo/search.dcgi"
            6 
            7 url="https://duckduckgo.com/lite/"
            8 
            9 if [ -n "$1" ]; then
           10         query=$1
           11         i=0
           12 elif [ -n "$2" ]; then
           13         query=${2%/*}
           14         i=${2##*/}
           15 else
           16         exit 1
           17 fi
           18 
           19 echo "t"
           20 echo "tDuckDuckGo"
           21 echo "t"
           22 
           23 curl -A '' -Lgs -d "q=${query}" -d "s=${i}" \
           24     -d "nextParams=" -d "v=l" -d "o=json" -d "api=/d.js" -- "${url}" |
           25 awk -v query="${query}" -v n="${i}" -v cgi="${SEARCH_CGI}" \
           26 '
           27 function gphize(s)
           28 {
           29         gsub("\\|", "\\|", s)
           30         return s
           31 }
           32 
           33 function html_decode(s)
           34 {
           35         gsub(/'/, "\047", s);
           36         gsub(/>/, ">", s);
           37         gsub(/&lt;/, "<", s);
           38         gsub(/&quot;/, "\"", s);
           39         gsub(/&amp;/, "\\&", s);
           40         return s
           41 }
           42 
           43 function html_to_text(s)
           44 {
           45         gsub(/<[^>]+>/, "", s)
           46         return html_decode(s)
           47 }
           48 
           49 /class=.result-link./ {
           50         if (match($0, /href="[^"]+"/)) {
           51                 url = substr($0, RSTART + 6, RLENGTH - 7)
           52         }
           53 
           54         if (match($0, /class=.result-link.>.*<\/a>$/)) {
           55                 title = substr($0, RSTART + 20, RLENGTH - 24)
           56                 title = html_to_text(title)
           57         }
           58 
           59         if (url && title) {
           60                 printf("[h|%s|URL:%s|server|port]\n", gphize(title), url)
           61         }
           62 }
           63 
           64 END {
           65         print "t"
           66 
           67         if (n >= 30) {
           68                 printf("[1|<< Previous|%s?%s/%d|server|port]\n",
           69                         cgi, query, n - 30)
           70         }
           71 
           72         if (n >= 0) {
           73                 printf("[1|>> Next|%s?%s/%d|server|port]\n",
           74                         cgi, query, n + 30)
           75         }
           76 }
           77 '