Skip to content

Instantly share code, notes, and snippets.

@suwa320
Created August 22, 2012 15:27
Show Gist options
  • Select an option

  • Save suwa320/3426765 to your computer and use it in GitHub Desktop.

Select an option

Save suwa320/3426765 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'jekyll'
require 'flickraw'
require 'fileutils'
module Jekyll
class GeneratePhotosets < Generator
safe true
priority :low
def generate(site)
generate_photosets(site) if (site.config['flickr']['enabled'])
end
def generate_photosets(site)
site.posts.each do |post|
post.data['photos'] =
load_photos(post.data['photoset'], site) if post.data['photoset']
end
end
def load_photos(photoset, site)
if cache_dir = site.config['flickr']['cache_dir']
FileUtils.mkdir_p(cache_dir)
path = File.join(cache_dir, "#{Digest::MD5.hexdigest(photoset.to_s)}.yml")
if File.exist?(path)
photos = YAML::load(File.read(path))
else
photos = generate_photo_data(photoset, site)
File.open(path, 'w') {|f| f.print(YAML::dump(photos)) }
end
else
photos = generate_photo_data(photoset, site)
end
photos
end
def generate_photo_data(photoset, site)
returnSet = []
FlickRaw.api_key = site.config['flickr']['api_key']
FlickRaw.shared_secret = site.config['flickr']['shared_secret']
flickr.access_token = site.config['flickr']['auth_token']
flickr.access_secret = site.config['flickr']['secret']
photos = flickr.photosets.
getPhotos(:photoset_id => photoset)
photos.photo.each do |photo|
sizes = flickr.photos.getSizes(
:photo_id => photo.id).inject({}){|h, v| h[v.label.downcase] = v; h}
large = sizes.max{|(ak, av), (bk, bv)|
av.width.to_i + av.height.to_i <=> bv.width.to_i + bv.height.to_i}[0]
photo = FlickrPhoto.new(photo.title, sizes, large)
returnSet << photo.to_liquid
end
returnSet
end
end
end
class FlickrPhoto
attr_accessor :title, :url, :source, :width, :height, :sizes
def initialize(title, sizes, large)
@title = title
@url = sizes[large].url
@source = sizes[large].source
@width = sizes[large].width
@height = sizes[large].height
@sizes = sizes
end
def to_liquid
ret = {
'title' => title,
'url' => url,
'source' => source,
'width' => width,
'height' => height
}
sizes.each do |k, v|
ret["#{k.gsub(/ +/, '_')}_url"] = v.url
ret["#{k.gsub(/ +/, '_')}_source"] = v.source
ret["#{k.gsub(/ +/, '_')}_width"] = v.width
ret["#{k.gsub(/ +/, '_')}_height"] = v.height
end
ret
end
end
if $0 == __FILE__
puts "API Key and shared secret can be obtain at following URL"
puts "http://www.flickr.com/services/apps/by/me"
puts
print "Your API Key: "
FlickRaw.api_key = gets.strip
print "Your shared secret: "
FlickRaw.shared_secret = gets.strip
token = flickr.get_request_token
auth_url = flickr.get_authorize_url(token['oauth_token'], :perms => 'delete')
puts "Open this url in your process to complete the authication process : #{auth_url}"
puts "Copy here the number given when you complete the process."
puts
print "Number: "
verify = gets.strip
begin
flickr.get_access_token(token['oauth_token'], token['oauth_token_secret'], verify)
login = flickr.test.login
puts "You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}"
rescue FlickRaw::FailedResponse => e
puts "Authentication failed : #{e.msg}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment