/usr/share/tdiary/contrib/plugin/flickr.rb is in tdiary-contrib 5.0.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | # -*- coding: utf-8 -*-
# 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 <http://www.machu.jp/>
# Distributed under the GPL
#
require 'net/https'
require 'digest/md5'
require 'rexml/document'
@conf['flickr.apikey'] ||= 'f7e7fb8cc34e52db3e5af5e1727d0c0b'
@conf['flickr.default_size'] ||= 'medium'
if /\A(form|edit|preview|showcomment)\z/ === @mode then
enable_js('flickr.js')
add_js_setting('$tDiary.plugin.flickr')
add_js_setting('$tDiary.plugin.flickr.apiKey', %Q|'#{@conf['flickr.apikey']}'|)
add_js_setting('$tDiary.plugin.flickr.userId', %Q|'#{@conf['flickr.user_id']}'|)
end
def flickr(photo_id, size = nil, place = 'flickr')
if @conf['flickr.apikey'] == nil || @conf['flickr.apikey'].empty?
return '[ERROR] flickr.rb: API Key is not specified.'
end
size ||= @conf['flickr.default_size']
photo = flickr_photo_info(photo_id.to_s, size)
unless photo
return '[ERROR] flickr.rb: failed to get photo.'
end
%Q|<a href="#{photo[:page]}" class="flickr"><img title="#{photo[:title]}" alt="#{photo[:title]}" src="#{photo[:src]}" class="#{place} photo" height="#{photo[:height]}" width="#{photo[:width]}"></a>|
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|
if defined?(Oga)
res = Oga.parse_xml(f)
photo[:page] = res.xpath('/rsp/photo/urls/url').text
photo[:title] = res.xpath('/rsp/photo/title').text
else
res = REXML::Document.new(f)
photo[:page] = res.elements['//rsp/photo/urls/url'].text
photo[:title] = res.elements['//rsp/photo/title'].text
end
}
flickr_open('flickr.photos.getSizes', photo_id) {|f|
if defined?(Oga)
res = Oga.parse_xml(f)
res.xpath('//rsp/sizes/size').each do |s|
if s.get('label').downcase == size.downcase
photo[:src] = s.get('source')
photo[:width] = s.get('width')
photo[:height] = s.get('height')
end
end
else
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
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}"
if !File.exist?(file) || File.size(file) == 0
req = Flickr::Request.new(@conf['flickr.apikey'])
req['method'] = method
req['photo_id'] = photo_id
begin
Timeout.timeout(5) do
open(file, 'w') {|fout|
fout.puts req.open
}
end
rescue TimeoutError, OpenSSL::OpenSSLError => e
File.delete(file)
raise e
end
end
open(file) {|f| yield f }
end
# delete cache files
def flickr_clear_cache
cache_dir = "#{@cache_path}/flickr"
Dir.glob("#{cache_dir}/*.flickr.photos.{getInfo,getSizes}") do |cache|
# File.unlink(cache)
File.rename(cache, "#{cache}.org")
end
end
FLICKER_FORM_PID = 'plugin_flickr_pid'
add_edit_proc do |date|
<<-FORM
<div id="flickr_form" style="margin: 1em 0">
<div>
Flickr: <input type="text" id="flickr_search_text">
<select id="flickr_search_count">
<option value="10">10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
件
<input id="flickr_search" type="button" value="Get flickr photos"></input>
</div>
<div id="flickr_photo_size">
Photo size:
<input type="radio" id="flickr_photo_size_square" name="flickr_photo_size" value="square">
<label for="flickr_photo_size_square">square</label>
<input type="radio" id="flickr_photo_size_large_square" name="flickr_photo_size" value="large square">
<label for="flickr_photo_size_large_square">large square</label>
<input type="radio" id="flickr_photo_size_thumbnail" name="flickr_photo_size" value="thumbnail">
<label for="flickr_photo_size_thumbnail">thumbnail</label>
<input type="radio" id="flickr_photo_size_small" name="flickr_photo_size" value="small">
<label for="flickr_photo_size_small">small</label>
<input type="radio" id="flickr_photo_size_small320" name="flickr_photo_size" value="small 320">
<label for="flickr_photo_size_small320">small 320</label>
<input type="radio" id="flickr_photo_size_medium" name="flickr_photo_size" value="medium" checked="true">
<label for="flickr_photo_size_medium">medium</label>
<input type="radio" id="flickr_photo_size_medium640" name="flickr_photo_size" value="medium 640">
<label for="flickr_photo_size_medium640">medium 640</label>
<input type="radio" id="flickr_photo_size_medium800" name="flickr_photo_size" value="medium 800">
<label for="flickr_photo_size_medium800">medium 800</label>
<input type="radio" id="flickr_photo_size_large" name="flickr_photo_size" value="large">
<label for="flickr_photo_size_large">large</label>
</div>
<div id="flickr_photos" style="margin: 1em">
<!-- <img src="dummy" height="100" width="100" title="dummy"> -->
</div>
</div>
FORM
end
def flickr_slideshow(tag, id = nil)
id ||= @conf['flickr.id']
return unless id
%Q|<iframe align="center" src="http://www.flickr.com/slideShow/index.gne?user_id=#{id}&tags=#{tag}" frameBorder="0" width="500" scrolling="no" height="500"></iframe>|
end
def flickr_slideshow_by_set(set_id)
return unless set_id
%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>|
end
module Flickr
class Request < Hash
def initialize(api_key, secret = nil)
self['api_key'] = api_key
@secret = secret
end
def open
Net::HTTP.version_1_2
https = Net::HTTP.new('www.flickr.com', 443)
https.use_ssl = true
https.start {
response = https.get(query)
response.body
}
end
def query
sign = @secret ? "&api_sig=#{signature}" : ''
base_path + sort.map{|key, val| "#{key}=#{val}" }.join('&') + sign
end
def signature
data = sort.map{|key, val| "#{key}#{val}" }.join
Digest::MD5.hexdigest("#{@secret}#{data}")
end
def base_path
'/services/rest/?'
end
end
class RequestAuth < Request
def base_path
'/services/auth/?'
end
end
end
|