4 # :title: tumblr interface
6 # Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2009 Giuseppe Bilotta
10 # Submit URLs to channel-specific tumblr accounts
12 # TODO reblog tumblr URLs
13 # TODO support video better (e.g. Vimeo or anything else with embed)
14 # TODO support image better (e.g. pages with a single big image)
15 # TODO customize caption/description format
17 require 'rexml/document'
20 class TumblrPlugin < Plugin
21 RBOT = CGI.escape("rbot #{$version.split.first}")
22 WRITE_URL = "http://www.tumblr.com/api/write"
23 REBLOG_URL = "http://www.tumblr.com/api/reblog"
24 READ_URL = "http://%{user}.tumblr.com/api/read?id=%{id}"
25 LOGIN = "email=%{email}&password=%{pwd}&group=%{group}&format=markdown&generator=" + RBOT
26 PHOTO = "&type=photo&source=%{src}&click-through-url=%{src}"
27 VIDEO = "&type=video&embed=%{src}"
28 CAPTION = "&caption=%{desc}"
29 LINK = "&type=link&url=%{src}"
30 NAME = "&name=%{name}"
31 DESC = "&description=%{desc}"
32 REBLOG = "&post-id=%{id}&reblog-key=%{reblog}"
33 COMMENT = "&comment=%{desc}"
35 def help(plugin, topic="")
38 "tumblr configure [<channel>]: show credentials used for channel <channel> (default: current). tumblr configure [<channel>] <email> <password> [<group>] => post links from channel <channel> (default: current) to group <group> (default: name of channel) using the given tumblr credentials"
40 "tumblr deconfigure [<channel>]: forget credentials for channel <channel> (default: current)."
42 "post links, photos and videos to a channel-specific tumblr. topics: configure, deconfigure"
46 def event_url_added(url, options={})
47 return unless options.key? :channel
48 chan = options[:channel]
49 return unless @registry.key? chan
51 account = @registry[chan]
53 line = options[:ircline]
54 line = nil if line and line.empty?
55 if line and nick = options[:nick]
56 line = "<#{nick}> #{line}"
58 html_line = line ? CGI.escapeHTML(line) : line
63 tumblr = options[:htmlinfo][:headers]['x-tumblr-user'].to_s rescue nil
65 id = url.match(/\/post\/(\d+)/)
69 read_url = READ_URL % { :user => tumblr, :id => id}
70 # TODO seems to return 503 a little too frequently
71 xml = @bot.httputil.get(read_url)
74 reblog = REXML::Document.new(xml).elements["//post"].attributes["reblog-key"] rescue nil
75 if reblog and not reblog.empty?
79 html_line = CGI.escapeHTML("(via <a href=%{url}>%{tumblr}</a>" % {
80 :url => url, :tumblr => tmblr
85 :desc => CGI.escape(htmlline)
94 type = options[:htmlinfo][:headers]['content-type'].first rescue nil
98 data << CAPTION if line
100 if url.match(%r{^http://(\w+\.)?youtube\.com/watch.*})
102 data << CAPTION if line
109 :src => CGI.escape(url),
110 :desc => CGI.escape(html_line),
111 :name => CGI.escape(line)
115 debug "posting #{req.inspect}"
116 resp = @bot.httputil.post(api_url, req)
117 debug "tumblr response: #{resp.inspect}"
120 def configuration(m, params={})
121 channel = params[:channel] || m.channel
123 m.reply _("Please specify a channel")
126 if not @registry.key? channel
127 m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
131 account = @registry[channel]
133 account[:pwd] = _("<hidden>") if m.public?
134 account[:chan] = channel
136 m.reply _("Links on %{chan} will go to %{group} using account %{email} and password %{pwd}" % account)
139 def deconfigure(m, params={})
140 channel = params[:channel] || m.channel
142 m.reply _("Please specify a channel")
145 if not @registry.key? channel
146 m.reply _("No tumblr credentials set for %{chan}" % { :chan => channel })
150 @registry.delete channel
152 m.reply _("Links on %{chan} will not be posted to tumblr anymore" % {:chan => channel})
155 def configure(m, params={})
156 channel = params[:channel] || m.channel
158 m.reply _("Please specify a channel")
161 if @registry.key? channel
162 m.reply _("%{chan} already has credentials configured" % { :chan => channel })
164 group = params[:group] || Channel.npname(channel)
165 group << ".tumblr.com" unless group.match(/\.tumblr\.com/)
166 @registry[channel] = {
167 :email => CGI.escape(params[:email]),
168 :pwd => CGI.escape(params[:pwd]),
169 :group => CGI.escape(group)
174 return configuration(m, params)
179 plugin = TumblrPlugin.new
181 plugin.default_auth('*', false)
183 plugin.map 'tumblr configure [:channel]', :action => :configuration
184 plugin.map 'tumblr deconfigure [:channel]', :action => :deconfigure
185 plugin.map 'tumblr configure [:channel] :email :pwd [:group]',
186 :action => :configure,
187 :requirements => {:channel => Regexp::Irc::GEN_CHAN, :email => /.+@.+/, :group => /[A-Za-z-]+/}