Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Google Video Sitemap Support #36

Open
wants to merge 1 commit into from

2 participants

@nikz

As per http://support.google.com/webmasters/bin/answer.py?hl=en&answer=80472#1

It is tested, but it's missing help, I could add that to a Wiki page or to the README if you'd like?

Also, you may or may not be happy about including the video namespace all the time - in which case I can make the change to that being dynamic and only included if you have > 1 :video option somewhere.

@alexrabarts
Owner

Hi @nikz. Sorry for the late response. Yes, this looks good but as it's Google's addition to the Sitemaps standard, it woud make sense to enable it with an option at initialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 19, 2012
  1. @nikz
This page is out of date. Refresh to see the latest.
View
6 Gemfile.lock
@@ -5,10 +5,10 @@ PATH
GEM
specs:
- mocha (0.9.10)
- rake
+ metaclass (0.0.1)
+ mocha (0.12.0)
+ metaclass (~> 0.0.1)
nokogiri (1.4.4)
- rake (0.8.7)
shoulda (2.11.3)
PLATFORMS
View
25 lib/big_sitemap/builder.rb
@@ -7,10 +7,15 @@ class Builder
HEADER_NAME = 'urlset'
HEADER_ATTRIBUTES = {
'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
+ 'xmlns:video' => "http://www.google.com/schemas/sitemap-video/1.1",
'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance",
'xsi:schemaLocation' => "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
}
+ # as per http://support.google.com/webmasters/bin/answer.py?hl=en&answer=80472#1
+ VIDEO_ATTRIBUTES = %w(thumbnail_loc title description content_loc player_loc duration expiration_date rating view_count
+ publication_date family_friendly restriction gallery_loc price requires_subscription uploader platform live)
+
def initialize(options)
@gzip = options.delete(:gzip)
@max_urls = options.delete(:max_urls) || MAX_URLS
@@ -38,6 +43,26 @@ def add_url!(location, options={})
tag! 'changefreq', options[:change_frequency] || 'weekly'
tag! 'priority', options[:priority] if options[:priority]
+ if options[:video]
+ _open_tag 'video:video'
+
+ options[:video].each do |attribute, value_or_hash|
+ if value_or_hash.is_a?(Hash)
+ tag_value = value_or_hash.delete(:value)
+ opts = value_or_hash
+ else
+ tag_value = value_or_hash
+ opts = {}
+ end
+
+ tag_value = tag_value.utc.strftime('%Y-%m-%dT%H:%M:%S+00:00') if attribute.to_s[0..-5] == "_date"
+
+ tag! "video:#{attribute}", tag_value, opts
+ end
+
+ _close_tag 'video:video'
+ end
+
_close_tag 'url'
@urls += 1
View
24 test/big_sitemap_test.rb
@@ -59,17 +59,17 @@ def teardown
end
should 'contain one sitemap element' do
- generate_sitemap { add '/' }
+ generate_sitemap { add '/' }
assert_equal 1, num_elements(sitemaps_index_file, 'sitemap')
end
should 'contain one loc element' do
- generate_sitemap { add '/' }
+ generate_sitemap { add '/' }
assert_equal 1, num_elements(sitemaps_index_file, 'loc')
end
should 'contain one lastmod element' do
- generate_sitemap { add '/' }
+ generate_sitemap { add '/' }
assert_equal 1, num_elements(sitemaps_index_file, 'lastmod')
end
@@ -178,6 +178,24 @@ def teardown
assert_equal 2, num_elements(second_sitemap_file, 'priority')
end
+ should 'allow adding video elements' do
+ video_title = "Maru Motel"
+ video_url = "http://www.youtube.com/embed/cvnPBZbth3E?rel=0"
+ video_date = Time.mktime(2010, 8, 29)
+
+ generate_sitemap do
+ add '/', :video => { :title => video_title,
+ :player_loc => { :value => video_url,
+ :allow_embed => true },
+ :publication_date => video_date
+ }
+ end
+
+ assert_equal video_title, elements(first_sitemap_file, "*[name()='video:title']").first.text
+ assert_equal video_url, elements(first_sitemap_file, "*[name()='video:player_loc']").first.text
+ assert_equal "true", elements(first_sitemap_file, "*[name()='video:player_loc']").first.attributes["allow_embed"].value
+ end
+
should 'not be gzipped' do
generate_sitemap(:gzip => false) { add '/' }
assert File.exists?(unzipped_first_sitemap_file)
View
4 test/test_helper.rb
@@ -55,13 +55,13 @@ def tmp_dir
end
def ns
- {'s' => 'http://www.sitemaps.org/schemas/sitemap/0.9'}
+ {'s' => 'http://www.sitemaps.org/schemas/sitemap/0.9', 'v' => "http://www.google.com/schemas/sitemap-video/1.1"}
end
def elements(filename, el)
file_class = filename.include?('.gz') ? Zlib::GzipReader : File
data = Nokogiri::XML.parse(file_class.open(filename).read)
- data.search("//s:#{el}", ns)
+ data.search("//s:#{el}", "//v:#{el}", ns)
end
def num_elements(filename, el)
Something went wrong with that request. Please try again.