youtuberb 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: abca9225386659a452e73a1a188260a389d4b9801ee8bad9cca354f0caf7fe46
4
- data.tar.gz: 7c83234a7f9b0ce77a81ad8d152e3cae422052bee1145c974260011c4c7dbbc7
3
+ metadata.gz: 9723022ed2efe8846dce8650e21077bc2be1e888bb034596b87620ba1cbda433
4
+ data.tar.gz: ed5ecf1b108bd4b9601e4ad93836dbe070dbc16fc60a1c1bca54d79c2d34f5df
5
5
  SHA512:
6
- metadata.gz: 151f38d41251545eb90b3aa978b4b0293222ae1f70fd6edc34ffcc16b44200db773eddb9decb89340177c4050bb19c4c69dd3252951a1335946759b72a9ccfaa
7
- data.tar.gz: 971c715687e1486aab1883055a08ecaa59c9271d223522f11e7b5a7d0902d2d4f2bf1f62329b48d400c2b68b0481211475a984e1bd8002ce47978cb92b49016c
6
+ metadata.gz: 719d5673d88e53d3fd4220a28c4eb4da599127d008a2d0223913eb1fbab10b1ac748f33d88c90c6a16069a908dfc4fa9f156eb063f0bb1e182bba4785bc09de4
7
+ data.tar.gz: 50c1bc722fcd3e0db8d008938aa96e0cd866818246fe94a040caa039a5d37238608771cd8271326a444a9f67ab642192cc5b660a8d8ca8da6dc93e964221c57a
data/Gemfile CHANGED
@@ -6,4 +6,5 @@ source "https://rubygems.org"
6
6
  gemspec
7
7
 
8
8
  gem "rake", "~> 13.0"
9
- gem "dotenv"
9
+ gem "minitest", "~> 5.0"
10
+ gem "dotenv"
data/Gemfile.lock CHANGED
@@ -1,40 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- youtuberb (0.1.2)
5
- faraday (~> 1.7)
6
- faraday_middleware (~> 1.1)
4
+ youtuberb (0.2.0)
5
+ faraday (~> 2.0)
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
10
9
  specs:
10
+ base64 (0.1.1)
11
11
  dotenv (2.7.6)
12
- faraday (1.9.3)
13
- faraday-em_http (~> 1.0)
14
- faraday-em_synchrony (~> 1.0)
15
- faraday-excon (~> 1.1)
16
- faraday-httpclient (~> 1.0)
17
- faraday-multipart (~> 1.0)
18
- faraday-net_http (~> 1.0)
19
- faraday-net_http_persistent (~> 1.0)
20
- faraday-patron (~> 1.0)
21
- faraday-rack (~> 1.0)
22
- faraday-retry (~> 1.0)
12
+ faraday (2.7.11)
13
+ base64
14
+ faraday-net_http (>= 2.0, < 3.1)
23
15
  ruby2_keywords (>= 0.0.4)
24
- faraday-em_http (1.0.0)
25
- faraday-em_synchrony (1.0.0)
26
- faraday-excon (1.1.0)
27
- faraday-httpclient (1.0.1)
28
- faraday-multipart (1.0.3)
29
- multipart-post (>= 1.2, < 3)
30
- faraday-net_http (1.0.1)
31
- faraday-net_http_persistent (1.2.0)
32
- faraday-patron (1.0.0)
33
- faraday-rack (1.0.0)
34
- faraday-retry (1.0.3)
35
- faraday_middleware (1.2.0)
36
- faraday (~> 1.0)
37
- multipart-post (2.1.1)
16
+ faraday-net_http (3.0.2)
17
+ minitest (5.20.0)
38
18
  rake (13.0.6)
39
19
  ruby2_keywords (0.0.5)
40
20
 
@@ -43,8 +23,9 @@ PLATFORMS
43
23
 
44
24
  DEPENDENCIES
45
25
  dotenv
26
+ minitest (~> 5.0)
46
27
  rake (~> 13.0)
47
28
  youtuberb!
48
29
 
49
30
  BUNDLED WITH
50
- 2.3.6
31
+ 2.3.5
data/README.md CHANGED
@@ -16,10 +16,14 @@ gem 'youtuberb'
16
16
 
17
17
  ### Set Client Details
18
18
 
19
- Firstly you'll need to set an API Key and an Access Token.
20
- An Access Token will be an OAuth2 token generated after authentication.
19
+ Firstly you'll need to set an API Key. A user Access Token is required to access private data.
20
+ An Access Token will be an OAuth2 token generated after authentication.
21
21
 
22
22
  ```ruby
23
+ # To access public data, just an API Key is required
24
+ @client = YouTube::Client.new(api_key: "")
25
+
26
+ # Or to access data for a user, an Access Token is also required
23
27
  @client = YouTube::Client.new(api_key: "", access_token: "")
24
28
  ```
25
29
 
@@ -41,15 +45,32 @@ An Access Token will be an OAuth2 token generated after authentication.
41
45
  ```ruby
42
46
  # Get a single video
43
47
  @client.videos.list(id: "abc123")
48
+ # => #<YouTube::Video...
44
49
 
45
50
  # Get multiple videos
46
- @client.videos.list(id: "abc123,123abc")
51
+ @client.videos.list(ids: ["abc123", "123abc"])
52
+ # => #<YouTube::Collection...
47
53
 
48
54
  # Liked videos for the currently authenticated user
49
55
  @client.videos.liked
56
+ # => #<YouTube::Collection...
50
57
 
51
58
  # Get a video owned by the current user. This retrieves extra information so will only work on videos owned by the current user.
52
59
  @client.videos.retrieve(id: "abc123")
60
+ # => #<YouTube::Video...
61
+ ```
62
+
63
+ #### Getting a list of Videos for a Channel
64
+
65
+ ```ruby
66
+ # First, grab the Channel details
67
+ channel = @client.channels.retrieve(id: "channel_id")
68
+
69
+ # Then use the Playlist Items endpoint to get the Videos
70
+ @client.playlist_items.list playlist_id: channel.contentDetails.relatedPlaylists.uploads
71
+
72
+ # Or use the Search endpoint
73
+ @client.search.list channelId: channel.id
53
74
  ```
54
75
 
55
76
  ### Playlists
@@ -69,6 +90,7 @@ An Access Token will be an OAuth2 token generated after authentication.
69
90
  @client.playlists.update(id: "playlist_id", title: "My Playlist", privacy_status: "public")
70
91
  @client.playlists.delete(id: "playlist_id")
71
92
  ```
93
+
72
94
  ### Playlist Items
73
95
 
74
96
  ```ruby
@@ -85,6 +107,21 @@ An Access Token will be an OAuth2 token generated after authentication.
85
107
  @client.playlist_items.delete(id: "playlist_id")
86
108
  ```
87
109
 
110
+ ### Search
111
+
112
+ For a full list of parameters, see the [YouTube API Docs](https://developers.google.com/youtube/v3/docs/search/list).
113
+
114
+ ```ruby
115
+ # Search YouTube for a term
116
+ @client.search.list(q: "search term")
117
+
118
+ # Restrict the search to a channel
119
+ @client.search.list(channelId: "channel")
120
+
121
+ # Search a channel for videos only and ordered by date
122
+ @client.search.list(channelId: "channel", type: "video", order: "date")
123
+ ```
124
+
88
125
  ## Contributing
89
126
 
90
127
  Bug reports and pull requests are welcome on GitHub at https://github.com/deanpcmad/youtuberb.
data/Rakefile CHANGED
@@ -1,4 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- task default: %i[]
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
13
+
@@ -4,13 +4,10 @@ module YouTube
4
4
 
5
5
  attr_reader :api_key, :access_token, :adapter
6
6
 
7
- def initialize(api_key:, access_token:, adapter: Faraday.default_adapter, stubs: nil)
7
+ def initialize(api_key:, access_token: nil, adapter: Faraday.default_adapter)
8
8
  @api_key = api_key
9
9
  @access_token = access_token
10
10
  @adapter = adapter
11
-
12
- # Test stubs for requests
13
- @stubs = stubs
14
11
  end
15
12
 
16
13
  def channels
@@ -29,12 +26,15 @@ module YouTube
29
26
  PlaylistItemsResource.new(self)
30
27
  end
31
28
 
29
+ def search
30
+ SearchResource.new(self)
31
+ end
32
+
32
33
  def connection
33
34
  @connection ||= Faraday.new(BASE_URL) do |conn|
34
35
  conn.request :authorization, :Bearer, access_token
35
36
  conn.request :json
36
37
 
37
- conn.response :dates
38
38
  conn.response :json, content_type: "application/json"
39
39
 
40
40
  conn.adapter adapter, @stubs
@@ -7,7 +7,7 @@ module YouTube
7
7
 
8
8
  new(
9
9
  data: body["items"].map { |attrs| type.new(attrs) },
10
- total: body["pageInfo"]["totalResults"],
10
+ total: body["items"].count,
11
11
  next_page_token: body["nextPageToken"],
12
12
  prev_page_token: body["prevPageToken"],
13
13
  # cursor: body.dig("pagination", "cursor")
@@ -0,0 +1,30 @@
1
+ module YouTube
2
+ class SearchResult < Object
3
+
4
+ def initialize(options = {})
5
+ super options
6
+
7
+ if options["id"]
8
+ self.video_id = options["id"]["videoId"] || nil
9
+ self.playlist_id = options["id"]["playlistId"] || nil
10
+ end
11
+
12
+ if options["snippet"]
13
+ self.title = options["snippet"]["title"]
14
+ self.description = options["snippet"]["description"]
15
+ self.published_at = options["snippet"]["publishedAt"]
16
+ self.channel_id = options["snippet"]["channelId"]
17
+
18
+ if options["snippet"]["thumbnails"]
19
+ thumb = options["snippet"]["thumbnails"]
20
+ self.thumbnail_default = thumb["default"]["url"] if thumb["default"]
21
+ self.thumbnail_medium = thumb["medium"]["url"] if thumb["medium"]
22
+ self.thumbnail_high = thumb["high"]["url"] if thumb["high"]
23
+ self.thumbnail_standard = thumb["standard"]["url"] if thumb["standard"]
24
+ self.thumbnail_maxres = thumb["maxres"]["url"] if thumb["maxres"]
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -9,7 +9,7 @@ module YouTube
9
9
  self.description = options["snippet"]["description"]
10
10
  self.published_at = options["snippet"]["publishedAt"]
11
11
  self.channel_id = options["snippet"]["channelId"]
12
-
12
+
13
13
  if options["snippet"]["thumbnails"]
14
14
  thumb = options["snippet"]["thumbnails"]
15
15
  self.thumbnail_default = thumb["default"]["url"] if thumb["default"]
@@ -25,6 +25,12 @@ module YouTube
25
25
  self.blocked = !options["contentDetails"]["regionRestriction"].nil?
26
26
  end
27
27
 
28
+ if options["liveStreamingDetails"]
29
+ self.live_stream = options["liveStreamingDetails"]["actualStartTime"].present?
30
+ else
31
+ self.live_stream = false
32
+ end
33
+
28
34
  if options["status"]
29
35
  self.upload_status = options["status"]["uploadStatus"]
30
36
  self.privacy_status = options["status"]["privacyStatus"]
@@ -1,7 +1,7 @@
1
1
  module YouTube
2
2
  class ChannelsResource < Resource
3
3
 
4
- PARTS = "id,snippet,status,statistics"
4
+ PARTS = "id,snippet,status,statistics,contentDetails"
5
5
 
6
6
  # Retrieve the channel of the currently authenticated user
7
7
  def mine
@@ -9,12 +9,13 @@ module YouTube
9
9
  return nil if response.body["pageInfo"]["totalResults"] == 0
10
10
  Channel.new(response.body["items"][0])
11
11
  end
12
-
12
+
13
13
  # Retrieve a Channel by its ID or Username
14
14
  def retrieve(id: nil, username: nil)
15
15
  attrs = {}
16
16
  attrs[:id] = id if id
17
17
  attrs[:forUsername] = username if username
18
+ raise ArgumentError, "Must provide either an ID or Username" if attrs.empty?
18
19
 
19
20
  response = get_request "channels", params: attrs.merge({part: PARTS})
20
21
  return nil if response.body["pageInfo"]["totalResults"] == 0
@@ -0,0 +1,10 @@
1
+ module YouTube
2
+ class SearchResource < Resource
3
+
4
+ def list(**params)
5
+ response = get_request "search", params: {part: "id,snippet"}.merge(params)
6
+ Collection.from_response(response, type: SearchResult)
7
+ end
8
+
9
+ end
10
+ end
@@ -1,13 +1,27 @@
1
1
  module YouTube
2
2
  class VideosResource < Resource
3
3
 
4
- PARTS = "id,contentDetails,snippet,statistics,status"
5
- AUTH_PARTS = "id,contentDetails,fileDetails,processingDetails,snippet,statistics,status,suggestions"
6
-
4
+ PARTS = "id,snippet,contentDetails,liveStreamingDetails,statistics,status"
5
+ AUTH_PARTS = "id,snippet,contentDetails,fileDetails,processingDetails,statistics,status,suggestions"
6
+
7
7
  # Retrieves Videos by ID. Can be comma separated to retrieve multiple.
8
- def list(id:)
9
- response = get_request "videos", params: {id: id, part: PARTS}
10
- Collection.from_response(response, type: Video)
8
+ def list(id: nil, ids: nil)
9
+ raise "Either id or ids is required" unless !id.nil? || !ids.nil?
10
+
11
+ if id
12
+ response = get_request("videos", params: {id: id, part: PARTS})
13
+ elsif ids
14
+ response = get_request("videos", params: {id: ids.join(","), part: PARTS})
15
+ end
16
+
17
+ body = response.body["items"]
18
+ if body.count == 1
19
+ Video.new body[0]
20
+ elsif body.count > 1
21
+ Collection.from_response(response, type: Video)
22
+ else
23
+ return nil
24
+ end
11
25
  end
12
26
 
13
27
  # Retrieves liked Videos for the currently authenticated user
@@ -1,3 +1,3 @@
1
1
  module YouTube
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/you_tube.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require "faraday"
2
- require "faraday_middleware"
3
2
  require "json"
4
3
 
5
4
  require_relative "you_tube/version"
@@ -16,10 +15,12 @@ module YouTube
16
15
  autoload :VideosResource, "you_tube/resources/videos"
17
16
  autoload :PlaylistsResource, "you_tube/resources/playlists"
18
17
  autoload :PlaylistItemsResource, "you_tube/resources/playlist_items"
18
+ autoload :SearchResource, "you_tube/resources/search"
19
19
 
20
20
  autoload :Channel, "you_tube/objects/channel"
21
21
  autoload :Video, "you_tube/objects/video"
22
22
  autoload :Playlist, "you_tube/objects/playlist"
23
23
  autoload :PlaylistItem, "you_tube/objects/playlist_item"
24
+ autoload :SearchResult, "you_tube/objects/search_result"
24
25
 
25
26
  end
data/youtuberb.gemspec CHANGED
@@ -28,6 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
30
 
31
- spec.add_dependency "faraday", "~> 1.7"
32
- spec.add_dependency "faraday_middleware", "~> 1.1"
31
+ spec.add_dependency "faraday", "~> 2.0"
33
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youtuberb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Perry
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-05 00:00:00.000000000 Z
11
+ date: 2023-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,29 +16,15 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
27
- - !ruby/object:Gem::Dependency
28
- name: faraday_middleware
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.1'
41
- description:
26
+ version: '2.0'
27
+ description:
42
28
  email:
43
29
  - dean@deanpcmad.com
44
30
  executables: []
@@ -61,11 +47,13 @@ files:
61
47
  - lib/you_tube/objects/channel.rb
62
48
  - lib/you_tube/objects/playlist.rb
63
49
  - lib/you_tube/objects/playlist_item.rb
50
+ - lib/you_tube/objects/search_result.rb
64
51
  - lib/you_tube/objects/video.rb
65
52
  - lib/you_tube/resource.rb
66
53
  - lib/you_tube/resources/channels.rb
67
54
  - lib/you_tube/resources/playlist_items.rb
68
55
  - lib/you_tube/resources/playlists.rb
56
+ - lib/you_tube/resources/search.rb
69
57
  - lib/you_tube/resources/videos.rb
70
58
  - lib/you_tube/version.rb
71
59
  - lib/youtuberb.rb
@@ -76,7 +64,7 @@ licenses:
76
64
  metadata:
77
65
  homepage_uri: https://deanpcmad.com
78
66
  source_code_uri: https://github.com/deanpcmad/youtuberb
79
- post_install_message:
67
+ post_install_message:
80
68
  rdoc_options: []
81
69
  require_paths:
82
70
  - lib
@@ -91,8 +79,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
79
  - !ruby/object:Gem::Version
92
80
  version: '0'
93
81
  requirements: []
94
- rubygems_version: 3.1.6
95
- signing_key:
82
+ rubygems_version: 3.4.10
83
+ signing_key:
96
84
  specification_version: 4
97
85
  summary: A Ruby library for interacting with the YouTube API
98
86
  test_files: []