给Surfraw添加一个百度引擎

什么是Surfraw

简单的说,Surfraw就是一些流行网页搜索引擎的命令行接口。一个搜索引擎 对应一个脚本文件,脚本里主要用来实现Surfraw里定义的一些功能接口。

详细概念参看官网上的解释: About Surfraw

Surfraw provides a fast unix command line interface to a variety of popular WWW search engines and other artifacts of power. It reclaims google, altavista, babelfish, dejanews, freshmeat, research index, slashdot and many others from the false-prophet, pox-infested heathen lands of html-forms, placing these wonders where they belong, deep in unix heartland, as god loving extensions to the shell.

Surfraw abstracts the browser away from input. Doing so lets it get on with what it's good at. Browsing. Interpretation of linguistic forms is handed back to the shell, which is what it, and human beings are good at. Combined with netscape-remote or incremental text browsers, such as lynx, links or w3m, along with screen a Surfraw liberateur is capable of navigating speeds that leave GUI tainted idolaters agape with fear and wonder.

为什么要搞这个?

  • 原因一:纯属个人喜好;
  • 原因二:在Emacs里按 C-c h s 然后输关键词,回车,选引擎,再回车可 以直接打开搜索结果;
  • 原因三:因为使用了 helm 插件!(关于HELM helm-surfraw
  • 原因四:即便不情愿,村里有时也不得不用百度!必尽要遵守纪律~

安装使用Surfraw

在Ubuntu下安装:

sudo apt-get install surfraw

其它平台的安装参看官网文档:Installation

使用示例:

# 查看搜索引擎列表
surfraw -elvi

# 用sr这个命令也是一样的
sr -elvi

# 命令格式: sr 引擎名称 [选项] 搜索项..

# 示例:在百度图片里搜索汽车的图片
sr baidu -i 汽车

# 查看帮助
sr google -help
sr baidu -help

实现百度的搜索接口脚本

在Ubuntu下,Surfraw的搜索引擎脚本文件都放在 /usr/lib/surfraw/ 目录 下。我是直接把目录下的 google 这个脚本文件复制出来,然后另存为 baidu 然后在此基础上进行修改的。具体步骤如下:

# 1. 复制一个脚本文件,做为模板
sudo cp /usr/lib/surfraw/google /usr/lib/surfraw/baidu
# 2. 修改脚本文件,可以下载下面的脚本
sudo editor /usr/lib/surfraw/baidu
# 3. 修改权限
sudo chmod +x /usr/lib/surfraw/baidu

修改后的 baidu 脚本:

cat /usr/lib/surfraw/baidu
#!/bin/sh
# $Id$
# elvis: baidu          -- Search the web using Baidu (www.baidu.com)
. surfraw || exit 1

w3_config_hook () {
    def   SURFRAW_baidu_results "$SURFRAW_results"
    def   SURFRAW_baidu_search search
    defyn SURFRAW_baidu_lucky 0
    def   SURFRAW_baidu_country zh
    def   SURFRAW_baidu_safe default
}

w3_usage_hook () {
    cat <<EOF
Usage: $w3_argv0 [options] [search words]...
Description:
  Surfraw search the web using Google (www.baidu.com)
Local options:
  -results=NUM                  Number of search results returned
                                Default: $SURFRAW_baidu_results
                                Environment: SURFRAW_baidu_results
  -search=                      Specialized search on topic
          image       | Images
          v           | Videos
          map           |       Maps
          tieba       | Tieba
          news          |       News
          zhidao        |       Zhidao
          music         |       Music
          wenku         |       Wenku
  -i, -images                   Image Search (same as -search=image)
  -v, -videos                   Video Search (same as -search=v)
  -m, -maps                     Map Search (same as -search=map)
  -n, -news                     News Search (same as -search=news)
  -w, -wenku                    Wenku Search (same as -search=wenku)
  -t, -tieba                    Tieba Search (same as -search=tieba)
  -z, -zhidao                   Zhidao Search (same as -search=zhidao)
  -M, -mu, -music               Music Search (same as -search=music)
EOF
    w3_global_usage
}

w3_parse_option_hook () {
    opt="$1"
    optarg="$2"
    case "$opt" in
    -r*=*)      setopt   SURFRAW_baidu_results "$optarg" ;;
    -sa*=*)     setopt   SURFRAW_baidu_safe    "$optarg" ;;
    -s*=*)      setopt   SURFRAW_baidu_search  "$optarg" ;;
    -w*)        setopt   SURFRAW_baidu_search  wenku     ;;
    -tb|-tieba)        setopt   SURFRAW_baidu_search  tieba     ;;
    -z*)        setopt   SURFRAW_baidu_search  zhidao    ;;
    -i*)        setopt   SURFRAW_baidu_search  image     ;;
    -v*)        setopt   SURFRAW_baidu_search  v         ;;
    -m|-map)        setopt   SURFRAW_baidu_search  map       ;;
    -ns|-news)   setopt   SURFRAW_baidu_search  news      ;;
    -M|-mu|-music) setopt   SURFRAW_baidu_search  music  ;;
    *) return 1 ;;
    esac
    return 0
}

w3_config
w3_parse_args "$@"
# w3_args now contains a list of arguments

safe=""
case "$SURFRAW_baidu_safe" in
  strict)   safe="&safe=strict"   ;;
  moderate) safe="&safe=moderate" ;;
  off)      safe="&safe=off"      ;;
esac

url="http://"

if test -z "$w3_args" ; then
    case "$SURFRAW_baidu_search" in
        i*) url="${url}image"  ;;
        m*) url="${url}map"    ;;
        b*) url="${url}baike"  ;;
        n*) url="${url}news"   ;;
        v*) url="${url}v"      ;;
        *)  url="${url}www"    ;;
    esac
    url="${url}.baidu.com"
else
    search="$SURFRAW_baidu_search"
    case "$SURFRAW_baidu_search" in
        t*) url="${url}tieba"; search="f" ;;
        w*) url="${url}wenku"; search="search" ;;
        z*) url="${url}zhidao"; search="search" ;;
        map) url="${url}map"; search="m" ;;
        v*) url="${url}v"; search="v" ;;
        i*) url="${url}image" ; search="i";;
        music) url="${url}music"   ; search="search"    ;;
        n*) url="${url}news"   ; search="ns"    ;;
        *)  url="${url}www"    ; search="s"     ;;
    esac

    url="${url}.baidu.com/${search}"
    escaped_args=`w3_url_of_arg $w3_args`
    url="${url}?&ie=utf-8&word=${escaped_args}&wd=${escaped_args}&rn=${SURFRAW_baidu_results}"
    url="${url}${safe}${extra}"
fi
echo $url
w3_browse_url "$url"