Cómo obtener más de 25 elementos en una lista de reproducción usando la gema youtube_it

I'm trying to use the Youtube_it gem to pull in a list of videos and store the urls, but the built in objects are only giving me 25 items max each time. Does anyone know of a way to use this gem to do this?

El código se ve así:

playlist = client.playlist(playlist_id)
playlist.videos
puts playlist.videos.length

In the meantime I think I need to a custom search in order to accomplish this. Any other suggestions and/or gems that my be useful are welcome. Thanks!

preguntado el 28 de julio de 12 a las 23:07

i'd write a loop, but there isnt an option for start index or max results -

4 Respuestas

I'm not too familiar with youtube_it, but it sounds like the reason you are getting back 25 results is that is the default maximum results that can be returned in a result set. Based on the help page of youtube_it, have you tried setting the parameter max-results...

$ client.videos_by(:query => "penguin", :max_results => 50)

Please note that the number of max_results, at some point, is limited by the YouTube API itself.

Respondido 28 Jul 12, 23:07

Well, I'm pretty sure that you are no longer in this problem, but I found a solution and I just want to share it.

If you have the id of your playlist you can do the follow

@xml = open("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}?max-results=50&start-index=1")
    @result = YouTubeIt::Parser::VideosFeedParser.new(@xml).parse_videos

In this example, I'm just retrieving a maximum of 50 videos, but as you can see, it's pretty easy to adjust.

Now, @result is going to be an array with the video objects, so you can go throug it with:

@result.each do |video|
end

There is a little bug, for some reason the unique_id attribute it's empty, but you can get it with:

str1_markerstring = "/v/"
str2_markerstring = "&"
@result.each do |video|
   unique_id = video.embed_url[/#{str1_markerstring}(.*?)#{str2_markerstring}/m, 1]
end

I hope this can help someone else with the same issue as mine.

contestado el 02 de mayo de 13 a las 04:05

Building on Uriel's answer, I wrote a helpful method fetch_all_videos_from_playlist.

def fetch_videos_from_playlist(playlist_id, start_index = 1)
  xml = open("http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}?max-results=50&start-index=#{start_index}")
  YouTubeIt::Parser::VideosFeedParser.new(xml).parse_videos
end

def fetch_all_videos_from_playlist(playlist_id)
  all_videos = []
  offset = 1
  while (videos = fetch_videos_from_playlist(playlist_id, offset)).length > 0
    all_videos += videos
    offset += videos.length
  end
  all_videos
end

It fetches 50 videos at a time, which seems to be the current max YouTube supports. And a playlist can hold at most 200 videos, apparently. So the method will make up to 4 fetches to get all videos.

Respondido 20 Feb 14, 13:02

Found this solution on youtube_it issues center on GitHub:

playlists_first_page = @client.playlists('sbnation', {'start-index' => 1, 'max-results' => 25})
playlists_second_page = @client.playlists('sbnation', {'start-index' => 26, 'max-results' => 25})

Fuente: https://github.com/kylejginavan/youtube_it/issues/111

contestado el 22 de mayo de 14 a las 00:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.