4 # :title: Del.icio.us plugin
6 # Author:: dmitry kim <dmitry.kim@gmail.com>
7 # Copyright:: (C) 2007 dmitry kim
10 # This plugin uses url_added hook (event) to submit all urls seen by url.rb
11 # plugin to a preconfigured account on http://del.icio.us/
13 require 'rexml/document'
16 class DeliciousPlugin < Plugin
17 DIU_BASE = 'https://api.del.icio.us/v1/posts/'
19 attr_accessor :last_error
21 BotConfig.register BotConfigStringValue.new('delicious.user',
22 :default => '', :desc => "Username on del.icio.us")
23 BotConfig.register BotConfigStringValue.new('delicious.password',
24 :default => '', :desc => "Password on del.icio.us")
25 BotConfig.register BotConfigStringValue.new('delicious.user_fmt',
26 :default => 'user:%s', :desc => "How to convert users to tags?")
27 BotConfig.register BotConfigStringValue.new('delicious.channel_fmt',
28 :default => 'channel:%s', :desc => "How to convert channels to tags?")
30 def help(plugin, topic="")
31 "delicious => show url of del.icio.us feed of bot url log."
34 def diu_req(verb, opts = {})
35 uri = URI.parse(DIU_BASE + verb)
36 uri.query = opts.map { |k, v| "#{k}=#{CGI.escape v}" }.join('&')
37 uri.user = @bot.config['delicious.user']
38 uri.password = @bot.config['delicious.password']
40 if uri.user.empty? || uri.password.empty?
41 self.last_error = 'delicious.user and delicious.password must be set!'
45 return REXML::Document.new(@bot.httputil.get(uri, :cache => false))
48 def diu_add(url, opts = {})
49 old = diu_req('get', :url => url).root.get_elements('/posts/post')[0] rescue ni
52 opts[:description] ||= old.attribute('description').to_s
53 opts[:extended] ||= old.attribute('extended').to_s
54 opts[:tags] = [opts[:tags].split, old.attribute('tag').to_s.split].flatten.uniq.compact.join(' ')
55 debug "reusing existing del.icio.us post"
57 debug "adding new del.icio.us post"
63 def event_url_added(url, options = {})
64 debug("called with #{url}, #{options.inspect}")
66 opts[:description] = options[:title] || options[:info] || url
67 opts[:extended] = options[:extra] if options[:extra]
68 opts[:tags] = @bot.config['delicious.user_fmt'] % options[:nick]
70 opts[:tags] << ' ' + (@bot.config['delicious.channel_fmt'] % options[:channel])
72 debug "backgrounding del.icio.us api call"
73 Thread.new { diu_add(url, opts) }
76 def delicious(m, params)
77 uname = @bot.config['delicious.user']
80 repl << 'error: del.icio.us username not set'
82 repl << "http://del.icio.us/#{uname}"
84 repl << ", last error: #{self.last_error}"
91 plugin = DeliciousPlugin.new
92 plugin.map 'delicious'