Module: YoutubeSearch
- Defined in:
- lib/youtube_search.rb,
lib/youtube_search/version.rb
Constant Summary collapse
- VERSION =
'0.1.7'
Class Method Summary collapse
- .parse(xml, options = {}) ⇒ Object
- .playlist_videos(playlist_id) ⇒ Object
- .search(query, options = {}) ⇒ Object
- .search_page(page, query, options = {}) ⇒ Object
- .search_playlists(query, options = {}) ⇒ Object
Class Method Details
.parse(xml, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/youtube_search.rb', line 28 def parse(xml, ={}) elements_in(xml, 'feed/entry').map do |element| entry = xml_to_hash(element) entry['video_id'] = if [: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 = element.elements['yt:noembed'] || element.elements['yt:private'] entry['embeddable'] = !() entry['raw'] = element entry end end |
.playlist_videos(playlist_id) ⇒ Object
22 23 24 25 26 |
# File 'lib/youtube_search.rb', line 22 def playlist_videos(playlist_id) playlist_id = playlist_id.sub(/^PL/, "") xml = open("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}?v=2").read parse(xml, :type => :playlist) end |
.search(query, options = {}) ⇒ Object
14 15 16 |
# File 'lib/youtube_search.rb', line 14 def search(query, ={}) search_page("http://gdata.youtube.com/feeds/api/videos", query, ) 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, ={}) = () query = .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, ={}) search_page("https://gdata.youtube.com/feeds/api/playlists/snippets", query, .merge(:v => 2)) end |