Module: YoutubeSearch

Defined in:
lib/youtube_search.rb,
lib/youtube_search/version.rb

Constant Summary collapse

VERSION =
'0.1.9'

Class Method Summary collapse

Class Method Details

.parse(xml, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/youtube_search.rb', line 32

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
    else
      entry['id'].split('/').last
    end

    duration = element.elements['*/yt:duration']
    entry['duration'] = duration.attributes['seconds'] if duration

    no_embed = element.elements['yt:noembed'] || element.elements['yt:private']
    entry['embeddable'] = !(no_embed)

    entry['raw'] = element

    entry
  end
end

.playlist_videos(playlist_id, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/youtube_search.rb', line 22

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
end

.search(query, options = {}) ⇒ Object



14
15
16
# File 'lib/youtube_search.rb', line 14

def search(query, options={})
  search_page("http://gdata.youtube.com/feeds/api/videos", query, options)
end

.search_page(page, query, options = {}) ⇒ Object



7
8
9
10
11
12
# File 'lib/youtube_search.rb', line 7

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

.search_playlists(query, options = {}) ⇒ Object



18
19
20
# File 'lib/youtube_search.rb', line 18

def search_playlists(query, options={})
  search_page("https://gdata.youtube.com/feeds/api/playlists/snippets", query, options.merge(:v => 2))
end