Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 30 additions and 2 deletions.
  1. +30 −2 lib/youtube_search.rb
View
32 lib/youtube_search.rb
@@ -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.