root/platform/tdiary/plugin/flickr.rb @ 37332

Revision 37332, 5.2 kB (checked in by drry, 3 years ago)
  • fixed regexes.
  • fixed HTML.
  • et cetera.
Line 
1# show photo image on Flickr.com
2#
3# usage:
4#   flickr(photo_id[, size[, place]])
5#     - photo_id: The id of the photo to show.
6#     - size: Size of photo. (optional)
7#       Choose from square, thumbnail, small, medium, or large.
8#     - place: class name of img element. default is 'flickr'.
9#
10#   flickr_left(photo_id[, size])
11#
12#   flickr_right(photo_id[, size])
13#
14# options configurable through settings:
15#   @conf['flickr.apikey'] : a key for access Flickr API
16#   @conf['flickr.default_size'] : default image size
17#
18# Copyright (c) MATSUOKA Kohei <http://www.machu.jp/>
19# Distributed under the GPL
20#
21require 'open-uri'
22require 'digest/md5'
23require 'rexml/document'
24
25def flickr(photo_id, size = nil, place = 'flickr')
26  unless @conf['flickr.apikey'] || @conf['flickr.apikey'].empty?
27    return '[ERROR] flickr.rb: API Key is not specified.'
28  end
29  size ||= @conf['flickr.default_size'] || 'small'
30  photo = flickr_photo_info(photo_id.to_s, size)
31  unless photo
32    return '[ERROR] flickr.rb: failed to get photo.'
33  end
34
35  if @cgi.mobile_agent?
36    body = %Q|<a href="#{photo[:src]}">#{photo[:title]}</a>|
37  else
38    body = %Q|<a href="#{photo[:page]}"><img title="#{photo[:title]}" alt="#{photo[:title]}" src="#{photo[:src]}" class="#{place}"|
39   unless @conf.iphone?
40    body << %Q| width="#{photo[:width]}"| if photo[:width]
41    body << %Q| height="#{photo[:height]}"| if photo[:height]
42   end
43    body << %Q|></a>|
44  end
45
46  body
47end
48
49def flickr_left(photo_id, size = nil)
50  flickr(photo_id, size, 'left')
51end
52
53def flickr_right(photo_id, size = nil)
54  flickr(photo_id, size, 'right')
55end
56
57def flickr_photo_info(photo_id, size)
58  photo = {}
59
60  begin
61    flickr_open('flickr.photos.getInfo', photo_id) {|f|
62      res = REXML::Document.new(f)
63      photo[:page]  = res.elements['//rsp/photo/urls/url'].text
64      photo[:title] = res.elements['//rsp/photo/title'].text
65    }
66    flickr_open('flickr.photos.getSizes', photo_id) {|f|
67      res = REXML::Document.new(f)
68      res.elements.each('//rsp/sizes/size') do |s|
69        if s.attributes['label'].downcase == size.downcase
70          photo[:src] = s.attributes['source']
71          photo[:width] = s.attributes['width']
72          photo[:height] = s.attributes['height']
73        end
74      end
75    }
76  rescue Exception => e
77    return nil
78  end
79  photo
80end
81
82def flickr_open(method, photo_id)
83  cache_dir = "#{@cache_path}/flickr"
84  Dir::mkdir(cache_dir) unless File::directory?(cache_dir)
85
86  file = "#{cache_dir}/#{photo_id}.#{method}"
87  unless File.exist?(file)
88    req = Flickr::Request.new(@conf['flickr.apikey'])
89    req['method'] = method
90    req['photo_id'] = photo_id
91    begin
92      timeout(5) do
93        open(file, 'w') {|fout|
94          req.open.each {|line| fout.puts line }
95        }
96      end
97    rescue TimeoutError => e
98      File.delete(file)
99      raise e
100    end
101  end
102  open(file) {|f| yield f }
103end
104
105# delete cache files
106def flickr_clear_cache
107  cache_dir = "#{@cache_path}/flickr"
108  Dir.glob("#{cache_dir}/*.flickr.photos.{getInfo,getSizes}") do |cache|
109    # File.unlink(cache)
110    File.rename(cache, "#{cache}.org")
111  end
112end
113
114FLICKER_FORM_PID = 'plugin_flickr_pid'
115add_edit_proc do |date|
116  photo_id = @cgi.params[FLICKER_FORM_PID][0] or next
117
118  # this code was from image.rb
119  case @conf.style.downcase.sub( /\Ablog/, '' )
120  when "wiki", "markdown"
121    ptag1 = "{{"
122    ptag2 = "}}"
123  when "rd"
124    ptag1 = "((%"
125    ptag2 = "%))"
126  else
127    ptag1 = "&lt;%="
128    ptag2 = "%&gt;"
129  end
130
131  ptag = %Q|#{ptag1}flickr #{photo_id}#{ptag2}|
132  photo = flickr_photo_info(photo_id.to_s, 'thumbnail')
133  flickr_image = %Q|<img title="#{photo[:title]}" alt="#{photo[:title]}" src="#{photo[:src]}">|
134
135  <<-FORM
136  <h3 class="subtitle">Flickr</h3>
137  <input type="hidden" name="#{FLICKER_FORM_PID}" value="#{photo_id}">
138  <div class="field title">
139    #{flickr_image}
140    <input type="button" onclick="flickr_edit_insert(&quot;#{ptag}&quot;)" value="#{@flickr_label_form_add}">
141  </div>
142  <script type="text/javascript"><!--
143  function flickr_edit_insert(photo_id) {
144    window.document.forms[0].body.value += photo_id;
145  }
146  //-->
147  </script>
148  FORM
149end
150
151def flickr_slideshow(tag, id = nil)
152  id ||= @conf['flickr.id']
153  return unless id
154  %Q|<iframe align="center" src="http://www.flickr.com/slideShow/index.gne?user_id=#{id}&amp;tags=#{tag}" frameBorder="0" width="500" scrolling="no" height="500"></iframe>|
155end
156
157def flickr_slideshow_by_set(set_id)
158  return unless set_id
159  %Q|<iframe align="center" src="http://www.flickr.com/slideShow/index.gne?set_id=#{set_id}" frameBorder="0" width="500" scrolling="no" height="500"></iframe>|
160end
161
162module Flickr
163  class Request < Hash
164    def initialize(api_key, secret = nil)
165      self['api_key'] = api_key
166      @secret = secret
167    end
168
169    def open(*param, &block)
170      Kernel::open(query, *param, &block)
171    end
172
173    def query
174      sign = @secret ? "&api_sig=#{signature}" : ''
175      base_url + sort.map{|key, val| "#{key}=#{val}" }.join('&') + sign
176    end
177
178    def signature
179      data = sort.map{|key, val| "#{key}#{val}" }.join
180      Digest::MD5.hexdigest("#{@secret}#{data}")
181    end
182
183    def base_url
184      'http://api.flickr.com/services/rest/?'
185    end
186  end
187
188  class RequestAuth < Request
189    def base_url
190      'http://www.flickr.com/services/auth/?'
191    end
192  end
193end
Note: See TracBrowser for help on using the browser.