DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Implementation Of VideoEgg Integration Library

02.26.2007
| 2517 views |
  • submit to reddit
        // This is a ruby implementation of the VideoEgg (www.videoegg.com) integration library. You will need to create a developer account on the website, and put in the appropriate authentication items. Although this was meant for Rails, you could probably use it anywhere with Ruby. For Rails, simply put it in your lib/ folder, and call VideoEgg.embed_publisher or VideoEgg.embed_player in your template. 

require 'digest/md5'

VE_HOSTNAME_PREFIX = ""
VE_CLIENT_ID = ""
VE_UPLOADPASSWORD = ""
VE_DOWNLOAD_PASSWORD = ""

VE_KITVER = "1.2.0"
VE_UPLOADTIMEFRAME = 28800
VE_DOWNLOADTIMEFRAME = 3600

module VideoEgg
  def self.embed_publisher_old(action, configuration, bgcolor, hidden, meta, username, destinationURL)
    embed_publisher2(action, configuration, bgcolor, hidden, meta, username, destinationURL, "")
  end

  def self.embed_publisher(action, configuration, bgcolor, hidden, meta, username, destinationURL, loadAlt)
    serverPath = "http://update" + loadAlt + ".videoegg.com/"
    
    hostname_prefix = Object.const_defined?('VE_HOSTNAME_PREFIX') ? VE_HOSTNAME_PREFIX : ""
    
    filename = self.generate_filename
    
    authToken = self.generate_upload_auth_token(filename + "*",  Time.now.to_i + VE_UPLOADTIMEFRAME, VE_UPLOADPASSWORD)
    
    if configuration[-3,3] == ".js"
      configuration_string = configuration
    else
      configuration_string = serverPath + "js/" + configuration + ".js"
    end

    publisher_path = serverPath + 'js/Publisher.js'
    player_path = serverPath + 'js/Player.js'
    
    hidden_str = hidden ? 'true' : 'false'
    
    output = <<EOF
<script language="javascript">
  var VE_SERVER_PATH = "#{serverPath}";
  var VE_LOAD_ALT = "#{loadAlt}";
  var VE_HOSTNAME_PREFIX = "#{hostname_prefix}";
  var VE_KITVER= "#{VE_KITVER}";
</script>

<script language="javascript" src="#{configuration_string}"></script>
<script language="javascript" src="#{publisher_path}"></script>
<script language="javascript" src="#{player_path}"></script>

<script language="javascript">
VE_JSEmbedPublisher('#{authToken}', '#{VE_CLIENT_ID}', '#{filename}', '#{action}', '#{configuration}', '#{bgcolor}', '#{hidden_str}', '#{meta}', '#{username}', '#{destinationURL}');
</script>

EOF
    output
  end

  def self.embed_player(video_path, width=350, height=330)
    output = <<EOF
<script type="text/javascript" src="http://update.videoegg.com/js/Player.js"></script>    
<script type="text/javascript">
  var VE_api = VE_getPlayerAPI("1.2");
  var myMoviePath = "#{video_path}";

  VE_api.embedPlayer(myMoviePath, #{width}, #{height}, false, "", "", false, "", "");
</script>
    
EOF
    output
  end

  def self.embed_thumbnail(video_path, width=0, height=0)
    sizeStr = ''
    sizeStr += "width='#{width}" if width != 0
    sizeStr += "height='#{height}" if height !=0
    
    output = <<EOF
<script type="text/javascript" src="http://update.videoegg.com/js/Player.js"></script>    
<script type="text/javascript">
  var VE_api = VE_getPlayerAPI("1.2");
  var myMoviePath = "#{video_path}";

  var myThumbnailPath = VE_api.getThumbnailURL(myMoviePath);
  var writeStr = "<img src='" + myThumbnailPath + "' #{sizeStr}>"
  document.writeln(writeStr);
</script>
EOF
    output
  end
  
  def self.generate_filename
    rand_string = ""
    dir_string = ""
    
    rand_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    4.times do
      dir_string += rand_chars[rand(36),1]
    end
  
    dir1 = dir_string[0..1]
    dir2 = dir_string[1..2]
    
    rand_chars += "abcdefghijklmnopqrstuvwxyz"
    20.times do
      rand_string += rand_chars[rand(62),1]
    end
    
    file_guid = Time.now.to_i.to_s + rand_string
    
    filename = "/" + VE_CLIENT_ID + "/" + dir1 + "/" + dir2 + "/" + file_guid
  end

  def self.generate_upload_auth_token(filename, expire_time, password)
    cookie_name = "auth"
  
    expires = expire_time
    
    access = Array.new
    access << filename
    
    values = Array.new
    md5c = Array.new
    seed = password
    
    akacook = Hash.new
    akacook['cookiename'] = cookie_name

    ip = ""
    #octets = ip.scan(/[0-9]+/).collect {|n| n.to_i }

    akacook['ip'] = (ip.to_s.empty? ? "" : "expires=" + ip)
    if not akacook['ip'].to_s.empty? then
      values << akacook['ip']
      md5c << ip
    end
        
    akacook['expire'] = (expire_time.to_s.empty? ? "" : "expires=" + expire_time.to_s )
    if not akacook['expire'].to_s.empty? then
      values << akacook['expire']
      md5c << expire_time
    end
    
    access_final = access.join('!')
    akacook['access'] = "access=" + access_final
    values << akacook['access'] if access.size > 0
    md5c << access_final if access.size > 0
    
    complete = values.join('~')
    md5string = md5c.join()
    
    puts "md5string=" + md5string
    puts "seed=" + seed
    
    md5 = Digest::MD5.new(md5string + seed).to_s
    
    authToken = akacook['cookiename'] + '=' + complete + '~md5=' + md5
  end

end