public
Description: Everyone should experiment!
Homepage:
Clone URL: git://github.com/jonleighton/experiments.git
experiments / bigr.rb
100644 89 lines (75 sloc) 2.423 kb
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
# 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