openmind ☃   August 26, 2008  ☃  ddict.rb  (, , )

N.B.: This is currently broken if the notes field contains semi-colon(s).
I have emailed the creator or www-delicious to see if there’s a workaround.

#!/usr/bin/env ruby
%w[rubygems www/delicious hpricot highline/import open-uri cgi].each{|lib| require lib}

# ddict.rb
# programmatic posting of dictionary.com definitions
# to del.icio.us
word  = ARGV.shift
url   = "http://dictionary.reference.com/search?q=" + CGI.escape("#{word}")
doc   = Hpricot(open(url))
title = (doc/"title").inner_text

# TODO:  fix this part -- it sucks!
defs  = (doc/"table.luna-Ent tr td").select{|x| (x.inner_text.length > 3)}


puts defs.inspect
if defs.length == 0
  puts "Not found!"
  exit
end

selected = []
loop do
  defs.each_with_index do |d,idx| 
    puts [(idx + 1).to_s,(selected.include?(idx+1) ? [" +++ ",d.inner_text].join : d.inner_text)].join(") ")
  end
  puts "Select the definitions to include by number.  \"p\" to post; \"q\" to quit."
  (input = ask('') { |q| q.character = true; q.echo = false }) != "\e"
  sinput,iinput = input.to_s.downcase,(input.to_i)
  selected.uniq!
  next if iinput > defs.length
  break if sinput == "p"
  exit if sinput == "q"
  selected.include?(iinput) ? selected.delete(iinput) : selected << iinput
end

user,pass  = "noah","mypw" 
str = []
selected.each do |x|
  txt = "#{x.to_s}) #{(defs[x-1].inner_text)}"
  str << (txt.nil? ? "" : txt)
end

puts "Posting . . .#{str.join(' ')}" 
begin
  del = WWW::Delicious.new(user,pass)
  del.posts_add(WWW::Delicious::Post.new(:url => url, :title => title, :notes => str.join(' '), :tags => 'dict'))
rescue WWW::Delicious::Error => e
  puts "Oops!"
  puts e.inspect
end

Firefox extensions can only take you so far.

blog comments powered by Disqus