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
#!/usr/bin/env ruby

require "rubygems"
require 'YAML'
require 'open-uri'

# A flickr library for parsing YAML feeds from http://flickr.com
# USAGE: d = Flickr::YAML.new('kastner')
# d.photos.first.url
module Flickr
  
  # A Photo on the Flickr server
  class Photo
    
    attr_accessor :title, :url, :date, :url, :tags
    
    def initialize(item)
      @title  = item["title"]
      @url    = item["description"].match(/http:\/\/[\w\.\/]+_m\.jpg/)[0]
      @date   = item["date"]
      @tags   = item["tags"]
    end
    
  end
  
  # Class for retriving a user_id from a username on flickr
  class UserName
    def self.get_id(name)
      page = open("http://flickr.com/photos/#{name}").read
      page.match(/photos_public.gne\?id=([\w@]+)/)[1]
    end
  end
  
  # Parses the YAML returned from flickr (in a feed) into a group of photos
  class YAML
    
    attr_reader :photos
    
    def initialize(username)
      @photos = []
      uid     = UserName.get_id(username)
      url     = "http://api.flickr.com/services/feeds/photos_public.gne?id=#{uid}&format=yaml"
      data    = open(url).read
      parsed  = ::YAML::load(data.gsub(/(\w): /, '\1 - '))
      if parsed
        parsed["items"].each do |item|
          @photos << Photo.new(item)
        end
      end      
    end    
  end
end