Skip to content

Commit

Permalink
Make reports available to Videos as well
Browse files Browse the repository at this point in the history
Not just for channels, but for videos too, the following reports can
be retrieved: views, comments, likes, dislikes, shares.

Content owners only can also retrieve earnings and impressions.

The code has some duplications (especially in the specs), which will
be removed later! :)
  • Loading branch information
claudiob committed Jun 27, 2014
1 parent 31d14b5 commit 2e44ba4
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ env:
- secure: Ejj8tsuwyrRVmCc/R9ubKWCHWhCGpe0Dy6fc1UuPCkcMZyXq9ZC02v2obWsTQQ7epEgsCYZAO4v/gWpuv1b1huGcWdfJzMW7RCoY87cEf9HnAK0lSwGx4+/pYkEMe8y5p149C3vAR8nqczvEavN1fUq/WwPUqp+JyDP7kwFTs2Y=
- secure: gE5kAT1R54hmS+W3YYGcUtlD8ZskvTctVR3sr+C5CUjVPdq6Ktx5Q/a6EJyAVVrhxpaCOuk3LG+VkzdQIVFUNRiDPcOulkond4HkSQDoy+IJ/wTXvUS+lIJ1ERUnWega+APrQUjH5s2WayPGZUBqWt/u8Tt9EmSUZfuKZSEXqZk=
- secure: ZUx5v/wHW/TENg8NfFINiiMoe2D031ntDTiuIBdf88c/bMClkEtRRgomtK9RBkFonEyGEOkXxUm2SLzRf340V3eIXWQhil7ab1lcYs8X59aVS/NK/GqChH8Nia17gc3OTQ9k6rYvj4Lp60Dh9WG1cijLPd4/OvPmf6qX9uYfJMw=
- secure: DumQVO01Y3Ki1skuOYOZzosDb6jS0XyG1O8Agy3mVxXGJzQE+s1z2UFz4gMpsU9o/gmiNMddp7I6+RtbZjo9hN3H7vlRRwEeB7tuUMiDyomSx1FlHcCFfPdTmhxGg8X78SErMWqNC6eReGrCTgBdIq1ho7dIu53qJNxTEFqx7eI=
- secure: DumQVO01Y3Ki1skuOYOZzosDb6jS0XyG1O8Agy3mVxXGJzQE+s1z2UFz4gMpsU9o/gmiNMddp7I6+RtbZjo9hN3H7vlRRwEeB7tuUMiDyomSx1FlHcCFfPdTmhxGg8X78SErMWqNC6eReGrCTgBdIq1ho7dIu53qJNxTEFqx7eI=
- secure: UCceSRqPe5Y8WCM85qj0i8wlB5gJRSegJZfVj5jnwyermcOc1ZucwDZwTzkYVbLfDGTqTqzqW6M322a1tWm74xeIbxuHG7fw3Waoxk4zh3tg1CmNkzeDZFnl6V10uOMT6pn5E3kfhLzqOzftJN6fTMCgYMdkwJcfdrMX+z1cxwM=
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yt (0.7.5)
yt (0.7.6)
activesupport

GEM
Expand Down
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ v0.7 - 2014/06/18
* Extract Reports (earnings, views) into module with macro `has_report`
* New channel reports: comments, likes, dislikes, shares and impressions
* Allow both normal and partnered channels to retrieve reports about views, comments, likes, dislikes, shares
* Make reports available also on Video (not just Channel)

v0.6 - 2014/06/05
-----------------
Expand Down
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: account

channel.subscribed? #=> false
channel.subscribe #=> true
```

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

```ruby
account = Yt::Account.new access_token: 'ya29.1.ABCDEFGHIJ'
channel = Yt::Channel.new id: 'UCxO1tY8h1AhOz0T4ENwmpow', auth: account

channel.create_playlist title: 'New playlist' #=> true
channel.delete_playlists title: 'New playlist' #=> [true]
Expand All @@ -130,7 +137,7 @@ channel.dislikes to: 2.days.ago #=> {Tue, 27 May 2014 => 0.0, Wed, 28 May 2014 =
channel.shares since: 7.days.ago, until: 7.days.ago #=> {Wed, 28 May 2014 => 3.0}
```

*The methods above require to be authenticated as a YouTube account (see below).*
*The methods above require to be authenticated as the channel’s account (see below).*

```ruby
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
Expand All @@ -156,6 +163,7 @@ Use [Yt::Video](http://rubydoc.info/github/Fullscreen/yt/master/Yt/Models/Video)
* update the attributes of a video
* access the annotations of a video
* like and dislike a video
* retrieve the daily earnings, views, comments, likes, dislikes, shares and impressions of a channel

```ruby
video = Yt::Video.new id: 'MESycYJytkU'
Expand Down Expand Up @@ -195,11 +203,35 @@ video.like #=> true
*The methods above require to be authenticated as a YouTube account (see below).*

```ruby
account = Yt::Account.new access_token: 'ya29.1.ABCDEFGHIJ'
video = Yt::Video.new id: 'MESycYJytkU', auth: account

video.update title: 'A title', description: 'A description', tags: ['a tag'], categoryId: '21'

video.views since: 7.days.ago #=> {Wed, 28 May 2014 => 12.0, Thu, 29 May 2014 => 3.0, …}
video.comments until: 2.days.ago #=> {Wed, 28 May 2014 => 9.0, Thu, 29 May 2014 => 4.0, …}
video.likes from: 8.days.ago #=> {Tue, 27 May 2014 => 7.0, Wed, 28 May 2014 => 0.0, …}
video.dislikes to: 2.days.ago #=> {Tue, 27 May 2014 => 0.0, Wed, 28 May 2014 => 1.0, …}
video.shares since: 7.days.ago, until: 7.days.ago #=> {Wed, 28 May 2014 => 3.0}
```

*The methods above require to be authenticated as the video’s owner (see below).*

```ruby
content_owner = Yt::ContentOwner.new owner_name: 'CMSname', access_token: 'ya29.1.ABCDEFGHIJ'
video = Yt::Video.new id: 'MESycYJytkU', auth: content_owner

video.earnings_on 5.days.ago #=> 12.23
video.views since: 7.days.ago #=> {Wed, 28 May 2014 => 12.0, Thu, 29 May 2014 => 3.0, …}
video.comments until: 2.days.ago #=> {Wed, 28 May 2014 => 9.0, Thu, 29 May 2014 => 4.0, …}
video.likes from: 8.days.ago #=> {Tue, 27 May 2014 => 7.0, Wed, 28 May 2014 => 0.0, …}
video.dislikes to: 2.days.ago #=> {Tue, 27 May 2014 => 0.0, Wed, 28 May 2014 => 1.0, …}
video.shares since: 7.days.ago, until: 7.days.ago #=> {Wed, 28 May 2014 => 3.0}
video.impressions_on 5.days.ago #=> 157.0
```

*The methods above require to be authenticated as the video’s content owner (see below).*

Yt::Playlist
------------

Expand Down Expand Up @@ -439,7 +471,7 @@ To install on your system, run

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

gem 'yt', '~> 0.7.5'
gem 'yt', '~> 0.7.6'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down
40 changes: 40 additions & 0 deletions lib/yt/models/video.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
require 'yt/models/resource'
require 'yt/modules/reports'

module Yt
module Models
# Provides methods to interact with YouTube videos.
# @see https://developers.google.com/youtube/v3/docs/videos
class Video < Resource
# Includes the +:has_report+ method to access YouTube Analytics reports.
extend Modules::Reports

delegate :tags, :channel_id, :channel_title, :category_id,
:live_broadcast_content, to: :snippet

Expand All @@ -22,6 +26,27 @@ class Video < Resource
# @return [Yt::Collections::Annotations] the video’s annotations.
has_many :annotations

# @macro has_report
has_report :earnings

# @macro has_report
has_report :views

# @macro has_report
has_report :comments

# @macro has_report
has_report :likes

# @macro has_report
has_report :dislikes

# @macro has_report
has_report :shares

# @macro has_report
has_report :impressions

# @!attribute [r] statistics_set
# @return [Yt::Models::StatisticsSet] the statistics for the video.
has_one :statistics_set
Expand Down Expand Up @@ -94,6 +119,21 @@ def unlike
!liked?
end

# @private
# Tells `has_reports` to retrieve the reports from YouTube Analytics API
# either as a Channel or as a Content Owner.
# @see https://developers.google.com/youtube/analytics/v1/reports
def reports_params
{}.tap do |params|
if auth.owner_name
params['ids'] = "contentOwner==#{auth.owner_name}"
else
params['ids'] = "channel==#{channel_id}"
end
params['filters'] = "video==#{id}"
end
end

private

# @return [Hash] the parameters to submit to YouTube to update a video.
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Yt
VERSION = '0.7.5'
VERSION = '0.7.6'
end
2 changes: 1 addition & 1 deletion spec/requests/as_account/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@

it 'returns valid reports for channel-related metrics' do
# Some reports are only available to Content Owners.
# See content ownere test for more details about what the methods return.
# See content owner test for more details about what the methods return.
expect{channel.views}.not_to raise_error
expect{channel.comments}.not_to raise_error
expect{channel.likes}.not_to raise_error
Expand Down
23 changes: 22 additions & 1 deletion spec/requests/as_account/video_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
it { expect{video.statistics_set}.to raise_error Yt::Errors::NoItems }
end

context 'given one of my own videos that I want to update' do
context 'given one of my own videos' do
let(:id) { $account.videos.first.id }

describe 'updates the attributes that I specify explicitly' do
Expand All @@ -78,5 +78,26 @@
it { expect{video.update attrs}.not_to change{video.category_id} }
it { expect{video.update attrs}.not_to change{video.privacy_status} }
end


it 'returns valid reports for channel-related metrics' do
# Some reports are only available to Content Owners.
# See content ownere test for more details about what the methods return.
expect{video.views}.not_to raise_error
expect{video.comments}.not_to raise_error
expect{video.likes}.not_to raise_error
expect{video.dislikes}.not_to raise_error
expect{video.shares}.not_to raise_error
expect{video.earnings}.to raise_error Yt::Errors::Unauthorized
expect{video.impressions}.to raise_error Yt::Errors::Unauthorized

expect{video.views_on 3.days.ago}.not_to raise_error
expect{video.comments_on 3.days.ago}.not_to raise_error
expect{video.likes_on 3.days.ago}.not_to raise_error
expect{video.dislikes_on 3.days.ago}.not_to raise_error
expect{video.shares_on 3.days.ago}.not_to raise_error
expect{video.earnings_on 3.days.ago}.to raise_error Yt::Errors::Unauthorized
expect{video.impressions_on 3.days.ago}.to raise_error Yt::Errors::Unauthorized
end
end
end
Loading

0 comments on commit 2e44ba4

Please sign in to comment.