# Bigr is a Camping hack which parses the feed of your contacts' photos on
# Flickr and replaces the images with the biggest size available (apart from
# 'original' size because that would be ridiculous).
#
# Enjoy, but remember that long term consumption of fatty feeds will severely
# endanger your health!
require 'rubygems'
require 'camping'
require 'hpricot'
require 'open-uri'
require 'cgi'
require 'mongrel/camping'
Camping.goes :Bigr
module Bigr
API_KEY = raise("Your API key goes here")
USER_ID = raise("Your user id goes here")
SIZES = %w(Large Medium Small Square Thumbnail)
end
module Bigr::Models
class Feed
def url
"http://api.flickr.com/services/feeds/photos_friends.gne?user_id=#{USER_ID}&friends=0&display_all=1&lang=en-us&format=atom"
end
def contents
@contents ||= Hpricot(open(url))
end
def bigr!
(contents/"entry").each do |entry|
id = (entry/"id").inner_html.sub(/^.*\/photo\/(\d+)$/) { $1 }
sizes = Hpricot(open("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=#{Bigr::API_KEY}&photo_id=#{id}"))
biggest_size = nil
Bigr::SIZES.each do |size|
unless (sizes/"size[@label='#{size}']").empty?
biggest_size = sizes.at("size[@label='#{size}']")
break
end
end
content = Hpricot(CGI.unescapeHTML((entry/"content").inner_html))
image = content.at("img")
image.set_attribute(:src, biggest_size.attributes["source"])
image.set_attribute(:height, biggest_size.attributes["height"])
image.set_attribute(:width, biggest_size.attributes["width"])
(entry/"content").inner_html = CGI.escapeHTML(content.to_html)
end
end
def to_html
contents.to_html
end
end
end
module Bigr::Controllers
class Index < R '/'
def get
@feed = Feed.new
@feed.bigr!
render :index
end
end
end
module Bigr::Views
def index
@feed.to_html
end
end
if __FILE__ == $0
config = Mongrel::Configurator.new :host => "0.0.0.0" do
daemonize :cwd => File.dirname(__FILE__), :log_file => File.dirname(__FILE__) + "/bigr.log"
listener :port => 3003 do
uri "/", :handler => Mongrel::Camping::CampingHandler.new(Bigr)
trap("INT") { stop }
run.join
end
end
puts "** Bigr is running at http://localhost:3003/"
end