Skip to content

Commit

Permalink
Merge pull request #24 from Fullscreen/channels-with-more-than-500-vi…
Browse files Browse the repository at this point in the history
…deos

Make channel.videos access more than 500 videos
  • Loading branch information
claudiofullscreen committed Jul 16, 2014
2 parents ce804dd + 1350682 commit b8c71c7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yt (0.7.8)
yt (0.7.9)
activesupport

GEM
Expand Down
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -15,6 +15,7 @@ v0.7 - 2014/06/18
* Allow both normal and partnered channels to retrieve reports about views, comments, likes, dislikes, shares
* Make reports available also on Video (not just Channel)
* New account.upload_video to upload a video (either local or remote).
* Make channel.videos access more than 500 videos per channel

v0.6 - 2014/06/05
-----------------
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -41,7 +41,7 @@ To install on your system, run

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'yt', '~> 0.7.8'
gem 'yt', '~> 0.7.9'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down
16 changes: 16 additions & 0 deletions lib/yt/collections/videos.rb
Expand Up @@ -27,8 +27,24 @@ def list_params
end
end

def next_page
super.tap{|items| add_offset_to(items) if @page_token.nil?}
end

# According to http://stackoverflow.com/a/23256768 YouTube does not
# provide more than 500 results for any query. In order to overcome
# that limit, the query is restarted with a publishedBefore filter in
# case there are more videos to be listed for a channel
def add_offset_to(items)
if items.count == videos_params[:maxResults]
last_published = items.last['snippet']['publishedAt']
@page_token, @published_before = '', last_published
end
end

def videos_params
params = {type: :video, maxResults: 50, part: 'snippet', order: 'date'}
params[:publishedBefore] = @published_before if @published_before
@extra_params ||= {}
params.merge @extra_params
end
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/version.rb
@@ -1,3 +1,3 @@
module Yt
VERSION = '0.7.8'
VERSION = '0.7.9'
end
9 changes: 9 additions & 0 deletions spec/requests/as_account/channel_spec.rb
Expand Up @@ -40,6 +40,15 @@
it { expect(channel.subscribed?).to be true }
it { expect(channel.unsubscribe!).to be_truthy }
end

# NOTE: These tests are slow because they go through multiple pages of
# results and do so to test that we can overcome YouTube’s limitation of
# only returning the first 500 results for each query.
context 'with more than 500 videos' do
let(:id) { 'UCsmvakQZlvGsyjyOhmhvOsw' }
it { expect(channel.video_count).to be > 500 }
it { expect(channel.videos.count).to be > 500 }
end
end

context 'given my own channel' do
Expand Down

0 comments on commit b8c71c7

Please sign in to comment.