Skip to content

Instantly share code, notes, and snippets.

@mister-ben
Last active August 29, 2015 13:56
Show Gist options
  • Save mister-ben/cbd5b0126d0e4859b5a6 to your computer and use it in GitHub Desktop.
Save mister-ben/cbd5b0126d0e4859b5a6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Creates a video sitemap following this spec https://support.google.com/webmasters/answer/80472?hl=en
#
# This is a proof of concept that shows mapping Media API output to the sitemap fields. It produces a
# sitemap listing every video in a Video Cloud account, and the page URLs are all the same page with
# a varying bctid URL parameter. This would instruct a video player at that page to load that
# particular video. For production use you would probably take a different approach - e.g. for
# each URL you list in your regular sitemap you'd want to look up and add the details of the video
# that's published there.
#
# Set the constants in the CONFIGURATION section to your own values.
#
# This script requires the Ruby wrapper for the Media API from https://github.com/BrightcoveOS/Ruby-MAPI-Wrapper
# install with `gem install brightcove-api`
require 'brightcove-api'
require 'uri'
# CONFIGURATION
# Your media API token with URL access
MEDIA_API_READ_TOKEN="TOKEN.."
# Landing page where you have a single player. Appended URL parameters will tell the player what to play
# This is simplistic - in reality you would put the URL on your site where you have published each video, e.g. by cross-referencing your CMS
LANDING_PAGE_URL="http://example.com/player.html"
# The id of a video player in your account - use your viral player
PLAYER_ID="1234567890"
# Your Video Cloud account
PUBLISHER_ID="1234567890"
# Default thumbnail - a thumbnail is mandatory, so provide a default in case a video doesn't have one (most likely live/remote assets)
DEFAULT_THUMBNAIL="http://example.com/image.jpg"
# Set to true only if a Brightcove Japan account
BRIGHTCOVE_KK=false
# END CONFIGURATION
if BRIGHTCOVE_KK == true
brightcoveapi = Brightcove::API.new(MEDIA_API_READ_TOKEN, "http://api.brightcove.co.jp/services/library")
DOMAIN="co.jp"
else
brightcoveapi = Brightcove::API.new(MEDIA_API_READ_TOKEN)
DOMAIN="com"
end
def sitemap_header
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:video=\"http://www.google.com/schemas/sitemap-video/1.1\">\n"
end
def sitemap_footer
return "</urlset>"
end
def sitemap_contents(items)
string = ""
items['items'].each do | item |
string = string + "\t<url>\n"
# The page to link to
string = string + "\t\t<loc><![CDATA[#{LANDING_PAGE_URL}?bctid=#{item['id']}]]></loc>\n"
string = string + "\t\t<video:video>\n"
# Video title
string = string + "\t\t\t<video:title><![CDATA[#{item['name']}]]></video:title>\n"
# Video description
string = string + "\t\t\t<video:description><![CDATA[#{item['shortDescription']}]]></video:description>\n"
# URL to player SWF
string = string + "<video:player_loc allow_embed=\"true\" autostart=\"autoStart=true\"><![CDATA["
string = string + "http://c.brightcove.#{DOMAIN}/services/viewer/federated_f9/?isVid=1&isUI=1&playerID=#{PLAYER_ID}&publisherID=#{PUBLISHER_ID}&videoId=#{item['id']}"
string = string + "]]></video:player_loc>\n"
# URL to MP4 file, if available
if (item['FLVURL'] =~ URI::regexp) && (item['FLVURL'].include? ".mp4")
string = string + "\t\t\t<video:content_loc><![CDATA[#{item['FLVURL']}]]></video:content_loc>\n"
end
# Thumbnail - default if not set (e.g. live)
if item['videoStillURL'].nil? || item['videoStillURL'].empty?
thumbnail = DEFAULT_THUMBNAIL
else
thumbnail = item['videoStillURL']
end
string = string + "\t\t\t<video:thumbnail_loc><![CDATA[#{thumbnail}]]></video:thumbnail_loc>\n"
# Publish date
pubtime = Time.at(Integer(item['publishedDate'])/1000).xmlschema
string = string + "\t\t\t<video:publication_date><![CDATA[#{pubtime}]]></video:publication_date>\n"
# Duration
length = Integer(item['length'])/1000.floor
if length > 0
string = string + "\t\t\t<video:duration><![CDATA[#{length}]]></video:duration>\n"
end
string = string + "\t\t</video:video>\n"
string = string + "\t</url>\n"
end
return string
end
response = brightcoveapi.get('find_all_videos', {:video_fields => 'id,name,shortDescription,videoStillURL,publishedDate,FLVURL,length', :get_item_count => true, :media_delivery => 'http'})
puts sitemap_header
puts sitemap_contents(response)
i = 0
(Integer(response['total_count'])/100).times do
response = brightcoveapi.get('find_all_videos', {:video_fields => 'id,name,shortDescription,videoStillURL,publishedDate,FLVURL,length', :get_item_count => true, :media_delivery => 'http', :page_number => i += 1})
puts sitemap_contents(response)
end
puts sitemap_footer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment