yt-andrewroth 0.25.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. data/.gitignore +27 -0
  2. data/.rspec +3 -0
  3. data/.travis.yml +9 -0
  4. data/.yardopts +5 -0
  5. data/CHANGELOG.md +732 -0
  6. data/Gemfile +4 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +489 -0
  9. data/Rakefile +11 -0
  10. data/YOUTUBE_IT.md +835 -0
  11. data/bin/yt +30 -0
  12. data/gemfiles/Gemfile.activesupport-3.x +4 -0
  13. data/gemfiles/Gemfile.activesupport-4.x +4 -0
  14. data/lib/yt.rb +21 -0
  15. data/lib/yt/actions/base.rb +32 -0
  16. data/lib/yt/actions/delete.rb +19 -0
  17. data/lib/yt/actions/delete_all.rb +32 -0
  18. data/lib/yt/actions/insert.rb +42 -0
  19. data/lib/yt/actions/list.rb +139 -0
  20. data/lib/yt/actions/modify.rb +37 -0
  21. data/lib/yt/actions/patch.rb +19 -0
  22. data/lib/yt/actions/update.rb +19 -0
  23. data/lib/yt/associations/has_attribute.rb +55 -0
  24. data/lib/yt/associations/has_authentication.rb +214 -0
  25. data/lib/yt/associations/has_many.rb +22 -0
  26. data/lib/yt/associations/has_one.rb +22 -0
  27. data/lib/yt/associations/has_reports.rb +320 -0
  28. data/lib/yt/collections/advertising_options_sets.rb +34 -0
  29. data/lib/yt/collections/annotations.rb +62 -0
  30. data/lib/yt/collections/assets.rb +58 -0
  31. data/lib/yt/collections/authentications.rb +47 -0
  32. data/lib/yt/collections/base.rb +62 -0
  33. data/lib/yt/collections/channels.rb +31 -0
  34. data/lib/yt/collections/claim_histories.rb +34 -0
  35. data/lib/yt/collections/claims.rb +56 -0
  36. data/lib/yt/collections/content_details.rb +30 -0
  37. data/lib/yt/collections/content_owner_details.rb +34 -0
  38. data/lib/yt/collections/content_owners.rb +32 -0
  39. data/lib/yt/collections/device_flows.rb +23 -0
  40. data/lib/yt/collections/file_details.rb +30 -0
  41. data/lib/yt/collections/ids.rb +27 -0
  42. data/lib/yt/collections/live_streaming_details.rb +30 -0
  43. data/lib/yt/collections/ownerships.rb +34 -0
  44. data/lib/yt/collections/partnered_channels.rb +28 -0
  45. data/lib/yt/collections/players.rb +30 -0
  46. data/lib/yt/collections/playlist_items.rb +53 -0
  47. data/lib/yt/collections/playlists.rb +28 -0
  48. data/lib/yt/collections/policies.rb +28 -0
  49. data/lib/yt/collections/ratings.rb +23 -0
  50. data/lib/yt/collections/references.rb +46 -0
  51. data/lib/yt/collections/related_playlists.rb +43 -0
  52. data/lib/yt/collections/reports.rb +161 -0
  53. data/lib/yt/collections/resources.rb +57 -0
  54. data/lib/yt/collections/resumable_sessions.rb +51 -0
  55. data/lib/yt/collections/snippets.rb +27 -0
  56. data/lib/yt/collections/statistics_sets.rb +30 -0
  57. data/lib/yt/collections/statuses.rb +27 -0
  58. data/lib/yt/collections/subscribed_channels.rb +46 -0
  59. data/lib/yt/collections/subscribers.rb +33 -0
  60. data/lib/yt/collections/subscriptions.rb +50 -0
  61. data/lib/yt/collections/user_infos.rb +36 -0
  62. data/lib/yt/collections/video_categories.rb +35 -0
  63. data/lib/yt/collections/videos.rb +137 -0
  64. data/lib/yt/config.rb +54 -0
  65. data/lib/yt/errors/forbidden.rb +13 -0
  66. data/lib/yt/errors/missing_auth.rb +81 -0
  67. data/lib/yt/errors/no_items.rb +13 -0
  68. data/lib/yt/errors/request_error.rb +74 -0
  69. data/lib/yt/errors/server_error.rb +13 -0
  70. data/lib/yt/errors/unauthorized.rb +50 -0
  71. data/lib/yt/models/account.rb +216 -0
  72. data/lib/yt/models/advertising_options_set.rb +38 -0
  73. data/lib/yt/models/annotation.rb +132 -0
  74. data/lib/yt/models/asset.rb +111 -0
  75. data/lib/yt/models/asset_metadata.rb +38 -0
  76. data/lib/yt/models/asset_snippet.rb +46 -0
  77. data/lib/yt/models/authentication.rb +83 -0
  78. data/lib/yt/models/base.rb +32 -0
  79. data/lib/yt/models/channel.rb +302 -0
  80. data/lib/yt/models/claim.rb +156 -0
  81. data/lib/yt/models/claim_event.rb +67 -0
  82. data/lib/yt/models/claim_history.rb +29 -0
  83. data/lib/yt/models/configuration.rb +70 -0
  84. data/lib/yt/models/content_detail.rb +65 -0
  85. data/lib/yt/models/content_owner.rb +48 -0
  86. data/lib/yt/models/content_owner_detail.rb +18 -0
  87. data/lib/yt/models/description.rb +58 -0
  88. data/lib/yt/models/device_flow.rb +16 -0
  89. data/lib/yt/models/file_detail.rb +21 -0
  90. data/lib/yt/models/id.rb +9 -0
  91. data/lib/yt/models/iterator.rb +16 -0
  92. data/lib/yt/models/live_streaming_detail.rb +23 -0
  93. data/lib/yt/models/match_policy.rb +34 -0
  94. data/lib/yt/models/ownership.rb +75 -0
  95. data/lib/yt/models/player.rb +18 -0
  96. data/lib/yt/models/playlist.rb +218 -0
  97. data/lib/yt/models/playlist_item.rb +112 -0
  98. data/lib/yt/models/policy.rb +36 -0
  99. data/lib/yt/models/policy_rule.rb +124 -0
  100. data/lib/yt/models/rating.rb +37 -0
  101. data/lib/yt/models/reference.rb +172 -0
  102. data/lib/yt/models/resource.rb +136 -0
  103. data/lib/yt/models/resumable_session.rb +52 -0
  104. data/lib/yt/models/right_owner.rb +58 -0
  105. data/lib/yt/models/snippet.rb +50 -0
  106. data/lib/yt/models/statistics_set.rb +26 -0
  107. data/lib/yt/models/status.rb +32 -0
  108. data/lib/yt/models/subscription.rb +38 -0
  109. data/lib/yt/models/timestamp.rb +13 -0
  110. data/lib/yt/models/url.rb +90 -0
  111. data/lib/yt/models/user_info.rb +26 -0
  112. data/lib/yt/models/video.rb +630 -0
  113. data/lib/yt/models/video_category.rb +12 -0
  114. data/lib/yt/request.rb +278 -0
  115. data/lib/yt/version.rb +3 -0
  116. data/spec/collections/claims_spec.rb +30 -0
  117. data/spec/collections/playlist_items_spec.rb +44 -0
  118. data/spec/collections/playlists_spec.rb +27 -0
  119. data/spec/collections/policies_spec.rb +30 -0
  120. data/spec/collections/references_spec.rb +30 -0
  121. data/spec/collections/reports_spec.rb +30 -0
  122. data/spec/collections/subscriptions_spec.rb +25 -0
  123. data/spec/collections/videos_spec.rb +43 -0
  124. data/spec/errors/forbidden_spec.rb +10 -0
  125. data/spec/errors/missing_auth_spec.rb +24 -0
  126. data/spec/errors/no_items_spec.rb +10 -0
  127. data/spec/errors/request_error_spec.rb +44 -0
  128. data/spec/errors/server_error_spec.rb +10 -0
  129. data/spec/errors/unauthorized_spec.rb +10 -0
  130. data/spec/models/account_spec.rb +138 -0
  131. data/spec/models/annotation_spec.rb +180 -0
  132. data/spec/models/asset_spec.rb +20 -0
  133. data/spec/models/channel_spec.rb +127 -0
  134. data/spec/models/claim_event_spec.rb +62 -0
  135. data/spec/models/claim_history_spec.rb +27 -0
  136. data/spec/models/claim_spec.rb +211 -0
  137. data/spec/models/configuration_spec.rb +44 -0
  138. data/spec/models/content_detail_spec.rb +45 -0
  139. data/spec/models/content_owner_detail_spec.rb +6 -0
  140. data/spec/models/description_spec.rb +94 -0
  141. data/spec/models/file_detail_spec.rb +13 -0
  142. data/spec/models/live_streaming_detail_spec.rb +6 -0
  143. data/spec/models/ownership_spec.rb +59 -0
  144. data/spec/models/player_spec.rb +13 -0
  145. data/spec/models/playlist_item_spec.rb +120 -0
  146. data/spec/models/playlist_spec.rb +138 -0
  147. data/spec/models/policy_rule_spec.rb +63 -0
  148. data/spec/models/policy_spec.rb +41 -0
  149. data/spec/models/rating_spec.rb +12 -0
  150. data/spec/models/reference_spec.rb +249 -0
  151. data/spec/models/request_spec.rb +163 -0
  152. data/spec/models/resource_spec.rb +57 -0
  153. data/spec/models/right_owner_spec.rb +71 -0
  154. data/spec/models/snippet_spec.rb +13 -0
  155. data/spec/models/statistics_set_spec.rb +13 -0
  156. data/spec/models/status_spec.rb +13 -0
  157. data/spec/models/subscription_spec.rb +30 -0
  158. data/spec/models/url_spec.rb +78 -0
  159. data/spec/models/video_category_spec.rb +21 -0
  160. data/spec/models/video_spec.rb +669 -0
  161. data/spec/requests/as_account/account_spec.rb +125 -0
  162. data/spec/requests/as_account/authentications_spec.rb +139 -0
  163. data/spec/requests/as_account/channel_spec.rb +259 -0
  164. data/spec/requests/as_account/channels_spec.rb +18 -0
  165. data/spec/requests/as_account/playlist_item_spec.rb +56 -0
  166. data/spec/requests/as_account/playlist_spec.rb +244 -0
  167. data/spec/requests/as_account/resource_spec.rb +18 -0
  168. data/spec/requests/as_account/thumbnail.jpg +0 -0
  169. data/spec/requests/as_account/video.mp4 +0 -0
  170. data/spec/requests/as_account/video_spec.rb +408 -0
  171. data/spec/requests/as_content_owner/account_spec.rb +25 -0
  172. data/spec/requests/as_content_owner/advertising_options_set_spec.rb +15 -0
  173. data/spec/requests/as_content_owner/asset_spec.rb +20 -0
  174. data/spec/requests/as_content_owner/channel_spec.rb +1934 -0
  175. data/spec/requests/as_content_owner/claim_history_spec.rb +20 -0
  176. data/spec/requests/as_content_owner/content_owner_spec.rb +241 -0
  177. data/spec/requests/as_content_owner/match_policy_spec.rb +17 -0
  178. data/spec/requests/as_content_owner/ownership_spec.rb +25 -0
  179. data/spec/requests/as_content_owner/playlist_spec.rb +782 -0
  180. data/spec/requests/as_content_owner/video_spec.rb +1239 -0
  181. data/spec/requests/as_server_app/channel_spec.rb +74 -0
  182. data/spec/requests/as_server_app/playlist_item_spec.rb +30 -0
  183. data/spec/requests/as_server_app/playlist_spec.rb +53 -0
  184. data/spec/requests/as_server_app/video_spec.rb +58 -0
  185. data/spec/requests/as_server_app/videos_spec.rb +40 -0
  186. data/spec/requests/unauthenticated/video_spec.rb +22 -0
  187. data/spec/spec_helper.rb +20 -0
  188. data/spec/support/fail_matcher.rb +15 -0
  189. data/spec/support/global_hooks.rb +48 -0
  190. data/yt.gemspec +32 -0
  191. metadata +416 -0
@@ -0,0 +1,26 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Provides methods to retrieve an account’s user profile.
7
+ # @see https://developers.google.com/+/api/latest/people/getOpenIdConnect
8
+ class UserInfo < Base
9
+ def initialize(options = {})
10
+ @data = options[:data]
11
+ end
12
+
13
+ has_attribute :id, default: ''
14
+ has_attribute :email, default: ''
15
+ has_attribute :verified_email, default: false, camelize: false
16
+ has_attribute :name, default: ''
17
+ has_attribute :given_name, default: '', camelize: false
18
+ has_attribute :family_name, default: '', camelize: false
19
+ has_attribute :link, default: ''
20
+ has_attribute :picture, default: ''
21
+ has_attribute :gender, default: ''
22
+ has_attribute :locale, default: ''
23
+ has_attribute :hd, default: ''
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,630 @@
1
+ require 'yt/models/resource'
2
+
3
+ module Yt
4
+ module Models
5
+ # Provides methods to interact with YouTube videos.
6
+ # @see https://developers.google.com/youtube/v3/docs/videos
7
+ class Video < Resource
8
+
9
+ ### SNIPPET ###
10
+
11
+ # @!attribute [r] title
12
+ # @return [String] the video’s title.
13
+ delegate :title, to: :snippet
14
+
15
+ # @!attribute [r] description
16
+ # @return [String] the video’s description.
17
+ delegate :description, to: :snippet
18
+
19
+ # @!method thumbnail_url(size = :default)
20
+ # Returns the URL of the video’s thumbnail.
21
+ # @param [Symbol, String] size The size of the video’s thumbnail.
22
+ # @return [String] if +size+ is +default+, the URL of a 120x90px image.
23
+ # @return [String] if +size+ is +medium+, the URL of a 320x180px image.
24
+ # @return [String] if +size+ is +high+, the URL of a 480x360px image.
25
+ # @return [nil] if the +size+ is not +default+, +medium+ or +high+.
26
+ delegate :thumbnail_url, to: :snippet
27
+
28
+ # @!attribute [r] published_at
29
+ # @return [Time] the date and time that the video was published.
30
+ delegate :published_at, to: :snippet
31
+
32
+ # @!attribute [r] channel_id
33
+ # @return [String] the ID of the channel that the video belongs to.
34
+ delegate :channel_id, to: :snippet
35
+
36
+ # @!attribute [r] channel_title
37
+ # @return [String] the title of the channel that the video belongs to.
38
+ delegate :channel_title, to: :snippet
39
+
40
+ # @!attribute [r] live_broadcast_content
41
+ # @return [String] the type of live broadcast that the video contains.
42
+ # Possible values are: +'live'+, +'none'+, +'upcoming'+.
43
+ delegate :live_broadcast_content, to: :snippet
44
+
45
+ # @return [Array<String>] the list of tags attached to the video.
46
+ def tags
47
+ ensure_complete_snippet :tags
48
+ end
49
+
50
+ # @return [String] ID of the YouTube category associated with the video.
51
+ def category_id
52
+ ensure_complete_snippet :category_id
53
+ end
54
+
55
+ ### STATUS ###
56
+
57
+ # @return [Boolean] whether the video was deleted by the user.
58
+ def deleted?
59
+ status.upload_status == 'deleted'
60
+ end
61
+
62
+ # @return [Boolean] whether the video failed to upload.
63
+ def failed?
64
+ status.upload_status == 'failed'
65
+ end
66
+
67
+ # @return [Boolean] whether the video has been fully processed by YouTube.
68
+ def processed?
69
+ status.upload_status == 'processed'
70
+ end
71
+
72
+ # @return [Boolean] whether the video was rejected by YouTube.
73
+ def rejected?
74
+ status.upload_status == 'rejected'
75
+ end
76
+
77
+ # @return [Boolean] whether the video is being uploaded to YouTube.
78
+ def uploading?
79
+ status.upload_status == 'uploaded'
80
+ end
81
+
82
+ # @return [Boolean] whether the video failed to upload to YouTube because
83
+ # of an unsupported codec.
84
+ # @see https://support.google.com/youtube/answer/1722171
85
+ def uses_unsupported_codec?
86
+ status.failure_reason == 'codec'
87
+ end
88
+
89
+ # @return [Boolean] whether the video failed to upload to YouTube because
90
+ # YouTube was unable to convert the video.
91
+ def has_failed_conversion?
92
+ status.failure_reason == 'conversion'
93
+ end
94
+
95
+ # @return [Boolean] whether the video failed to upload to YouTube because
96
+ # the video file is empty.
97
+ def empty?
98
+ status.failure_reason == 'emptyFile'
99
+ end
100
+
101
+ # @return [Boolean] whether the video failed to upload to YouTube because
102
+ # the video uses an unsupported file format.
103
+ # @see https://support.google.com/youtube/troubleshooter/2888402?hl=en
104
+ def invalid?
105
+ status.failure_reason == 'invalidFile'
106
+ end
107
+
108
+ # @return [Boolean] whether the video failed to upload to YouTube because
109
+ # the video file is too small for YouTube.
110
+ def too_small?
111
+ status.failure_reason == 'tooSmall'
112
+ end
113
+
114
+ # @return [Boolean] whether the video failed to upload to YouTube because
115
+ # the uploading process was aborted.
116
+ def aborted?
117
+ status.failure_reason == 'uploadAborted'
118
+ end
119
+
120
+ # @return [Boolean] whether the video was rejected by YouTube because
121
+ # the video was claimed by a different account.
122
+ def claimed?
123
+ status.rejection_reason == 'claim'
124
+ end
125
+
126
+ # @return [Boolean] whether the video was rejected by YouTube because
127
+ # the video commits a copyright infringement.
128
+ def infringes_copyright?
129
+ status.rejection_reason == 'copyright'
130
+ end
131
+
132
+ # @return [Boolean] whether the video was rejected by YouTube because
133
+ # the video is a duplicate of another video.
134
+ def duplicate?
135
+ status.rejection_reason == 'duplicate'
136
+ end
137
+
138
+ # @return [Boolean] whether the video was rejected by YouTube because
139
+ # the video contains inappropriate content.
140
+ def inappropriate?
141
+ status.rejection_reason == 'inappropriate'
142
+ end
143
+
144
+ # @return [Boolean] whether the video was rejected by YouTube because
145
+ # the video exceeds the maximum duration for YouTube.
146
+ # @see https://support.google.com/youtube/answer/71673?hl=en
147
+ def too_long?
148
+ status.rejection_reason == 'length'
149
+ end
150
+
151
+ # @return [Boolean] whether the video was rejected by YouTube because
152
+ # the video violates the Terms of Use.
153
+ def violates_terms_of_use?
154
+ status.rejection_reason == 'termsOfUse'
155
+ end
156
+
157
+ # @return [Boolean] whether the video was rejected by YouTube because
158
+ # the video infringes a trademark.
159
+ # @see https://support.google.com/youtube/answer/2801979?hl=en
160
+ def infringes_trademark?
161
+ status.rejection_reason == 'trademark'
162
+ end
163
+
164
+ # @return [Boolean] whether the video was rejected by YouTube because
165
+ # the account that uploaded the video has been closed.
166
+ def belongs_to_closed_account?
167
+ status.rejection_reason == 'uploaderAccountClosed'
168
+ end
169
+
170
+ # @return [Boolean] whether the video was rejected by YouTube because
171
+ # the account that uploaded the video has been suspended.
172
+ def belongs_to_suspended_account?
173
+ status.rejection_reason == 'uploaderAccountSuspended'
174
+ end
175
+
176
+ # Returns the time when a video is scheduled to be published.
177
+ # @return [Time] if the video is scheduled to be published, the time
178
+ # when it will become public.
179
+ # @return [nil] if the video is not scheduled to be published.
180
+ def scheduled_at
181
+ status.publish_at if scheduled?
182
+ end
183
+
184
+ # @return [Boolean] whether the video is scheduled to be published.
185
+ def scheduled?
186
+ private? && status.publish_at
187
+ end
188
+
189
+ # @!attribute [r] license
190
+ # @return [String] the video’s license. Possible values are:
191
+ # +'creativeCommon'+, +'youtube'+.
192
+ delegate :license, to: :status
193
+
194
+ # @return [Boolean] whether the video uses the Standard YouTube license.
195
+ # @see https://www.youtube.com/static?template=terms
196
+ def licensed_as_standard_youtube?
197
+ license == 'youtube'
198
+ end
199
+
200
+ # @return [Boolean] whether the video uses a Creative Commons license.
201
+ # @see https://support.google.com/youtube/answer/2797468?hl=en
202
+ def licensed_as_creative_commons?
203
+ license == 'creativeCommon'
204
+ end
205
+
206
+ # Returns whether the video statistics are publicly viewable.
207
+ # @return [Boolean] if the resource is a video, whether the extended
208
+ # video statistics on the video’s watch page are publicly viewable.
209
+ # By default, those statistics are viewable, and statistics like a
210
+ # video’s viewcount and ratings will still be publicly visible even
211
+ # if this property’s value is set to false.
212
+ def has_public_stats_viewable?
213
+ status.public_stats_viewable
214
+ end
215
+
216
+ # @return [Boolean] whether the video can be embedded on other websites.
217
+ def embeddable?
218
+ status.embeddable
219
+ end
220
+
221
+ ### CONTENT DETAILS ###
222
+
223
+ has_one :content_detail
224
+
225
+ # @!attribute [r] duration
226
+ # @return [Integer] the duration of the video (in seconds).
227
+ delegate :duration, to: :content_detail
228
+
229
+ # @return [Boolean] whether the video is available in 3D.
230
+ def stereoscopic?
231
+ content_detail.dimension == '3d'
232
+ end
233
+
234
+ # @return [Boolean] whether the video is available in high definition.
235
+ def hd?
236
+ content_detail.definition == 'hd'
237
+ end
238
+
239
+ # @return [Boolean] whether captions are available for the video.
240
+ def captioned?
241
+ content_detail.caption == 'true'
242
+ end
243
+
244
+ # @return [Boolean] whether the video represents licensed content, which
245
+ # means that the content has been claimed by a YouTube content partner.
246
+ def licensed?
247
+ content_detail.licensed_content || false
248
+ end
249
+
250
+ # @return [Boolean] whether the video is identified by YouTube as
251
+ # age-restricted content.
252
+ def age_restricted?
253
+ content_detail.youtube_rating == 'ytAgeRestricted'
254
+ end
255
+
256
+ ### FILE DETAILS ###
257
+
258
+ has_one :file_detail
259
+
260
+ # @!attribute [r] file_size
261
+ # @return [Integer] the size of the uploaded file (in bytes).
262
+ delegate :file_size, to: :file_detail
263
+
264
+ # @!attribute [r] file_type
265
+ # @return [String] the type of file uploaded. May be one of:
266
+ # archive, audio, document, image, other, project, video.
267
+ delegate :file_type, to: :file_detail
268
+
269
+ # @!attribute [r] container
270
+ # @return [String] the video container of the uploaded file. (e.g. 'mov').
271
+ delegate :container, to: :file_detail
272
+
273
+
274
+ ### RATING ###
275
+
276
+ has_one :rating
277
+
278
+ # @return [Boolean] whether the authenticated account likes the video.
279
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
280
+ # authenticated Yt::Account.
281
+ def liked?
282
+ rating.rating == :like
283
+ end
284
+
285
+ # Likes the video on behalf of the authenticated account.
286
+ # @return [Boolean] whether the authenticated account likes the video.
287
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
288
+ # authenticated Yt::Account.
289
+ def like
290
+ rating.set :like
291
+ liked?
292
+ end
293
+
294
+ # Dislikes the video on behalf of the authenticated account.
295
+ # @return [Boolean] whether the account does not like the video.
296
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
297
+ # authenticated Yt::Account.
298
+ def dislike
299
+ rating.set :dislike
300
+ !liked?
301
+ end
302
+
303
+ # Resets the rating of the video on behalf of the authenticated account.
304
+ # @return [Boolean] whether the account does not like the video.
305
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
306
+ # authenticated Yt::Account.
307
+ def unlike
308
+ rating.set :none
309
+ !liked?
310
+ end
311
+
312
+ ### VIDEO CATEGORY ###
313
+
314
+ has_one :video_category
315
+
316
+ # @return [String] the video category’s title.
317
+ def category_title
318
+ video_category.title
319
+ end
320
+
321
+ ### ADVERTISING OPTIONS ###
322
+
323
+ has_one :advertising_options_set
324
+
325
+ # @!attribute [r] ad_formats
326
+ # @return [Array<String>] the list of ad formats that the video is
327
+ # allowed to show. Possible values are: +'long'+, +'overlay'+,
328
+ # +'standard_instream'+, +'third_party'+, +'trueview_inslate'+,
329
+ # +'trueview_instream'+.
330
+ delegate :ad_formats, to: :advertising_options_set
331
+
332
+ ### LIVE STREAMING DETAILS ###
333
+
334
+ has_one :live_streaming_detail
335
+
336
+ # @!attribute [r] actual_start_time
337
+ # The time when a live broadcast started.
338
+ # @return [Time] if the broadcast has begun, the time it actually started.
339
+ # @return [nil] if the broadcast has not begun or video is not live.
340
+ delegate :actual_start_time, to: :live_streaming_detail
341
+
342
+ # @!attribute [r] actual_end_time
343
+ # The time when a live broadcast ended.
344
+ # @return [Time] if the broadcast is over, the time it actually ended.
345
+ # @return [nil] if the broadcast is not over or video is not live.
346
+ delegate :actual_end_time, to: :live_streaming_detail
347
+
348
+ # @!attribute [r] scheduled_start_time
349
+ # The time when a live broadcast is scheduled to start.
350
+ # @return [Time] the time that the broadcast is scheduled to begin.
351
+ # @return [nil] if video is not live.
352
+ delegate :scheduled_start_time, to: :live_streaming_detail
353
+
354
+ # @!attribute [r] scheduled_end_time
355
+ # The time when a live broadcast is scheduled to end.
356
+ # @return [Time] if the broadcast is scheduled to end, the time it is
357
+ # scheduled to end.
358
+ # @return [nil] if the broadcast is scheduled to continue indefinitely
359
+ # or the video is not live.
360
+ delegate :scheduled_end_time, to: :live_streaming_detail
361
+
362
+ # @!attribute [r] concurrent_viewers
363
+ # The number of current viewers of a live broadcast.
364
+ # @return [Integer] if the broadcast has current viewers and the
365
+ # broadcast owner has not hidden the viewcount for the video, the
366
+ # number of viewers currently watching the broadcast.
367
+ # @return [nil] if the broadcast has ended or the broadcast owner has
368
+ # hidden the viewcount for the video or the video is not live.
369
+ delegate :concurrent_viewers, to: :live_streaming_detail
370
+
371
+ ### ASSOCIATIONS ###
372
+
373
+ # @!attribute [r] annotations
374
+ # @return [Yt::Collections::Annotations] the video’s annotations.
375
+ has_many :annotations
376
+
377
+ has_many :resumable_sessions
378
+
379
+ ### ANALYTICS ###
380
+
381
+ # @macro reports
382
+
383
+ # @macro report_by_video_dimensions
384
+ has_report :views, Integer
385
+
386
+ # @macro report_by_day
387
+ has_report :uniques, Integer
388
+
389
+ # @macro report_by_video_dimensions
390
+ has_report :estimated_minutes_watched, Integer
391
+
392
+ # @macro report_by_gender_and_age_group
393
+ has_report :viewer_percentage, Float
394
+
395
+ # @macro report_by_day_and_country
396
+ has_report :comments, Integer
397
+
398
+ # @macro report_by_day_and_country
399
+ has_report :likes, Integer
400
+
401
+ # @macro report_by_day_and_country
402
+ has_report :dislikes, Integer
403
+
404
+ # @macro report_by_day_and_country
405
+ has_report :shares, Integer
406
+
407
+ # @note This is not the total number of subscribers gained by the video’s
408
+ # channel, but the subscribers gained *from* the video’s page.
409
+ # @macro report_by_day_and_country
410
+ has_report :subscribers_gained, Integer
411
+
412
+ # @note This is not the total number of subscribers lost by the video’s
413
+ # channel, but the subscribers lost *from* the video’s page.
414
+ # @macro report_by_day_and_country
415
+ has_report :subscribers_lost, Integer
416
+
417
+ # @macro report_by_day_and_country
418
+ has_report :favorites_added, Integer
419
+
420
+ # @macro report_by_day_and_country
421
+ has_report :favorites_removed, Integer
422
+
423
+ # @macro report_by_day_and_state
424
+ has_report :average_view_duration, Integer
425
+
426
+ # @macro report_by_day_and_state
427
+ has_report :average_view_percentage, Float
428
+
429
+ # @macro report_by_day_and_state
430
+ has_report :annotation_clicks, Integer
431
+
432
+ # @macro report_by_day_and_state
433
+ has_report :annotation_click_through_rate, Float
434
+
435
+ # @macro report_by_day_and_state
436
+ has_report :annotation_close_rate, Float
437
+
438
+ # @macro report_by_day_and_country
439
+ has_report :earnings, Float
440
+
441
+ # @macro report_by_day_and_country
442
+ has_report :impressions, Integer
443
+
444
+ # @macro report_by_day_and_country
445
+ has_report :monetized_playbacks, Integer
446
+
447
+ # @macro report_by_day_and_country
448
+ has_report :playback_based_cpm, Float
449
+
450
+ ### STATISTICS ###
451
+
452
+ has_one :statistics_set
453
+
454
+ # @!attribute [r] view_count
455
+ # @return [Integer] the number of times the video has been viewed.
456
+ delegate :view_count, to: :statistics_set
457
+
458
+ # @!attribute [r] like_count
459
+ # @return [Integer] the number of users who liked the video.
460
+ delegate :like_count, to: :statistics_set
461
+
462
+ # @!attribute [r] dislike_count
463
+ # @return [Integer] the number of users who disliked the video.
464
+ delegate :dislike_count, to: :statistics_set
465
+
466
+ # @!attribute [r] favorite_count
467
+ # @return [Integer] the number of users who marked the video as favorite.
468
+ delegate :favorite_count, to: :statistics_set
469
+
470
+ # @!attribute [r] comment_count
471
+ # @return [Integer] the number of comments for the video.
472
+ delegate :comment_count, to: :statistics_set
473
+
474
+ ### PLAYER ###
475
+
476
+ has_one :player
477
+
478
+ # @!attribute [r] embed_html
479
+ # @return [String] the HTML code of an <iframe> tag that embeds a
480
+ # player that will play the video.
481
+ delegate :embed_html, to: :player
482
+
483
+ ### ACTIONS (UPLOAD, UPDATE, DELETE) ###
484
+
485
+ # Uploads a thumbnail
486
+ # @param [String] path_or_url the image to upload. Can either be the
487
+ # path of a local file or the URL of a remote file.
488
+ # @return the new thumbnail resource for the given image.
489
+ # @raise [Yt::Errors::RequestError] if path_or_url is not a valid path
490
+ # or URL.
491
+ def upload_thumbnail(path_or_url)
492
+ file = open(path_or_url, 'rb') rescue StringIO.new
493
+ session = resumable_sessions.insert file.size
494
+
495
+ session.update(body: file) do |data|
496
+ snippet.instance_variable_set :@thumbnails, data['items'].first
497
+ end
498
+ end
499
+
500
+ # Deletes the video on behalf of the authenticated account.
501
+ # @return [Boolean] whether the video does not exist anymore.
502
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
503
+ # authenticated Yt::Account with permissions to delete the video.
504
+ def delete(options = {})
505
+ do_delete {@id = nil}
506
+ !exists?
507
+ end
508
+
509
+ # Updates the attributes of a video.
510
+ # @return [Boolean] whether the video was successfully updated.
511
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
512
+ # authenticated Yt::Account with permissions to update the video.
513
+ # @param [Hash] attributes the attributes to update.
514
+ # @option attributes [String] :title The new video’s title.
515
+ # Cannot have more than 100 characters. Can include the characters
516
+ # < and >, which are replaced to ‹ › in order to be accepted by YouTube.
517
+ # @option attributes [String] :description The new video’s description.
518
+ # Cannot have more than 5000 bytes. Can include the characters
519
+ # < and >, which are replaced to ‹ › in order to be accepted by YouTube.
520
+ # @option attributes [Array<String>] :tags The new video’s tags.
521
+ # Cannot have more than 500 characters. Can include the characters
522
+ # < and >, which are replaced to ‹ › in order to be accepted by YouTube.
523
+ # @option attributes [String] :category_id The new video’s category ID.
524
+ # @option attributes [String] :privacy_status The new video’s privacy
525
+ # status. Must be one of: private, unscheduled, public.
526
+ # @option attributes [Boolean] :embeddable The new value to specify
527
+ # whether the video can be embedded on other websites.
528
+ # @option attributes [String] :license The new video’s license. Must be
529
+ # one of: youtube, creativeCommon.
530
+ # @option attributes [Boolean] :public_stats_viewable The new value to
531
+ # specify whether the video’s statistics are publicly viewable.
532
+ # @option attributes :publish_at The new timestamp when the video will be
533
+ # made public. Must be in ISO 8601 format (YYYY-MM-DDThh:mm:ss.sZ).
534
+ # @example Update title and description of a video.
535
+ # video.update title: 'New title', description: 'New description'
536
+ # @example Update tags and category ID of a video.
537
+ # video.update tags: ['new', 'tags'], category_id: '22'
538
+ # @example Update status of a video.
539
+ # video.update privacy_status: 'public', public_stats_viewable: true
540
+ # @example Update visibility/license of a video.
541
+ # video.update embeddable: true, license: :youtube
542
+ # @example Update the time when a private video will become public.
543
+ # video.update publish_at: 3.days.from_now.utc.iso8601(3)
544
+ def update(attributes = {})
545
+ super
546
+ end
547
+
548
+ ### PRIVATE API ###
549
+
550
+ # @private
551
+ # Override Resource's new to set statistics and content details as well
552
+ # if the response includes them
553
+ def initialize(options = {})
554
+ super options
555
+ if options[:statistics]
556
+ @statistics_set = StatisticsSet.new data: options[:statistics]
557
+ end
558
+ if options[:content_details]
559
+ @content_detail = ContentDetail.new data: options[:content_details]
560
+ end
561
+ if options[:file_details]
562
+ @file_detail = FileDetail.new data: options[:file_details]
563
+ end
564
+ if options[:live_streaming_details]
565
+ @live_streaming_detail = LiveStreamingDetail.new data: options[:live_streaming_details]
566
+ end
567
+ if options[:video_category]
568
+ @video_category = VideoCategory.new data: options[:video_category]
569
+ end
570
+ if options[:player]
571
+ @player = Player.new data: options[:player]
572
+ end
573
+ end
574
+
575
+ # @private
576
+ def exists?
577
+ !@id.nil?
578
+ end
579
+
580
+ # @private
581
+ # Tells `has_reports` to retrieve the reports from YouTube Analytics API
582
+ # either as a Channel or as a Content Owner.
583
+ # @see https://developers.google.com/youtube/analytics/v1/reports
584
+ def reports_params
585
+ {}.tap do |params|
586
+ if auth.owner_name
587
+ params[:ids] = "contentOwner==#{auth.owner_name}"
588
+ else
589
+ params[:ids] = "channel==#{channel_id}"
590
+ end
591
+ params[:filters] = "video==#{id}"
592
+ end
593
+ end
594
+
595
+ # @private
596
+ # Tells `has_many :resumable_sessions` what path to hit to upload a file.
597
+ def upload_path
598
+ '/upload/youtube/v3/thumbnails/set'
599
+ end
600
+
601
+ # @private
602
+ # Tells `has_many :resumable_sessions` what params are set for the object
603
+ # associated to the uploaded file.
604
+ def upload_params
605
+ {video_id: id}
606
+ end
607
+
608
+ # @private
609
+ # Tells `has_many :resumable_sessions` what type of file can be uploaded.
610
+ def upload_content_type
611
+ 'application/octet-stream'
612
+ end
613
+
614
+ private
615
+
616
+ # TODO: instead of having Video, Playlist etc override this method,
617
+ # they should define *what* can be updated in their own *update*
618
+ # method.
619
+ # @see https://developers.google.com/youtube/v3/docs/videos/update
620
+ # @todo: Add recording details keys
621
+ def update_parts
622
+ snippet_keys = [:title, :description, :tags, :category_id]
623
+ snippet = {keys: snippet_keys, sanitize_brackets: true}
624
+ status_keys = [:privacy_status, :embeddable, :license,
625
+ :public_stats_viewable, :publish_at]
626
+ {snippet: snippet, status: {keys: status_keys}}
627
+ end
628
+ end
629
+ end
630
+ end