# Video SEO Developer Kit Version 1.4 # Feb 10, 2010, Ooyala # # The Video SEO Develop Kit is a set of methods which enable publishers to increase the visibility of their content # to search engines. The Develop Kit contains a primary class Ooyala::SEOFriendlyVideo, and two primary methods: # header() and body(). # # See example.rb for an example of how to call these methods require "rubygems" require "base64" require "cgi" require "digest/sha2" require "net/http" require "rexml/document" module Ooyala class BacklotAPIClient def initialize(partner_code, secret_code, cache_for_num_seconds=900) @partner_code = partner_code @secret_code = secret_code @cache_for_num_seconds = cache_for_num_seconds end def query(params) return send_request("query", params) end private def send_request(request_type, params) # Round expires to nearest @cache_for_num_seconds interval params['expires'] = ((Time.now.to_i + 15).to_f / @cache_for_num_seconds).ceil * @cache_for_num_seconds string_to_sign = @secret_code url = "http://cdn.api.ooyala.com/partner/#{request_type}?pcode=#{@partner_code}" params.keys.sort.each do |key| string_to_sign += "#{key}=#{params[key]}" url += "&#{CGI.escape(key.to_s)}=#{CGI.escape(params[key].to_s)}" end digest = Digest::SHA256.digest(string_to_sign) signature = Base64::encode64(digest).chomp.gsub(/=+$/, '') url += "&signature=#{CGI.escape(signature)}" xml_data = Net::HTTP.get_response(URI.parse(url)).body return xml_data end end class SEOFriendlyVideo # Creates an SEOFriendlyVideo instance. Performs a calls to Ooyala's API to obtain relevant SEO data. # # partner_code: the partner code found in your Backlot > Account > Developers tab # secret_code: the secret code found in your Backlot > Account > Developers tab # embed_code: the embed code of your video or channel, found in your Manage > Embed tab def initialize(partner_code, secret_code, embed_code) @api_client = BacklotAPIClient.new(partner_code, secret_code) @embed_code = embed_code fetch_data() end # Returns a string to be inserted into the header of your page. def header() # We assume keywords are stored in the metadata entry for "keywords". return %Q( #{@title.to_s} ) end # Returns a string to be inserted into the body of your page. # # width: the width of your video embed # height: the height of your video embed def body(width, height) player_id = "flashVideoPlayer"; labels_html = '' # use a fixed size div to embed the SEO text. We add overflow: hidden to make sure it doesn't get a larger than the size of the player return %Q(

#{@title.to_s}

#{@description.to_s}

#{@metadata.to_s}

#{labels_html}
) end private # Fetches title, description, labels and metadata from the Backlot API. def fetch_data() # a query to the backlot console is made xml_result = @api_client.query({ 'embedCode' => @embed_code, 'fields' => 'labels,metadata' }) # extract information doc = REXML::Document.new(xml_result) @title = doc.elements['list/item/title'].text @description = doc.elements['list/item/description'].text @labels = doc.get_elements('list/item/labels/label').collect { |l| l.text }.sort @metadata = "" if doc.elements["/list/item/metadata"].has_elements?() doc.get_elements("list/item/metadata/metadataItem").each do |node| @metadata << node.attributes['value'] + ", " end #removing the last comma from the metadata string @metadata = @metadata[0...@metadata.rindex(',')] end end end end