命令行查词典

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/env ruby
# encoding: utf-8

require 'net/http'
require 'rexml/document'

include REXML

def translate(word)
  unless word.nil? or word.empty?
    __ "dict translation: #{word}", "="
    parse Net::HTTP.get( "dict.youdao.com", "/fsearch?q=#{word}" )
  end
end

def parse(src)
  xml = Document.new(src)

  xml.each_node('//translation/content') { |node|
    puts "  \e[32m#{node.text}\e[0m"
  }

  __ "web translation", "="

  xml.each_node('//yodao-web-dict/web-translation') { |node|
    __ node.first_node('key/').text
    node.each_node('trans/value/') { |val|
      puts "  #{val.text}"
    }
  }
  
end

def __(t, pad='-', len=30 )
  puts " #{t} ".center(len, pad)
end

# REXML::Element patch
# for better readablitiy
class Element
  def each_node(path, &block); XPath.each(self, path, &block); end
  def first_node(path); XPath.first(self, path); end
end

translate(ARGV[0])