class BingDictionary::Base

Constants

COLORS

Attributes

doc[RW]
options[RW]
word[RW]

Public Class Methods

new(word, options = {}) click to toggle source
# File lib/bing_dictionary.rb, line 31
def initialize(word, options = {})
  body = self.request(word)
  self.word = word
  self.doc = Nokogiri::HTML(body)
  self.options = options
end
translate(word, options = {}) click to toggle source
# File lib/bing_dictionary.rb, line 12
def self.translate(word, options = {})
  self.new(word, options).translate
rescue SocketError
  warn 'Connection failed! Please check your network.'
  exit 1
end

Public Instance Methods

guess() click to toggle source
# File lib/bing_dictionary.rb, line 76
def guess
  puts
  content = doc.at_css('.content')
  puts content.at_css('.p2-2').text
  puts

  content.css('.dym_area').each do |area|
    puts area.at_css('.df_wb_a').text
    puts area.css('.df_wb_c').map(&:text)
    puts
  end
end
head() click to toggle source
# File lib/bing_dictionary.rb, line 46
def head
  puts doc.at_css('#headword').text
  puts with_color(doc.at_css('.hd_tf_lh').text, :green)
  puts
  doc.at_css('.hd_area').next_sibling.css('li').each do |li|
    puts with_color(with_fixed(li.at_css('.pos').text, 5), :blue) + li.at_css('.def').text
  end
end
machine() click to toggle source
# File lib/bing_dictionary.rb, line 61
def machine
  puts doc.at_css('.smt_hw').text
  puts doc.at_css('.p1-10').text
  puts with_color(doc.at_css('.p1-11').text, :green)
end
pronounce() click to toggle source
# File lib/bing_dictionary.rb, line 55
def pronounce
  url = doc.at_css('.hd_tf_lh .hd_tf a').attr('onclick').match(/http.*mp3/)
  `curl -o /tmp/dict.mp3 #{url} &> /dev/null`
  `afplay /tmp/dict.mp3`
end
request(word) click to toggle source
# File lib/bing_dictionary.rb, line 19
def request(word)
  url = URI("https://cn.bing.com/dict/search?q=#{CGI::escape(word)}")

  https = Net::HTTP.new(url.host, url.port)
  https.use_ssl = true
  request = Net::HTTP::Get.new(url)
  request["Cookie"] = "_EDGE_S=mkt=zh-cn;"

  response = https.request(request)
  return response.read_body
end
sentence() click to toggle source
# File lib/bing_dictionary.rb, line 67
def sentence
  puts
  doc.css('#sentenceSeg .se_li').first(4).map do |li|
    puts with_highlight(li.css('.sen_en').text, word)
    puts li.css('.sen_cn').text
    puts
  end
end
translate() click to toggle source
# File lib/bing_dictionary.rb, line 38
def translate
  head if doc.at_css('#headword')
  machine if doc.at_css('.smt_hw')
  sentence if doc.at_css('#sentenceSeg .se_li')
  guess if doc.at_css('.dym_area')
  pronounce if doc.at_css('#headword') && options[:pronounce]
end
with_color(str, color) click to toggle source
# File lib/bing_dictionary.rb, line 90
def with_color(str, color)
  "\e[3#{COLORS.index(color)}m" << str.to_s << "\e[0m"
end
with_fixed(str, width) click to toggle source
# File lib/bing_dictionary.rb, line 94
def with_fixed(str, width)
  width = width - str.each_char.count { |c| c =~ /\p{Han}/ }
  width > 0 ? ("%-#{width}s" % str) : str
end
with_highlight(str, keyword) click to toggle source
# File lib/bing_dictionary.rb, line 99
def with_highlight(str, keyword)
  str.gsub(/#{keyword}/i) { |s| with_color(s, :yellow) }
end