Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 30 additions and 2 deletions.
  1. +30 −2 lib/youtube_search.rb
32 lib/youtube_search.rb
View
@@ -4,10 +4,24 @@
module YoutubeSearch
def self.search(query, options={})
+ @debug = options_with_debug(options)
+
options = options_with_per_page_and_page(options)
+ debug "query string: " + query.inspect
+ debug "options hash: " + options.inspect
+
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)
+ url = "http://gdata.youtube.com/feeds/api/videos?#{query}"
+ debug "resulting url: " + url
+
+ xml = open(url).read
+ array = parse(xml)
+
+ debug "videos returned: " + array.size.to_s
+ totalresults = get_totalresults(xml)
+ debug "total videos: " + totalresults.to_s
+
+ array
end
def self.playlist_videos(playlist_id)
@@ -54,6 +68,20 @@ def self.xml_to_hash(element)
end]
end
+ def self.get_totalresults(xml)
+ doc = REXML::Document.new(xml)
+ doc.elements['feed/openSearch:totalResults'].text.to_i
+ end
+
+ def self.options_with_debug(options)
+ return true if options.delete(:debug)
+ false
+ end
+
+ def self.debug(string)
+ puts "[DEBUG] " + string if @debug
+ end
+
def self.options_with_per_page_and_page(options)
options = options.dup
if per_page = options.delete(:per_page)

No commit comments for this range

Something went wrong with that request. Please try again.