Sha256: bfe5895137ae76e2dc709bb895d08f341883276cbcaea25234608f7635052436

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'rexml/document'
require 'cgi'
require 'open-uri'

module YoutubeSearch
  def self.search(query, options={})
    options = options_with_per_page_and_page(options)
    query = options.merge(:q => query).map{|k,v| "#{CGI.escape k.to_s}=#{CGI.escape v.to_s}" }.join('&')
    xml = open("http://gdata.youtube.com/feeds/api/videos?#{query}").read
    parse(xml)
  end

  def self.parse(xml)
    entries = []
    doc = REXML::Document.new(xml)
    doc.elements.each('feed/entry') do |p|
      entry = Hash[p.children.map do |child|
        [child.name, child.text]
      end]

      entry['video_id'] = entry['id'].split('/').last
      entries << entry
    end
    entries
  end

  private

  def self.options_with_per_page_and_page(options)
    options = options.dup
    if per_page = options.delete(:per_page)
      options['max-results'] = per_page
    else
      per_page = options['max-results']
    end

    if per_page and page = options.delete(:page)
      options['start-index'] = per_page.to_i * ([page.to_i, 1].max - 1)
    end

    options
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
youtube_search-0.1.1 lib/youtube_search.rb