#$:.push("/Users/richardmonette/Documents/ruby projects/rflickr/lib/") 
$:.push("/Users/richardmonette/Documents/ruby projects/")

require 'rubygems'
require 'flickr'
require 'net/http'
require 'uri'

class Flickr  
  
  alias old_initialize initialize
  def initialize(api_key=MY_KEY, email=nil, password=nil)
   # puts "new_initialize " + MY_KEY
    old_initialize(api_key, email, password)
    @host="http://api.flickr.com"
    @activity_file='flickr_activity_cache.xml'
  end
end

class Net::HTTP
  def self.save_file(remote, local)
    uri = URI.parse remote    
    raise "unsuported scheme" unless (uri.scheme.casecmp('HTTP') == 0)
    path = remote[7 + uri.host.length, remote.length]
  
    Net::HTTP.start(uri.host) do |http|
      resp = http.get(path)
     # puts "saving as: " + local
      IMAGE_ARR.push(local)
      open(local, "wb") { |file| file.write(resp.body) }
    end    
  end
end

MY_KEY = '983259433995e8f278587fd6407f090a'
IMAGE_ARR = Array.new

class FlickrPhoto < Shoes

url '/', :index


def index 

    load_photos
    
 @main =    stack do 
    
   para "Here are some photos"
   
 end
 
 @main.append {
   
   x = 0;
   y = 0;
   
   IMAGE_ARR.each { |img| 
     
      puts img
     
        image img, :left => x, :top => y
        
        x += 75
        
        if x > 640 
          y += 75;
          x = 0;
        end
        
     
     }

   
 }
  
end

def load_photos
  
   flickr = Flickr.new

   photos = flickr.photos(:tags => "water", :per_page => '50')

k = 0;

    photos.each { |photo| 
      
      puts photo.title
      
      titleToUse = photo.title
      
      if titleToUse.length < 1 
        titleToUse = "NA"
      end
      
      Net::HTTP::save_file( photo.sizes[0]['source'], "/Users/richardmonette/Desktop/rubyphotos/" + k.to_s + ".jpg" )

      k+= 1

    }
  
end

end

Shoes.app :width => 640, :height => 480, :title => "Shoes Flickr App"


