# show photo image on Flickr.com
#
# usage:
# flickr(photo_id[, size[, place]])
# - photo_id: The id of the photo to show.
# - size: Size of photo. (optional)
# Choose from square, thumbnail, small, medium, or large.
# - place: class name of img element. default is 'flickr'.
#
# flickr_left(photo_id[, size])
#
# flickr_right(photo_id[, size])
#
# options configurable through settings:
# @conf['flickr.apikey'] : a key for access Flickr API
# @conf['flickr.default_size'] : default image size
#
# Copyright (c) MATSUOKA Kohei |
end
body
end
def flickr_left(photo_id, size = nil)
flickr(photo_id, size, 'left')
end
def flickr_right(photo_id, size = nil)
flickr(photo_id, size, 'right')
end
def flickr_photo_info(photo_id, size)
photo = {}
begin
flickr_open('flickr.photos.getInfo', photo_id) {|f|
res = REXML::Document.new(f)
photo[:page] = res.elements['//rsp/photo/urls/url'].text
photo[:title] = res.elements['//rsp/photo/title'].text
}
flickr_open('flickr.photos.getSizes', photo_id) {|f|
res = REXML::Document.new(f)
res.elements.each('//rsp/sizes/size') do |s|
if s.attributes['label'].downcase == size.downcase
photo[:src] = s.attributes['source']
photo[:width] = s.attributes['width']
photo[:height] = s.attributes['height']
end
end
}
rescue Exception => e
return nil
end
photo
end
def flickr_open(method, photo_id)
cache_dir = "#{@cache_path}/flickr"
Dir::mkdir(cache_dir) unless File::directory?(cache_dir)
file = "#{cache_dir}/#{photo_id}.#{method}"
unless File.exist?(file)
req = Flickr::Request.new(@conf['flickr.apikey'])
req['method'] = method
req['photo_id'] = photo_id
begin
timeout(5) do
open(file, 'w') {|fout|
req.open.each {|line| fout.puts line }
}
end
rescue TimeoutError => e
File.delete(file)
raise e
end
end
open(file) {|f| yield f }
end
FLICKER_FORM_PID = 'plugin_flickr_pid'
add_edit_proc do |date|
photo_id = @cgi.params[FLICKER_FORM_PID][0] or next
# this code was from image.rb
case @conf.style.sub( /^blog/i, '' )
when /^wiki|markdown$/i
ptag1 = "{{"
ptag2 = "}}"
when /^rd$/i
ptag1 = "((%"
ptag2 = "%))"
else
ptag1 = "<%="
ptag2 = "%>"
end
ptag = %Q|#{ptag1}flickr #{photo_id}#{ptag2}|
photo = flickr_photo_info(photo_id.to_s, 'thumbnail')
flickr_image = %Q|
|
<<-FORM