How many views do the most recent videos of a YouTube channel have?

HowMany helps you write apps that need to know how many times the videos of a CMS-partnered YouTube channel were viewed.

After registering your app, you can run the only command currently provided by the gem:

HowMany.views channel_id: 'UCxO1tY8h1AhOz0T4ENwmpow' # => 139

How to install

To install on your system, run

gem install how_many

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

gem 'how_many', '~> 0.1.0'

Since the gem follows Semantic Versioning, indicating the full version in your Gemfile (~> major.minor.patch) guarantees that your project won’t occur in any error when you bundle update and a new version of HowMany is released.

Available methods

views

Use HowMany.views to retrieve the median views for the videos of a channel:

require 'how_many'

# get the median views of the last videos of a Fullscreen-partnered channel
HowMany.views(channel_id: 'UCSBzN-I43cp0-xekSFW5mkQ')
#=> 12534.5

# get the same value for a channel with not enough videos
HowMany.views(channel_id: 'UCI7ajRBjOkCUvfQleFUfelA')
#=> raise error HowMany::NotEnoughVideos

# get the same value for PewDiePie
HowMany.views(channel_id: 'UC-lHJZR3Gqxm24_Vd_AJ5Yw')
#=> raise error HowMany::Unauthorized

The methods above require to be authenticated as a YouTube Content Partner account (see below).

Configuring your app

In order to use HowMany you must be able to authenticate as a YouTube CMS owner. You will only be able to retrieve the views for channels partnered with your CMS.

In the Google Developers Console, find or create an app that can be used to obtain a refresh token for the YouTube CMS owner.

Copy Client ID and Client secret from the app and add them together with the refresh token to you code with the following initializer (replacing with your own keys):

HowMany.configure do |config|
  config.client_id = '1234567890.apps.googleusercontent.com'
  config.client_secret = '1234567890'
  config.refresh_token = '1/1234567890'
end

As an alternative to the approach above, you can configure your app with variables. Setting the following environment variables:

export HOW_MANY_CLIENT_ID="1234567890.apps.googleusercontent.com"
export HOW_MANY_CLIENT_SECRET="1234567890"
export HOW_MANY_REFRESH_TOKEN="1/1234567890"

is equivalent to the approach above. If a variable is set in both places, then HowMany.configure takes precedence.

How to test

Set the following environment variables:

  • HOW_MANY_TEST_CLIENT_ID the client ID of your registered YouTube app.
  • HOW_MANY_TEST_CLIENT_SECRET the client secret of your registered YouTube app.
  • HOW_MANY_TEST_REFRESH_TOKEN the refresh token of the test YouTube CMS user.

Then run rspec.

Whenever you change the code, make sure all the tests pass and the code coverage is 100% before submitting a pull request. Thanks.