Permalink
Browse files

support video sitemaps

Example usage:

  sitemap.add(
    UploadedVideo,
    :video => lambda { |video|
      {
        :title => video.name,
        :url => video.flv_path,
        :player_url => video_player_url(video)
      }
    }
  )
  • Loading branch information...
1 parent e966e55 commit 569ad127da929b44af5c27b7e962f55f951fba04 @mislav committed Apr 6, 2009
Showing with 41 additions and 3 deletions.
  1. +7 −3 lib/big_sitemap.rb
  2. +34 −0 lib/big_sitemap/builder.rb
View
@@ -49,10 +49,13 @@ def clean
def generate
for model, options in @sources
- with_sitemap(model.name.tableize) do |sitemap|
+ type = options[:video] && 'video'
+
+ with_sitemap(model.name.tableize, :type => type) do |sitemap|
find_options = options.dup
changefreq = find_options.delete(:change_frequency) || 'weekly'
priority = find_options.delete(:priority)
+ video_gen = find_options.delete(:video)
find_options[:batch_size] ||= @options[:batch_size]
timestamp_column = model.column_names.find { |col| TIMESTAMP_COLUMNS.include? col }
@@ -62,7 +65,8 @@ def generate
polymorphic_url(record),
:time => timestamp_column && record.read_attribute(timestamp_column),
:frequency => changefreq.is_a?(Proc) ? changefreq.call(record) : changefreq,
- :priority => priority.is_a?(Proc) ? priority.call(record) : priority
+ :priority => priority.is_a?(Proc) ? priority.call(record) : priority,
+ :video => video_gen && video_gen.call(record)
)
end
end
@@ -94,7 +98,7 @@ def with_sitemap(name, options = {})
def generate_sitemap_index
with_sitemap 'index', :type => 'index' do |sitemap|
for path in @sitemap_files
- sitemap.add_url!(url_for_sitemap(path), File.stat(path).mtime)
+ sitemap.add_url!(url_for_sitemap(path), :time => File.stat(path).mtime)
end
end
end
@@ -26,6 +26,10 @@ def index?
@type == 'index'
end
+ def video?
+ @type == 'video'
+ end
+
def add_url!(url, options = {})
_rotate if @max_urls == @urls
@@ -34,6 +38,7 @@ def add_url!(url, options = {})
lastmod(options[:time].to_s(:sitemap)) if options[:time]
changefreq(options[:frequency]) if options[:frequency]
priority(options[:priority]) if options[:priority]
+ _build_video(options[:video]) if video?
end
@urls += 1
end
@@ -72,6 +77,7 @@ def _init_document
instruct!
# define root element and namespaces
attrs = {'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'}
+ attrs['xmlns:video'] = 'http://www.google.com/schemas/sitemap-video/1.1' if video?
_open_tag(index?? 'sitemapindex' : 'urlset', attrs)
end
@@ -124,6 +130,34 @@ def _close_document
_close_tag(name)
end
end
+
+ def _build_video(data)
+ return if data.nil? or data.empty?
+
+ video :video do
+ video :content_loc, data[:url] if data[:url]
+ video :player_loc, data[:player_url], :allow_embed => "yes" if data[:player_url]
+ video :thumbnail_loc, data[:thumbnail_url] if data[:thumbnail_url]
+
+ video :title, data[:title] if data[:title]
+ video :description, data[:description] if data[:description]
+ video :rating, data[:rating] if data[:rating]
+ video :view_count, data[:views] if data[:views]
+ video :publication_date, data[:published_at].to_s(:sitemap) if data[:published_at]
+ video :duration, data[:length] if data[:length]
+
+ if data[:tags]
+ for tag in data[:tags]
+ video :tag, tag
+ end
+ end
+
+ unless data[:family_friendly].nil?
+ video :family_friendly, data[:family_friendly] ? 'yes' : 'no'
+ end
+ video :category, data[:category] if data[:category]
+ end
+ end
end
end

0 comments on commit 569ad12

Please sign in to comment.