lib/youtube_search.rb in youtube_search-0.1.9 vs lib/youtube_search.rb in youtube_search-0.1.10

- old
+ new

@@ -1,36 +1,38 @@ require 'rexml/document' require 'cgi' require 'open-uri' module YoutubeSearch + API_URL = 'http://gdata.youtube.com/feeds/api' + class << self def search_page(page, 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("#{page}?#{query}").read parse(xml) end def search(query, options={}) - search_page("http://gdata.youtube.com/feeds/api/videos", query, options) + search_page("#{API_URL}/videos", query, options) end def search_playlists(query, options={}) - search_page("https://gdata.youtube.com/feeds/api/playlists/snippets", query, options.merge(:v => 2)) + search_page("#{API_URL}/playlists/snippets", query, options.merge(:v => 2)) end def playlist_videos(playlist_id, options={}) playlist_id = playlist_id.sub(/^PL/, "") - res = open("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}?v=2#{'&alt=json' if options[:format] == :json}").read - if options[:format] == :json - res - else - parse(res, :type => :playlist) - end + videos "#{API_URL}/playlists/#{playlist_id}?v=2", options[:format] end + def user_channel_videos(channel_id, options={}) + channel_id = channel_id.sub(/^UC/, "") + videos "#{API_URL}/users/#{channel_id}/uploads?v=2", options[:format] + end + def parse(xml, options={}) elements_in(xml, 'feed/entry').map do |element| entry = xml_to_hash(element) entry['video_id'] = if options[:type] == :playlist element.elements['*/yt:videoid'].text @@ -49,9 +51,19 @@ entry end end private + + def videos(url, format) + url += '&alt=json' if format == :json + res = open(url).read + if format == :json + res + else + parse res + end + end def elements_in(xml, selector) entries = [] doc = REXML::Document.new(xml) doc.elements.each(selector) do |element|