# Picasa album helper, by Hackerdude.
# For usage, see http://www.hackerdude.com/2006/02/27/parse-picasa-xml-with-rails/
#
# Distributed under the creative commons Attribution license.
# http://creativecommons.org/licenses/by/2.0/
#
# This basically means, do whatever you want with it, give me credit for this piece
# of it.
#
# If you do end up using it, I'd appreciate a line. Please email david at hackerdude
module PicasaAlbumHelper
require 'net/http'
require 'uri'
require 'rexml/document'
# Encapsulates the picasa album. Follows the same attribute
# names as the XML payload
class PicasaAlbum
attr_accessor :firstImage, :lastImage, :albumName
attr_reader :entries
# A hash of entries keyed by their thumbnail name
attr_reader :thumbnails
def entries=(entries)
@entries = entries
# Set the thumbnails
@thumbnails = {}
@entries.each {|entry|
@thumbnails[entry.itemThumbnailImage] = entry
}
end
# Creates the album out of a URL
def self.create_album(base_url, index_path)
result = PicasaAlbum.new()
full_url = base_url+index_path
url = URI.parse(full_url)
res = Net::HTTP.get(url)
#res = req.get(url.path)
xml_string = res
xml_doc = REXML::Document.new(xml_string)
root_node = xml_doc.root
result.albumName = root_node.elements["albumName"].text.strip
entries = []
images_node = root_node.elements["images"]
images_node.each_element('image') { |imageElement|
fileEntry = PicasaFileEntry.new(imageElement)
entries.push(fileEntry)
result.firstImage = imageElement.elements["firstImage"].text.strip
result.lastImage = imageElement.elements["lastImage"].text.strip
}
result.entries = entries;
return result
end
end
# Encapsulates a single file entry. Follows the same attribute
# names as the XML payload.
class PicasaFileEntry
# Some simple metadata
attr_accessor :isFirstImage, :isLastImage, :isPrevImage, :isNextImage,
:itemLargeImage, :nextImage, :nextThumbnail, :prevImage, :prevThumbNail
# The meat of this entry
attr_accessor :itemWidth, :itemHeight, :itemName,
:itemThumbnailImage, :itemThumbnailWidth, :itemThumbnailHeight,
:itemCaption
# The constructor needs an XML element to read the information from.
def initialize(node)
@itemWidth = node.elements["itemWidth"].text.strip
@itemHeight = node.elements["itemHeight"].text.strip
@itemName = node.elements["itemName"].text.strip
@itemThumbnailImage = node.elements["itemThumbnailImage"].text.strip
@itemThumbnailWidth = node.elements["itemThumbnailWidth"].text.strip
@itemCaption = node.elements["itemCaption"].text.strip
@itemLargeImage = node.elements["itemLargeImage"].text.strip
end
end
# Creates a slideshow using a picasa index.xml file.
#
# == Usage:
# include lightbox.js in your page
#
# slideshow( "http://localhost:3000/", "images/travel/album1/index.xml",
# { :max_per_row=3, :max_rows=4 } )
def slideshow( base_url, index_file, options = {} )
max_rows = options[:max_rows]
max_rows = 9999 unless max_rows != nil
max_per_row = options[:max_per_row]
max_per_row = 10 unless max_per_row != nil
fullURL = "#{base_url}#{index_file}"
# Get the base path for the picasa album
itemPath = /http:\/\/([^\/]*)\/(.*)\/([A-Za-z0-9.]*)/.match(fullURL)[2]
# Get album info from picasa
album = PicasaAlbum.create_album(base_url, index_file)
album_thumbnails = album.thumbnails
html_output=""
# Now let's render those images..
row = 0
column = 0
album_thumbnails.keys.each { |itemThumbnailImage|
column += 1
if column > max_per_row then
html_output += "
"
column = 0
row+= 1
end
item = album_thumbnails[itemThumbnailImage]
itemLargeImage = item.itemLargeImage
itemCaption = item.itemCaption
itemThumbnailImage = '' if itemThumbnailImage == nil
itemCaption = '' if itemCaption == nil
itemLargeImage = '' if itemLargeImage == nil
html_output += ""
html_output += "
"
}
return html_output
end
end