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}/youtube/search.dcgi"
            6 
            7 if [ -n "$1" ]; then
            8         query=$(printf "%s" "$1" | vis -h)
            9         url="https://www.youtube.com/results?search_query=${query}"
           10 fi
           11 
           12 if [ -n "$2" ]; then
           13         url="https://www.youtube.com$2"
           14 fi
           15 
           16 echo "t"
           17 echo "tYouTube"
           18 echo "t"
           19 
           20 curl -H 'Accept-Language: en-US' -Lgs -- "${url}" |
           21 awk '
           22 /<h3 class="yt-lockup-title ">/ {
           23         if (!match($0, /href="\/watch\?v=[^"]+"/)) {
           24                 next
           25         }
           26 
           27         video_id = substr($0, RSTART + 15, RLENGTH - 16)
           28 
           29         # ignore non-video (i.e. playlist/channel)
           30         if (length(video_id) != 11) {
           31                 next    
           32         }
           33 
           34         match($0, /title="[^"]+"/)
           35         title = substr($0, RSTART + 7, RLENGTH - 8)
           36 
           37         match($0, / - Duration: ([0-9]+:)?[0-9]+:[0-9]+\./)
           38         duration = substr($0, RSTART + 13, RLENGTH - 14)
           39 
           40         match($0, /<a href="\/(channel|user).* >.*<\/a>/)
           41         author = substr($0, RSTART, RLENGTH)
           42         sub(/<a href="\/(channel|user).* >/, "", author)
           43         sub(/<\/a>.*/, "", author)
           44 
           45         desc = title
           46         if (duration) {
           47                 desc = desc "   (" duration ")"
           48         }
           49         desc = desc "   " author
           50         gsub("\\|", "\\|", desc)
           51         gsub("&#39;", "\047", desc)
           52         gsub("&amp;", "\\&", desc)
           53         gsub("&quot;", "\"", desc)
           54 
           55         printf("[h|%s|URL:https://www.youtube.com/embed/%s|server|port]\n",
           56                 desc, video_id)
           57 }
           58 
           59 /« Previous/ {
           60         if (!match($0, /href="\/results\?[^"]+"/)) {
           61                 next
           62         }
           63 
           64         page_prev = substr($0, RSTART + 6, RLENGTH - 7)
           65         sub("%25", "%", page_prev)
           66         sub("&amp;", "\\&", page_prev)
           67         printf("\n")
           68         printf("[1|<< Previous|'"${SEARCH_CGI}"'?%s|server|port]\n", page_prev)
           69 }
           70 
           71 /Next »/ {
           72         if (!match($0, /href="\/results\?[^"]+"/)) {
           73                 next
           74         }
           75 
           76         page_next = substr($0, RSTART + 6, RLENGTH - 7)
           77         sub("%25", "%", page_next)
           78         sub("&amp;", "\\&", page_next)
           79         if (!page_prev) {
           80                 printf("\n")
           81         }
           82         printf("[1|>> Next|'"${SEARCH_CGI}"'?%s|server|port]\n", page_next)
           83 }
           84 '
           85 
           86 echo "t"
           87 echo "[7|Search|${SEARCH_CGI}|server|port]"